Cascading Stylesheet Tutorial SMP2 Material
Cascading Stylesheet Tutorial SMP2 Material
Cascading Stylesheet
CSS is the language we use to style an HTML document.
CSS describes how HTML elements should be displayed.
What is CSS?
• CSS stands for Cascading Style Sheets
• CSS describes how HTML elements are to be displayed on screen, paper,
or in other media
• CSS saves a lot of work. It can control the layout of multiple web pages
all at once
Universal School 1
Sanjay Sharma
<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.
With an external stylesheet file, you can change the look of an entire website by
changing just one file!
CSS Syntax
Universal School 2
Sanjay Sharma
Multiple CSS declarations are separated with semicolons, and declaration blocks
are surrounded by curly braces.
Example
In this example all <p> elements will be center-aligned, with a red text color:
p {
color: red;
text-align: center;
}
Example Explained
• p is a selector in CSS (it points to the HTML element you want to style:
<p>).
• color is a property, and red is the property value
• text-align is a property, and center is the property value
Universal School 3
Sanjay Sharma
Example
Here, all <p> elements on the page will be center-aligned, with a red text
color:
p {
text-align: center;
color: red;
}
To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
Universal School 4
Sanjay Sharma
To select elements with a specific class, write a period (.) character, followed by
the class name.
Example
In this example all HTML elements with class="center" will be red and center-
aligned:
.center {
text-align: center;
color: red;
}
Universal School 5
Sanjay Sharma
Example
In this example the <p> element will be styled according to class="center" and
to class="large":
Universal School 6
Sanjay Sharma
Example
The CSS rule below will affect every HTML element on the page:
* {
text-align: center;
color: blue;
}
Look at the following CSS code (the h1, h2, and p elements have the same style
definitions):
Universal School 7
Sanjay Sharma
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center; h1, h2, p {text-align: center; color: red;}
color: red;
}
p {
text-align: center;
color: red;
}
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
Universal School 8
Sanjay Sharma
Exercise 1:
Change the color of all <p> elements to "red".
Exercise 2:
Change the color of the element with id="para1", to "red".
Exercise 3:
Change the color of all elements with the class "colortext", to "red".
----------------------------------------------------------------------------------
• External CSS
• Internal CSS
• Inline CSS
Universal School 9
Sanjay Sharma
Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal CSS is defined inside the <style> element, inside the head section.
Example
Internal CSS are defined within the <style> element, inside the <head> section
of an HTML page:
Universal School 10
Sanjay Sharma
Inline CSS
An inline CSS 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:
Universal School 11
Sanjay Sharma
External 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:
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.
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Universal School 12
Sanjay Sharma
Note: Do not add a space between the property value and the unit (such as margin-left: 20 px;). The
correct way is: margin-left: 20px;
Assume that an external style sheet has the following style for the <h1>
element:
h1 {
color: navy;
}
Then, assume that an internal style sheet also has the following style for the
<h1> element:
h1 {
color: orange;
}
Example
If the internal style is defined after the link to the external style sheet, the
<h1> elements will be "orange":
Universal School 13
Sanjay Sharma
Example
However, if the internal style is defined before the link to the external style
sheet, the <h1> elements will be "navy":
Cascading Order
What style will be used when there is more than one style specified for an HTML
element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the
following rules, where number one has the highest priority:
Universal School 14
Sanjay Sharma
So, an inline style has the highest priority, and will override external and
internal styles and browser defaults.
Exercise:
1. Add an external style sheet with the URL: "mystyle.css".
2. Set "background-color: linen" for the page, using an internal style sheet.
3. Set "background-color: linen" for the page, using an inline style.
Universal School 15
Sanjay Sharma
CSS Comments
1. CSS comments are not displayed in the browser, but they can help document
your source code.
2. Comments are used to explain the code, and may help when you edit the source
code at a later date.
3. Comments are ignored by browsers.
4. A CSS comment is placed inside the <style> element, and starts with /* and ends
with */:
Universal School 16
Sanjay Sharma
Universal School 17
Sanjay Sharma
Universal School 18
Sanjay Sharma
Hello
Universal School 19
Sanjay Sharma
Hello World
Hello World
Hello World
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
----------------------------------------------------------------
CSS Backgrounds
The CSS background properties are used to add background effects for elements.
In this section you will learn about the following CSS background properties:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
• background (shorthand property)
Universal School 20
Sanjay Sharma
CSS background-color
The background-color property specifies the background color of an element.
Example
The background color of a page is set like this:
body {
background-color: lightblue;
}
You can set the background color for any HTML elements:
Example
Here, the <h1>, <p>, and <div> elements will have different background
colors:
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p {
background-color: yellow;
}
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:
Example
Universal School 21
Sanjay Sharma
div {
background-color: green;
opacity: 0.3;
}
CSS background-image
The background-image property specifies an image to use as the background of an
element.
Example
Set the background image for a page:
body {
background-image: url("paper.gif");
}
The background image can also be set for specific elements, like the <p>
element:
Example
p {
background-image: url("paper.gif");
}
add background for the body in the below code.
Universal School 22
Sanjay Sharma
Universal School 23
Sanjay Sharma
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:
Universal School 24
Sanjay Sharma
Example
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
Universal School 25
Sanjay Sharma
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;
}
Universal School 26
Sanjay Sharma
Example
Specify that the background image should be fixed:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
Specify that the background image should scroll with the rest of the page:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: scroll;
}
Instead of writing:
body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
Example
Use the shorthand property to set the background properties in one declaration:
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
Universal School 27
Sanjay Sharma
When using the shorthand property the order of the property values is:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
It does not matter if one of the property values is missing, as long as the other
ones are in this order. Note that we do not use the background-attachment
property in the examples above, as it does not have a value.
Exercise:
1. Set the background color for the page to "linen" and the background color
for <h1> to "lightblue".
2. Set "paper.gif" as the background image of the page
3. Set "gradient_bg_vertical.png" as the background image of the div, and
repeat it vertically only.
4. Specify that the background image for div should be shown once, in the
top right corner.
The border-style property can have from one to four values (for the top border,
right border, bottom border, and the left border).
Universal School 28
Sanjay Sharma
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;}
Result:
A dotted border.
A dashed border.
A solid border.
A double border.
No border.
A hidden border.
A mixed border.
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;
}
Result:
Universal School 30
Sanjay Sharma
The following image shows a web page and its output, all three CSS types are used in the
below example. Write the below code and find out what could be the external CSS.
Universal School 31
Sanjay Sharma
The
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 */
}
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;
}
Universal School 32
Sanjay Sharma
Result
Example
p.one {
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue
bottom and yellow left */
}
Universal School 33
Sanjay Sharma
CSS Margins
The CSS margin properties are used to create space around elements, outside of
any defined borders.
With CSS, you have full control over the margins. There are properties for
setting the margin for each side of an element (top, right, bottom, and left).
• margin-top
• margin-right
• margin-bottom
• margin-left
Universal School 34
Sanjay Sharma
Example
Use the margin shorthand property with three values:
p {
margin: 25px 50px 75px;
}
The element will then take up the specified width, and the remaining space will
be split equally between the left and right margins.
Example
Use margin: auto:
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
Universal School 35
Sanjay Sharma
Universal School 36