Web Technology Introduction: Cascading Style Sheets
Web Technology Introduction: Cascading Style Sheets
Outlines
Introduction
First example
Introduction
CSS: Cascading Style Sheets
CSS is a style language for defining layout of
XHTML documents
Separation of structure from presentation
CSS covers fonts, colors, margins, lines, height,
width, background images, advanced positions
and many other things
Introduction
CSS works by associating rules with elements
in documents
Each rule has two parts
Selector: indicates which elements the declaration
applies to
Declaration: indicates how elements should be
styled
Inline Styles
Declare an individual elements format
Attribute style
CSS property
Followed by a colon and a value
Property font-family
Specifies the name of the font to use
Property font-size
Specifies a 14-point font
10
Conflicting Styles
authors style > users style > browsers style
Children inherit parents style
If a style is specified for the child => used
instead of parents style
11
Cascading
The order
inline
embedded
external
12
Type of Selectors
Class selector
ID selector
Type selector
13
Class Selector
<p class="highlight">This paragraph has red text.</p>
<p class="default">This paragraph has dark gray text.</p>
<p class="default">This paragraph also has dark gray text.</p>
/* Define highlight class */
.highlight {
color:#F00;
}
/* Define default class */
.default {
color:#333;
}
14
ID Selector
<p id="highlight">This paragraph has red text.</p>
<p id="default">This paragraph has dark gray text.</p>
/* Define highlighted text */
#highlight {
color:#F00;
}
/* Define default text */
#default {
color:#333;
}
15
Type Selector
<p>This paragraph has red text.</p>
<p>This paragraph has red text.</p>
/* Define red text */
p{
color:#F00;
}
16
HTML
CSS
CSS
CSS
CSS
CSS
@import url("default.css");
@import url("layout.css");
@import url("navigation.css");
@import url("forms.css");
Grouping
/* Heading styles */
h1 {
font-family:Helvetica,Arial,sans-serif;
line-height:140%;
color:#333;
}
h2 {
font-family:Helvetica,Arial,sans-serif;
line-height:140%;
color:#333;
}
h3 {
font-family:Helvetica,Arial,sans-serif;
line-height:140%;
color:#333;
}
/* Heading styles */
h1, h2, h3 {
font-family:Helvetica,Arial,sansserif;
line-height:140%;
color:#333;
}
19
Grouping
/* Heading styles */
h1, h2, h3 {
font-family:Helvetica,Arial,sans-serif;
line-height:140%;
color:#333;
}
/* Additionally, render all h1 headings in italics */
h1 {
font-style:italic;
}
20
Inheritance
/* Top-level heading */
h1 {
color:#333;
}
21
Contextual Selectors
/* Top-level heading */
h1 {
color:#333;
}
/* Make emphasized text shine brightly */
em {
color:#F00;
}
CSS Measurements
23
CSS Measurements
24
Colors
Color
Color HEX
Color RGB
#000000
rgb(0,0,0)
#FF0000
rgb(255,0,0)
#00FF00
rgb(0,255,0)
#0000FF
rgb(0,0,255)
#FFFF00
rgb(255,255,0)
#00FFFF
rgb(0,255,255)
#FF00FF
rgb(255,0,255)
#C0C0C0
rgb(192,192,192)
#FFFFFF
rgb(255,255,255)
25
Division
<div> tag divides a page into groups
<div>
<p>This is our content area.</p>
</div>
<div id="container">
<p>This is our content area.</p>
</div>
26
Division
<div id="container">
<p>This is our content area.</p>
</div>
Controlling Fonts
28
Controlling Fonts
The font-family Property
<p class=one>Here is some text.</p>
<p class=two>Here is some text.</p>
<p class=three>Here is some text.</p>
29
Generic Fonts
30
Controlling Fonts
31
Formatting Text
color: color of text
text-align: Specifies the alignment of the text within
its containing element; values = {left, right, center,
justify}
vertical-align: Vertical alignment of text within
containing element and in relation to containing
element; values={baseline, sub, super, top, etc.}
text-decoration: Specifies whether the text should
be underlined, overlined, strikethrough, or blinking
text; values={overline, underline, line-through, blink}
32
Box Model
Every element is treated as a box in CSS
33
Box Model
Property
Description
border
Even if you cannot see it, every box has a border. This
separates the edge of the box from other boxes.
margin
padding
34
Border Properties
border-color
border-style: none, solid, dotted, etc.
border-width
border-top/bottom/left/rightcolor/style/width
35
Borders
border-style: none, dotted, dashed, solid,
double, groove, ridge, inset, and outset
#container {
width: 400px;
margin: 10px auto 10px auto;
padding: 20px;
border-style: dashed dotted solid ridge;
}
36
Padding Properties
Padding is the distance between the edges
(borders) of an (X)HTML element and the
content within it, and can be applied to any
element.
#container {
padding-top: 20px;
padding-left: 10%;
padding-right: 1em;
padding-bottom: 0;
}
37
Margin Properties
The margin property is used to declare the
margin between an (X)HTML element and
those elements outside of it
#container {
margin-top: 20px;
margin-left: auto;
margin-right: auto;
margin-bottom: 1em;
}
#container {
margin: 20px auto 1em auto;
}
38
Dimensions
Property
height
width
line-height
max-height
min-height
max-width
min-width
Purpose
Sets the height of a box
Sets the width of a box
Sets the height of a line of text
Sets a maximum height for a box
Sets the minimum height for a box
Sets the maximum width for a box
Sets the minimum width for a box
39
Purpose
The overflowing content is hidden.
The box is given scrollbars to allow users to
scroll to see the content.
40
Pseudo-class
41
Pseudo-class
42
Background
43
Tables
padding to set the amount of space between the
border of a table cell and its content this
property is very important to make tables easier
to read.
border to set the properties of the border of a
table.
text and font properties to change the
appearance of anything written in the cell.
text-align to align writing to the left, right, or
center of a cell.
44
Tables
vertical-align to align writing to the top,
middle, or bottom of a cell.
width to set the width of a table or cell.
height to set the height of a cell (often used
on a row as well).
background-color to change the background
color of a table or cell.
background-image to add an image to the
background of a table or cell.
45
46