L3 Cascading Style Sheets
L3 Cascading Style Sheets
1
HTML AND FORMATTING
}
GROUPING SELECTORS
You can group HTML Selectors together:
}
CSS
When a browser reads a style sheet, it will format the
document according to it.
There are three ways of inserting a style sheet:
External style sheet
Internal style sheet
Inline styles
8
SETTING UP STYLE SHEETS: 3 APPROACHES
Style Type Placement Affected How?
As part of an HTML tag
Inline The specific tag the style is
placed in between the <body>
aka: Local Style attached to in that occurrence.
tags
10
INLINE STYLE SHEET
An inline style can be used if a unique style is to be applied to one
single occurrence of an element.
To use inline styles, use the style attribute in the relevant tag. The
style attribute can contain any CSS property.
The example shows how to change the text color and left margin
of a paragraph:
12
INLINE STYLE SHEET –
EXERCISE 1
13
INLINE STYLE SHEET –
EXERCISE 1
The Styles
14
INTERNAL (DOCUMENT-LEVEL) STYLE SHEET
15
INTERNAL (DOCUMENT-LEVEL) STYLE SHEET
An internal style sheet can be used if one single document has a unique
style.
Internal styles are defined in the <head> section of an HTML page, by
using the <style> tag, like this:
<head>
<style type="text/css">
body {background-color:yellow}
p {color:blue}
</style> 16
</head>
INTERNAL STYLE SHEET –
EXERCISE 2
17
INTERNAL STYLE SHEET –
EXERCISE 2
18
INTERNAL STYLE SHEET –
EXERCISE 2
19
EXTERNAL STYLE SHEET
20
EXTERNAL STYLE SHEET
An external style sheet is ideal when the style is applied to many pages.
With an external style sheet, you can change the look of an entire Web
site by changing one file.
Each page must link to the style sheet using the <link> tag. The <link>
tag goes inside the <head> section:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" /> 21
</head>
EXTERNAL STYLE SHEET –
EXERCISE 3
22
EXTERNAL STYLE SHEET –
EXERCISE 3
What does the code look like?
23
COMMENTS FOR STYLE SHEETS
24
STYLE SHEET STRATEGIES
25
SELECTORS
26
1. CLASS SELECTOR
The class selector uses the HTML class attribute, and the
class name always begins with a “.”
In the example below, all HTML elements with
class="center" will be center-aligned:
.center {text-align:center;} 28
TYPES OF CLASSES
You can also control the images that are displayed in your site
from a central point with Style Sheets.
To do this, you will use another of the Classification Properties.
32
EXERCISE 4
33
34
EXERCISE 4 - OUTPUT
35
EXERCISE 5 - OUTPUT
36
EXERCISE 5 - CODE
37
EXERCISE
6
Write the
code for
the
output:
38
2. ID SELECTOR
The id selector is used to specify a style for a single, unique element.
The id selector uses the id attribute of the HTML element, and is
defined with a "#".
The style rule below will be applied to the element with id="para1":
#para1
{
text-align:center;
color:red; 39
}
USE OF ID
41
A. BACKGROUND IMAGE
42
A. BACKGROUND IMAGE - CODE
<html> </p>
<head> <br/><br/>
<link href="CSSExample.css" rel="stylesheet"
type="text/css"> <p>
<style type="text/css"> <h1 style= "color:orange; font-family: sans-
sarif">This is an in-line style sheet</h1>
h2 {font-family: sans-serif; color: purple} </p>
body { background-image:url("pattern.jpg");
background-repeat:no-repeat; <br/><br/>
background-position:top;
} <p>
<h2> And finally, an embedded style sheet</h2>
</style> </p>
</head> </body>
</html>
<body>
<p> 43
<b>Now this is an example of an external style sheet</b>
B. COLOR, BORDER AND MARGINS
Putting a gradient color at the background.
Syntax:
background : linear – gradient(direction, color-stop1, color-stop2, ….)
background : linear – gradient(angle, color-stop1, color-stop2)
background : repeating- linear – gradient(red, yellow 10%, green 20%)
Putting a border around text
Inserting a margin and padding
44
Inserting an image
GRADIENT COLOR BACKGROUND
45
GRADIENT COLOR BACKGROUND - CODE
<html> img { size: 100px 100px;
<head> position:absolute;
<link href="CSSExample.css" rel="stylesheet" left:500px;
type="text/css"> top:0px;
<style type="text/css"> z-index:-1;}
</style>
h2 {font-family: sans-serif; color: purple}
body { background-image:url("dancer.png"); </head>
background-repeat:no-repeat;
background-position:top right; <body>
<body>
<p>
<b>Now this is an example of an external style sheet</b>
</p><br/><br/>
<img src="dancer.png">
<p>
<h1 style= "color:orange; font-family: sans-sarif">This is 47
an in-line style sheet</h1></p><br/><br/>
C. MENUS, IMAGES, SPAN AND PARAGRAPHS
49
A MENU CAN BE DONE HORIZONTALLY AS IN THE
EXAMPLE BELOW - CODE
50
C. MENUS, IMAGES, SPAN AND PARAGRAPHS
51
C. MENUS, IMAGES, SPAN AND PARAGRAPHS
52
C. MENUS, IMAGES, SPAN AND PARAGRAPHS
53
C. MENUS, IMAGES, SPAN AND PARAGRAPHS
54
EXERCISE 7
Write the HTML and CSS code for the following:
55
EXERCISE 7 - CSS
56
EXERCISE 7 - HTML
57
PRACTICE QUESTION:
59