Css 1
Css 1
Topics
• CSS Introduction,
• Advantages, Uses,
• CSS syntax,
• Style Specification format:Css Selectors,
• Types of CSS(Levels of CSS),
• Box Model,
• Variousproperties(Colors,Background,Border,Margins,Pad
ding,Height and width,Text ,Font,links)
CSS
• CSS stands for Cascading Style Sheets
• CSS is used to define styles for your web pages, including the design,
layout and variations in display for different devices and screen sizes
• cascading style sheets are a language used to simplify the process
of making a webpage.
• CSS is used to handle some parts of the webpage. With the help of
CSS, we can control the colour of text and style of fonts, and we can
control the spacing between the paragraph and many more things.
• CSS is easy to understand but provides strong control on the Html
documents.
• CSS is combined with HTML CSS saves a lot of work. It can control
the layout of multiple web pages all at once
• External stylesheets are stored in CSS files
Advantages of CSS
Faster page speed: It has a faster page speed than other code's page speeds. With the
help of the CSS rule, we can apply it to all occurrences of certain tags in HTML
documents.
Better user experience: CSS makes a webpage very attractive to the eyes. Also, CSS
makes it user-friendly. When the button or text is in a proper format, it improves the
user experience.
Quicker Development time: With the help of CSS, we can specify the format and style
the multiple pages into one code string. In cascading style sheet, we can make a
duplicate copy of several website pages.
If we make a webpage, it has the same formatting, looks, and feel, so with the help of
the CSS rule for one page, and it is sufficient for all the pages.
Easy Formatting changes: In CSS, if we need to make changes in the format, it is very
easy; we only need to change the one-page format it will automatically apply to the
other pages of CSS.
There is no need to correct individual pages in a CSS style sheet. If we fix a CSS style
sheet, it will automatically update the other CSS style sheet.
Compatibility: Compatibility is very important in today's age. If we create any webpage,
it should be very responsive and user-friendly. CSS is used with Html to make webpage
design responsive.
Why do We Use CSS?
As we all know, CSS is a powerful style sheet language used to control the HTML
document to improve the webpage design.
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
Example of CSS
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
h1 {
colour: white;
text-align: center;
}
p{
font-family: Verdana;
font-size: 20px;
}
</style>
</head>
<body>
<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Syntax
• A CSS rule-set consists of a selector and a declaration block:
Syntax:
selector {
property1: value1;
property2: value2;
...
property_n: value_n;
}
.center {
text-align: center;
color: red;
}
4. The CSS Universal Selector
*{
text-align: center;
color: blue;
}
5. The CSS Grouping Selector
• The grouping selector selects all the HTML elements with the same style
definitions.
• Look at the following CSS code (the h1, h2, and p elements have the same style
definitions):
Example
In this example we have grouped
• h1 { the selectors from the code above:
text-align: center;
color: red;
}
h1, h2, p {
h2 {
text-align: center;
text-align: center;
color: red; color: red;
} }
p{
text-align: center;
color: red;
}
All CSS Simple Selectors
element,eleme div, p Selects all <div> elements and all <p> elements
nt,..
Types of CSS or Levels of CSS
• With an external style sheet, you can change the look of an entire website
by changing just one file!
• Each HTML page must include a reference to the external style sheet file
inside the <link> element, inside the head section.
• Example: External styles are defined within the <link> element, inside the
<head> section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
1. External CSS: css file
• An external style sheet can be written in any text editor, and
must be saved with a .css extension.
• The external .css file should not contain any HTML tags.
Here is how the "mystyle.css" file looks like:
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
2. Internal CSS
• An internal style sheet may be background-color: linen;
used if one single HTML page}
has a unique style.
• The internal style is definedh1 {
inside the <style> element, color: maroon;
inside the head section. margin-left: 40px;
• Example: Internal styles are}
defined within the <style></style>
element, inside the <head></head>
section of an HTML page: <body>
<h1>This is a heading</h1>
<!DOCTYPE html> <p>This is a paragraph.</p>
<html>
<head> </body>
<style> </html>
body {
3. Inline CSS
• An inline style may be used to apply a unique style for a single element.
• To use inline styles, add the style attribute to the relevant element. The
style attribute can contain any CSS property.
• Example: Inline styles are defined within the "style" attribute of the
relevant element:
• <!DOCTYPE html>
<html>
<body>
</body>
</html>
CSS Comments
• Comments are used to explain the code, and may help
when you edit the source code at a later date.
• Comments are ignored by browsers.
• A CSS comment is placed inside the <style> element, and
starts with /* and ends with */:
Example
/* This is a single-line comment */
p{
color: red;
Box Model
CSS Box Model
• All HTML elements can be considered as boxes. In CSS,
the term "box model" is used when talking about
design and layout.
• The CSS box model is essentially a box that wraps
around every HTML element. It consists of: margins,
borders, padding, and the actual content.
• The image below illustrates the box model:
Explanation of the different parts:
1.Content - The content of the box, where text and images
appear
2.Padding - Clears an area around the content. The padding is
transparent
3.Border - A border that goes around the padding and content
4.Margin - Clears an area outside the border. The margin is
transparent
• The box model allows us to add a border around elements,
and to define space between elements.
Example: Demonstration of the box model:
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Properties of
• Colors
• Background
• Border
• Margins
• Padding
• Height and width
• Text
• Font
• links
Colors
• CSS Background Color
• Text Color
• Border Color
CSS Colors
Example:
Example:
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
body {
background-color: lightblue;
}
Opacity / Transparency
• The opacity property specifies the opacity/transparency of an element.
It can take a value from 0.0 - 1.0. The lower value, the more transparent:
body {
background-color: lightblue;
opacity:0.3;
}
2. CSS Background Image
CSS background-image
• The background-image property specifies an image to use as the background of an
element.
• By default, the image is repeated so it covers the entire element.
Example
• The background image for a page can be set like this:
body {
background-image: url("paper.gif");
}
body { background-image:
url('http://www.birds.com/wp-content/uploads/home/bird4.jpg');}
body {background-image:linear-gradient(red,yellow); }
body {background-image:radial-gradient(red,yellow); }
body {background-image:conic-gradient(red,blue,yellow); }
3. CSS Background Repeat
CSS background-repeat
• By default, the background-image property repeats an
image both horizontally and vertically.
• Some images should be repeated only horizontally or
vertically, or they will look strange, like this:
• Example
body {
background-image: url("gradient_bg.png");
background-repeat: norepeat
}
CSS background-repeat: no-repeat
• Showing the background image only once is
also specified by the background-
repeat property:
Example
• Show the background image only once:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
CSS background-position
• The background-position property is used to specify the position of the background image.
Example
• Position the background image in the top-right corner:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
• left top
• left center
• left bottom
• right top
• right center
• right bottom
• center top
• center center
• center bottom
4. CSS background-attachment
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
Or
background-attachment: scroll;
5. CSS background - Shorthand property
• To shorten the code, it is also possible to specify all the background
properties in one single property. This is called a shorthand property.
• Instead of writing:
body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
background-color
background-image
background-position
background-size
background-repeat
background-origin
background-clip
background-attachment
Borders
Border-Style
border-width: thin, medium, or thick
border-color
border-top-style
border-right-style
border-bottom-style
border-left-style
border-radius
CSS Borders
• CSS Border Properties: T he CSS border properties allow you to specify the
style, width, and color of an element's border.
1. CSS Border Style: The border-style property specifies what kind of
border to display.
dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double border
groove - Defines a 3D grooved border. The effect depends on the border-color
value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color
value
none - Defines no border
hidden - Defines a hidden border
Example: Demonstration of the different border
styles:
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}
2. CSS Border Width
• The border-width property specifies the width of the four borders.
• The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of the three
pre-defined values: thin, medium, or thick:
• p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
• Specific Side Widths: The border-width property can have from one to
four values (for the top border, right border, bottom border, and the left
border):
• Example
p.one {
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides */
}
p.two {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides */
}
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom
and 35px left */
}
3. CSS Border Color
• The border-color property is used to set the color of
the four borders.
• The color can be set by:
• name - specify a color name, like "red"
• HEX - specify a HEX value, like "#ff0000"
• RGB - specify a RGB value, like "rgb(255,0,0)"
• HSL - specify a HSL value, like "hsl(0, 100%, 50%)"
• transparent
• Note: If border-color is not set, it inherits the color
of the element.
Example
Demonstration of the different border colors:
• p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
• Specific Side Colors
• The border-color property can have from one
to four values (for the top border, right border,
bottom border, and the left border).
• Example
• p.one { border-style: solid;
border-color: red green blue yellow; }
/* red top, green right, blue bottom and yellow
left */
4.CSS Border - Individual Sides
/* Three values */
p{
border-style: dotted solid double;
}
/* Two values */
p{
border-style: dotted solid;
}
/* One value */
p{
border-style: dotted;
}
CSS Border - Shorthand Property
• To shorten the code, it is also possible to specify all the individual
border properties in one property.
• The border property is a shorthand property for the following
individual border properties:
border-width
border-style (required)
border-color
• Example
p{
border: 5px solid red;
}
Left Border
• p{
border-left: 6px solid red;
background-color: lightgrey;
}
Bottom Border
• p{
border-bottom: 6px solid red;
background-color: lightgrey;
}
5. CSS Rounded Borders
• The border-radius property is used to add
rounded borders to an element
• Example
p{
border: 2px solid red;
border-radius: 5px;
}
All CSS Border
Properties
Property Description
p{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
• Margin - Shorthand Property: To shorten the code, it is
possible to specify all the margin properties in one property.
• The margin property is a shorthand property for the
following individual margin properties:
margin-top
margin-right
margin-bottom
margin-left
• So, here is how it works:
• If the margin property has four values:
• margin: 25px 50px 75px 100px;
– top margin is 25px
– right margin is 50px
– bottom margin is 75px
– left margin is 100px
2. CSS Margin Collapse
• Margin Collapse: Top and bottom margins of elements are
sometimes collapsed into a single margin that is equal to the
largest of the two margins.
• This does not happen on left and right margins! Only top and
bottom margins!
• Example: Demonstration of margin collapse:
h1 {
margin: 0 0 50px 0;
}
h2 {
margin: 20px 0 0 0;
}
All CSS Margin Properties
Property Description
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
• Padding - Shorthand Property
• To shorten the code, it is possible to specify all the padding
properties in one property.
• The padding property is a shorthand property for the following
individual padding properties:
padding-top
padding-right
padding-bottom
padding-left
• So, here is how it works:If the padding property has four values:
• padding: 25px 50px 75px 100px;
– top padding is 25px
– right padding is 50px
– bottom padding is 75px
– left padding is 100px
• Examples: div { padding: 25px 50px 75px 100px; }
All CSS Padding Properties
Property Description
• CSS height and width Values: The height and width properties may
have the following values:
• auto - This is default. The browser calculates the height and width
• length - Defines the height/width in px, cm etc.
• % - Defines the height/width in percent of the containing block
• initial - Sets the height/width to its default value
• inherit - The height/width will be inherited from its parent value
• Example1:Set the height and width of a <div> element:
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
• Example2:Set the height and width of another <div>
element:
div {
height: 100px;
width: 500px;
background-color: powderblue;
}
Text
CSS Text
1)Text Color: The color property is used to set the color of the text. The
color is specified by:
h1 {
color: green;
}
2)Text Alignment: The text-align property is used to set the
horizontal alignment of a text.
• A text can be left or right aligned, centered, or justified.
• The following example shows center aligned, and left and right
aligned text (left alignment is default if text direction is left-to-
right, and right alignment is default if text direction is right-to-left):
Example
• h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
• Text Direction: The direction and unicode-bidi properties can be
used to change the text direction of an element:
EX:
p{
direction: rtl;
unicode-bidi: bidi-override;
}
Vertical Alignment
• The vertical-align property sets the vertical alignment of an
element.
• This example demonstrates how to set the vertical alignment of an
image in a text:
EX: img.top { vertical-align: top; }
h1 { text-decoration: overline; }
h2 { text-decoration: line-through; }
h3 { text-decoration: underline; }
4)Text Transformation: The text-transform property is
used to specify uppercase and lowercase letters in a text.
• It can be used to turn everything into uppercase or
lowercase letters, or capitalize the first letter of each
word:
Example
h1 { letter-spacing: 3px; }
h2 { letter-spacing: -3px; }
3)Line Height: The line-height property is used to specify the space
between lines:
• p.small { line-height: 0.8; }
h2 { word-spacing: -5px; }
5)White Space: The white-space property specifies how white-space
inside an element is handled.
• This example demonstrates how to disable text wrapping inside an
element:
p { white-space: nowrap;}
6)CSS Text Shadow
1)Text Shadow: The text-shadow property adds
shadow to text.
• In its simplest use, you only specify the horizontal
shadow (2px) and the vertical shadow (2px):
Property Description
text-overflow Specifies how overflowed content that is not displayed should be signaled to the user
unicode-bidi Used together with the direction property to set or return whether the text should be overridden to
support multiple languages in the same document
.serif {
font-family: "Times New Roman", Times, serif;
}
.sansserif {
font-family: Arial, Helvetica, sans-serif;
}
.monospace {
font-family: "Lucida Console", Courier, monospace;
}
2. CSS Font Style
1)Font Style: The font-style property is mostly used to specify italic text.
• This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to italic, but less
supported)
• Example
h2 { font-size: 30px;}
p { font-size: 14px;}
• Set Font Size With Em: To allow users to resize the text
(in the browser menu), many developers use em instead
of pixels.
• The em size unit is recommended by the W3C.
• 1em is equal to the current font size. The default text size
in browsers is 16px. So, the default size of 1em is 16px.
• The size can be calculated from pixels to em using this
formula: pixels/16=em
• Example
• h1 { font-size: 2.5em; /* 40px/16=2.5em */}
</style>
</head>
<body>
<h1>Sofia Font</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</body>
</html>
5.Shortand
/* visited link */
a:visited { color: green; }
/* selected link */
a:active { color: blue;}
• 2) Text Decoration
• The text-decoration property is mostly used to remove underlines from links:
• Example
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
3. Background Color
• The background-color property can be used to specify a background color for
links:
• Example
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
4. Link Buttons: This example demonstrates a more advanced
example where we combine several CSS properties to display links
as boxes/buttons:
• Example
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
Thank you