CSS (Cascading Style Sheet)
CSS (Cascading Style Sheet)
CSS (Cascading Style Sheet)
• CSS saves a lot of work. It can control the layout of multiple web
pages all at once.
Prepared By: Farhad Mirkhail 2
CSS Introduction/ CSS Solved a Big Problem
HTML was NEVER intended to contain tags for formatting a web page!
• <h1>This is a heading</h1>
• <p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2
specification, it started a nightmare for web developers. Development of large
websites, where fonts and color information were added to every single page,
became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
Each declaration includes a CSS property name and a value, separated by a colon.
A CSS declaration always ends with a semicolon, and declaration blocks are
surrounded by curly braces.
Prepared By: Farhad Mirkhail 4
CSS Syntax/ Example
In the following example all <p> elements will be center-
aligned, with a red text color:
p {
color: red;
text-align: center;
}
You can select all <p> elements on a page like this (in this
case, all <p> elements will be center-aligned, with a red text
color):
Exp:
p {
text-align: center;
color: red;
}
Prepared By: Farhad Mirkhail 7
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be unique within a page, so the id selector is used to select
one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id
of the element.
The style rule below will be applied to the HTML element with id="para1":
In the example below, all HTML elements with class="center" will be red and
center-aligned:
Exp:
.center {
text-align: center;
color: red; }
Exp:
p.center {
text-align: center;
color: red; }
Exp:
<p class="center large">This paragraph refers to two classes.</p>
Exp:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
Prepared By: Farhad Mirkhail 12
Grouping Selectors – continue
It will be better to group the selectors, to minimize the code.
Exp:
h1, h2, p {
text-align: center;
color: red; }
Exp:
table tr td p {
color: red;
text-align: center;
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:
p {
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
Prepared By: Farhad Mirkhail 15
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
Each page must include a reference to the external style sheet file inside
the <link> element. The <link> element goes inside the <head> section:
Exp:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
body {
background-color: lightblue; }
h1 {
color: navy;
margin-left: 20px; }
Note: Do not add a space between the property value and the unit (such as margin-left: 20 px;).
Internal styles are defined within the <style> element, inside the <head> section of
an HTML page:
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Prepared By: Farhad Mirkhail 20
Inline Styles
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.
The example below shows how to change the color and the left margin of a <h1>
element:
Tip: An inline style loses many of the advantages of a style sheet (by mixing content
with presentation). Use this method sparingly.
Generally speaking we can say that all the styles will "cascade" into a new "virtual"
style sheet by the following rules, where number one has the highest priority:
Each parameter (red, green, and blue) defines the intensity of the color between
0 and 255.
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest
value (255) and the others are set to 0.
To display the color black, all color parameters must be set to 0, like this: rgb(0,
0, 0).
To display the color white, all color parameters must be set to 255, like this:
rgb(255, 255, 255).
Prepared By: Farhad Mirkhail 26
RGB Color Examples
#rrggbb
For example, #ff0000 is displayed as red, because red is set to its highest
value (ff) and the others are set to the lowest value (00).
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green,
and 240 is blue.
50% is 50% gray, but you can still see the color.
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
Prepared By: Farhad Mirkhail 39
Background Color
The background-color property specifies the background color of an element.
body {
background-color: lightblue; }
h1 { background-color: green; }
body {
background-image: url("paper.gif");
}
When using the shorthand property the order of the property values is:
Background-color
Background-image
Background-repeat
Background-attachment
Background-position
The CSS border properties allow you to specify the style, width, and color
of an element's 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
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.
The border-width property can have from one to four values (for the top
border, right border, bottom border, and the left border).
p.two {
border-style: solid;
border-width: medium; }
p.three {
border-style: solid;
border-width: 2px 10px 4px 20px; }
Prepared By: Farhad Mirkhail 51
Border Color
The border-color property is used to set the color of the four borders.
The border-color property can have from one to four values (for the top
border, right border, bottom border, and the left border).
border-style: dotted;
all four borders are dotted
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
Prepared By: Farhad Mirkhail 58
CSS Margins - continue
All the margin properties can have the following values:
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
p {
margin: 25px 50px 75px;
}
margin: 25px;
all four margins are 25px
p {margin: 25px;}
The element will then take up the specified width, and the remaining space
will be split equally between the left and right margins:
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
Prepared By: Farhad Mirkhail 64
The inherit Value
div {
border: 1px solid red;
margin-left: 100px;
}
p.ex1 {
margin-left: inherit;
}
This does not happen on left and right margins! Only top and bottom margins!
h1 {
margin: 0 0 50px 0;}
h2 {
margin: 20px 0 0 0;}
In the example above, the <h1> element has a bottom margin of 50px and the <h2>
element has a top margin set to 20px.
Common sense would seem to suggest that the vertical margin between the <h1> and
the <h2> would be a total of 70px (50px + 20px). But due to margin collapse, the
actual margin ends up being 50px.
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
div {
padding: 25px 50px 75px;
}
padding: 25px;
all four paddings are 25px
So, if an element has a specified width, the padding added to that element will be added to the
total width of the element. This is often an undesirable result.
In the following example, the <div> element is given a width of 300px. However, the actual
rendered width of the <div> element will be 350px (300px + 25px of left padding + 25px of right
padding):
div {
width: 300px;
padding: 25px;
}
Prepared By: Farhad Mirkhail 75
Padding and Element Width – continue
To keep the width at 300px, no matter the amount of padding, you can use the box-
sizing property. This causes the element to maintain its width; if you increase the
padding, the available content space will decrease. Here is an example:
div {
width: 300px;
padding: 25px;
box-sizing: border-box;
}
The height and width can be set to auto (this is default. Means that the browser
calculates the height and width), or be specified in length values, like px, cm,
etc., or in percent (%) of the containing block.
div {
height: 100px;
width: 500px;
background-color: powderblue; }
The max-width can be specified in length values, like px, cm, etc., or in
percent (%) of the containing block, or set to none (this is default. Means
that there is no maximum width).
The problem with the <div> above occurs when the browser window is smaller
than the width of the element (500px). The browser then adds a horizontal
scrollbar to the page.
The max-width can be specified in length values, like px, cm, etc., or in
percent (%) of the containing block, or set to none (this is default. Means
that there is no maximum width).
The problem with the <div> above occurs when the browser window is smaller than the width
of the element (500px). The browser then adds a horizontal scrollbar to the page.
Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!
Prepared By: Farhad Mirkhail 80
Setting max-width – continue
Note: The value of the max-width property overrides width.
The following example shows a <div> element with a height of 100 pixels and
a max-width of 500 pixels:
div {
max-width: 500px;
height: 100px;
background-color: powderblue;
}
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:
• Content - The content of the box, where text and images appear
The box model allows us to add a border around elements, and to define space
between elements.
div {
width: 300px;
border: 25px solid green;
padding: 25px;
margin: 25px;
}
Prepared By: Farhad Mirkhail 85
Width and Height of an Element
In order to set the width and height of an element correctly in
all browsers, you need to know how the box model works.
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
• outline-style
• outline-color
• outline-width
• outline-offset
• outline
Prepared By: Farhad Mirkhail 90
CSS Outline – continue
Note: Outline differs from borders! Unlike border, the outline
is drawn outside the element's border, and may overlap other
content. Also, the outline is NOT a part of the element's
dimensions; the element's total width and height is not
affected by the width of the outline.