Chapter4 CSS
Chapter4 CSS
CSS
Chapter 4
1
What Is CSS?
Cascading Style Sheets (CSS)
2
What Is CSS?
Benefits of CSS
• Improved accessibility.
3
What Is CSS?
CSS Versions
4
What Is CSS?
Browser Adoption
5
CSS Syntax
Overview
6
Benefits of CSS
Why using CSS is a better way of describing presentation than HTML
7
CSS Syntax
Rules, properties, values, declarations
syntax
selector { property: value; property2: value2; } rule
declaration block
selector
em { color: red; }
8
Declaration Blocks
• Each declaration is
declaration block
terminated with a
semicolon. selector
em { color: red; }
9
Properties
Which style properties of the selected elements
selector
em { color: red; }
10
Properties
Common CSS properties
Property Type Property
Fonts font
font-family
font-size
font-style
font-weight
@font-face
Text letter-spacing
line-height
text-align
text-decoration
text-indent
Color and background background
background-color
background-image
background-position
background-repeat
color
Borders border
border-color
border-width
border-style
border-top
border-top-color
border-top-width
etc
11
Properties
Common CSS properties continued.
Property Type Property
Spacing padding
padding-bottom, padding-left, padding-right, padding-top
margin
margin-bottom, margin-left, margin-right, margin-top
Sizing height
max-height
max-width
min-height
min-width
width
Layout bottom, left, right, top
clear
display
float
overflow
position
visibility
z-index
Lists list-style
list-style-image
list-style-type
12
Values
What style value for the properties
13
Color Values
CSS supports a variety of different ways of describing color
Method Description Example
Name Use one of 17 standard color names. CSS3 color: red;
has 140 standard names. color: hotpink; /* CSS3 only */
RGB Uses three different numbers between 0 color: rgb(255,0,0);
and 255 to describe the Red, Green, and color: rgb(255,105,180);
Blue values for the color.
Hexadecimal Uses a six-digit hexadecimal number to color: #FF0000;
describe the red, green, and blue value of color: #FF69B4;
the color; each of the three RGB values is
between 0 and FF (which is 255 in
decimal). Notice that the hexadecimal
number is preceded by a hash or pound
symbol (#).
RGBa Allows you to add an alpha, or color: rgb(255,0,0, 0.5);
transparency, value. This allows a
background color or image to “show
through” the color. Transparency is a value
between 0.0 (fully transparent) and 1.0
(fully opaque).
HSL Allows you to specify a color using Hue color: hsl(0,100%,100%);
Saturation and Light values. This is color: hsl(330,59%,100%);
available only in CSS3. HSLA is also
available as well.
14
Units of Measurement
There are multiple ways of specifying a unit of measurement in CSS
15
Relative Units
Unit Description Type
px Pixel. In CSS2 this is a relative measure, while in CSS3 it is Relative (CSS2)
absolute (1/96 of an inch).
Absolute (CSS3)
em Equal to the computed value of the font-size property of Relative
the element on which it is used. When used for font sizes,
the em unit is in relation to the font size of the parent.
% A measure that is always relative to another value. The Relative
precise meaning of % varies depending upon which
property it is being used.
ex A rarely used relative measure that expresses size in Relative
relation to the x-height of an element’s font.
ch Another rarely used relative measure; this one expresses Relative
size in relation to the width of the zero ("0") character of
an element’s font. (CSS3 only)
rem Stands for root em, which is the font size of the root Relative
element. Unlike em, which may be different for each
element, the rem is constant throughout the document. (CSS3 only)
vw, vh Stands for viewport width and viewport height. Both are Relative
percentage values (between 0 and 100) of the viewport
(browser window). This allows an item to change size (CSS3 only)
when the viewport is resized.
16
Absolute Units
Unit Description Type
in Inches Absolute
cm Centimeters Absolute
mm Millimeters Absolute
pt Points (equal to 1/72 of an inch) Absolute
pc Pica (equal to 1/6 of an inch) Absolute
17
Comments in CSS
18
Actually there are three …
Different types of style sheet
19
Style Locations
Author Created CSS style rules can be located in three different locations
20
Inline Styles
Style rules placed within an HTML element via the style attribute
21
Embedded Style Sheet
Style rules placed within the <style> element inside the <head> element
22
External Style Sheet
Style rules placed within a external text file with the .css extension
23
Selectors
Things that make your life easier
24
Element Selectors
Selects all instances of a given HTML element
declaration
declaration block
selector
em { color: red; }
property value
p {
margin: 5px 0 10px 0;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
25
Grouped Selectors
Selecting multiple things
26
Reset
27
Class Selectors
Simultaneously target different HTML elements
28
Class Selectors
<head>
<title>Share Your Travels </title>
<style>
.first {
font-style: italic;
color: brown;
}
</style>
</head>
<body>
<h1 class="first">Reviews</h1>
<div>
<p class="first">By Ricardo on <time>September 15, 2012</time></p>
<p>Easy on the HDR buddy.</p>
</div>
<hr/>
<div>
<p class="first">By Susan on <time>October 1, 2012</time></p>
<p>I love Central Park.</p>
</div>
<hr/>
</body>
.first {
font-style: italic;
color: brown;
}
29
Id Selectors
Target a specific element by its id attribute
30
Id Selectors
<head lang="en">
<meta charset="utf-8">
<title>Share Your Travels -- New York - Central Park</title>
<style>
#latestComment {
font-style: italic;
color: brown;
}
</style>
</head>
<body>
<h1>Reviews</h1>
<div id="latestComment">
<p>By Ricardo on <time>September 15, 2012</time></p>
<p>Easy on the HDR buddy.</p>
</div>
<hr/>
<div>
<p>By Susan on <time>October 1, 2012</time></p>
<p>I love Central Park.</p>
</div>
<hr/>
</body>
#latestComment {
font-style: italic;
color: brown;
}
31
Id versus Class Selectors
How to decide
32
Attribute Selectors
Selecting via presence of element attribute or by the value of an attribute
33
Attribute Selectors
[title] {
cursor: help;
<head lang="en"> padding-bottom: 3px;
<meta charset="utf-8"> border-bottom: 2px dotted blue;
<title>Share Your Travels</title> text-decoration: none;
<style> }
[title] {
cursor: help;
padding-bottom: 3px;
border-bottom: 2px dotted blue;
text-decoration: none;
}
</style>
</head>
<body>
<div>
<img src="images/flags/CA.png" title="Canada Flag" />
<h2><a href="countries.php?id=CA" title="see posts from Canada">
Canada</a>
</h2>
<p>Canada is a North American country consisting of … </p>
<div>
<img src="images/square/6114907897.jpg" title="At top of Sulpher Mountain">
<img src="images/square/6592317633.jpg" title="Grace Presbyterian Church">
<img src="images/square/6592914823.jpg" title="Calgary Downtown">
</div>
</div>
</body>
34
Pseudo Selectors
Select something that does not exist explicitly as an element
35
Pseudo Selectors
36
Contextual Selectors
Select elements based on their ancestors, descendants, or siblings
37
Contextual
Selector SelectorsExample
Matches
Descendant A specified element that is div p
contained somewhere within
Selects a <p> element that is contained somewhere
another specified element
within a <div> element. That is, the <p> can be any
descendant, not just a child.
Child A specified element that is a div>h2
direct child of the specified
Selects an <h2> element that is a child of a <div>
element
element.
Adjacent A specified element that is the h3+p
Sibling next sibling (i.e., comes directly
Selects the first <p> after any <h3>.
after) of the specified element.
General Sibling A specified element that shares h3~p
the same parent as the specified
Selects all the <p> elements that share the same parent
element.
as the <h3>.
38
Descendant Selector
Selects all elements that are contained within another element
39
Contextual Selectors in Action <body>
<nav>
<ul>
<li><a href="#">Canada</a></li>
<li><a href="#">Germany</a></li>
ul a:link { color: blue; } <li><a href="#">United States</a></li>
#main time { color: red; }
</ul>
</nav>
<div id="main">
Comments as of <time>November 15, 2012</time>
<div>
#main>time { color: purple; } <p>By Ricardo on <time>September 15, 2012</time></p>
<p>Easy on the HDR buddy.</p>
</div>
<hr/>
<div>
#main div p:first-child { <p>By Susan on <time>October 1, 2012</time></p>
color: green; <p>I love Central Park.</p>
} </div>
<hr/>
</div>
<footer>
<ul>
<li><a href="#">Home</a> | </li>
<li><a href="#">Browse</a> | </li>
</ul>
</footer>
</body>
40
Why Conflict Happens
In CSS that is
Because
• there are three different types of style sheets
(author-created, user-defined, and the default
browser style sheet),
• author-created stylesheets can define multiple rules
for the same HTML element,
CSS has a system to help the browser determine how
to display elements when different style rules conflict.
41
Cascade
How conflicting rules are handled in CSS
42
Cascade Principles
43
Inheritance
Cascade Principle #1
44
Inheritance body {
font-family: Arial; inherited
<html> color: red; inherited
border: 8pt solid green; not inherited
margin: 100px; not inherited
}
<head> <body>
<meta> <title> <h1> <h2> <p> <img> <h3> <div> <div> <p>
<time> <time>
45
Inheritance
How to force inheritance
<h3>Reviews</h3>
<div>
<p>By Ricardo on <time>September 15, 2012</time></p>
<p>Easy on the HDR buddy.</p>
</div>
<hr/>
<div>
<p>By Susan on <time>October 1, 2012</time></p>
<p>I love Central Park.</p>
</div>
<hr/>
46
Inheritance div {
<html>
font-weight: bold; inherited
margin: 50px; not inherited
border: 1pt solid green; not inherited
<head> <body> }
<meta> <title> <h1> <h2> <p> <img> <h3> <div> <div> <p>
<time> <time>
47
Specificity
Cascade Principle #2
48
Specificity
How it works
49
Specificity
These color and font-weight
body { This text is not within a p element.
properties are inheritable and thus font-weight: bold; <p>Reviews</p>
potentially applicable to all the child color: red; <div>
elements contained within the body. } <p>By Ricardo on <time>September 15, 2012</time
<p>Easy on the HDR buddy.</p>
However, because the <div> and <p> div { This text is not within a p element.
elements also have the same font-weight: normal; </div>
properties set, they override the value color: magenta; <hr/>
}
defined for the <body> element
<div>
because their selectors (div and p) are p { <p>By Susan on <time>October 1, 2012</time></p>
more specific. color: green; <p>I love Central Park.</p>
} </div>
Class selectors are more specific <hr/>
.last {
than element selectors, and thus color: blue; <div>
take precedence and override } <p class="last">By Dave on <time>October 15, 20
element selectors. <p class="last" id="verylast">Thanks for postin
#verylast { </div>
color: orange; <hr/>
font-size: 16pt;
}
Id selectors are more specific than
class selectors, and thus take
precedence and override class
selectors.
50
Specificity Algorithm
The algorithm that is used to determine specificity is :
51
Specificity Algorithm
element selector
div {
color: green;
Specificity Value
0001
}
1 div form {
descendant selector overrides color: orange; 0002
}
(elements only)
2 .example {
overrides
class and attribute color: blue; 0010
selectors }
3
#firstExample {
id selector overrides 0100
color: magenta;
}
4
overrides id + div #firstExample {
additional color: grey; 0101
}
selectors
A higher specificity value
overrides lower specificity
values
inline style 5
overrides <div style="color: red;"> 1000
attribute
52
Location
Cascade Principle #3
53
Location
Browser’s
default style
settings
user-styles.css #example {
1 color: green;
overrides }
2 <head>
overrides <link rel="stylesheet" href="stylesA.css" /> 3
<link rel="stylesheet" href="stylesWW.css" /> overrides
4 <style>
overrides
#example {
color: orange; 5 #example {
overrides color: blue;
color: magenta; }
}
</style>
</head>
<body> 6 overrides
<p id="example" style="color: red;">
sample text
</p>
</body>
Can you guess what will be the color of the sample text ?
54
Location
What color would the sample text be if there wasn’t an inline style definition?
Browser’s
default style
settings
user-styles.css #example {
1 color: green;
overrides }
2 <head>
overrides <link rel="stylesheet" href="stylesA.css" /> 3
<link rel="stylesheet" href="stylesWW.css" /> overrides
4 <style>
overrides
#example {
color: orange; 5 #example {
overrides color: blue;
color: magenta; }
}
</style>
</head>
<body> 6 overrides
<p id="example" style="color: red;">
sample text
</p>
</body>
55
Location
There’s always an exception
56
The BOX MODEL
Section 6 of 7
57
The Box Model
Time to think inside the box
58
The Box Model
margin
border
padding
59
Background
Box Model Property #1
60
Background
Property Properties
background
Description
A combined short-hand property that allows you to set the background values
in one property. While you can omit properties with the short-hand, do
remember that any omitted properties will be set to their default value.
background-attachment Specifies whether the background image scrolls with the document (default) or
remains fixed. Possible values are: fixed, scroll.
background-image Specifies the background image (which is generally a jpeg, gif, or png file) for
the element. Note that the URL is relative to the CSS file and not the HTML.
CSS3 introduced the ability to specify multiple background images.
background-position Specifies where on the element the background image will be placed. Some
possible values include: bottom, center, left, and right. You can also supply a
pixel or percentage numeric position value as well. When supplying a numeric
value, you must supply a horizontal/vertical pair; this value indicates its
distance from the top left corner of the element.
background-repeat Determines whether the background image will be repeated. This is a common
technique for creating a tiled background (it is in fact the default behavior).
Possible values are: repeat, repeat-x, repeat-y, and no-repeat.
background-size New to CSS3, this property lets you modify the size of the background image.
61
Background Repeat
background-image: url(../images/backgrounds/body-background-tile.gif);
background-repeat: repeat;
62
Background Position
50px
300px
body {
background: white url(../images/backgrounds/body-background-tile.gif) no-repeat;
background-position: 300px 50px;
}
63
Borders
Box Model Property #2
64
Borders
Property Description
border A combined short-hand property that allows you to set the style,
width, and color of a border in one property. The order is important
and must be:
border-style border-width border-color
border-style Specifies the line type of the border. Possible values are: solid,
dotted, dashed, double, groove, ridge, inset, and outset.
border-width The width of the border in a unit (but not percents). A variety of
keywords (thin, medium, etc) are also supported.
65
Shortcut notation
TRBL
border-color: red green orange blue; /* sets all four sides differently */
When using this multiple values shortcut, they are applied in clockwise order starting at the top.
Thus the order is: top right bottom left.
TRBL (Trouble)
top
border-color: top right bottom left;
left right
border-color: red green orange blue;
bottom
66
Margins and Padding
Box Model Properties #3 and #4
p {
border: solid 1pt red;
margin: 0;
padding: 0;
}
p {
border: solid 1pt red;
margin: 30px;
padding: 0;
}
p {
border: solid 1pt red;
margin: 30px;
padding: 30px;
}
67
Margins
Why they will cause you trouble.
p {
border: solid 1pt red;
margin: 0;
padding: 0;
}
68
Collapsing Margins <div>
<p>Every CSS rule ...</p>
1 90px <p>Every CSS rule ...</p>
</div>
50px <div>
<p>In CSS, the adjoining ... </p>
50px 4 <p>In CSS, the adjoining ... </p>
</div>
50px
div {
2 90px border: dotted 1pt green;
padding: 0;
50px margin: 90px 20px;
}
50px 5
p {
50px border: solid 1pt red;
padding: 0;
3 90px margin: 50px 20px;
}
If overlapping margins did not collapse, then margin space for
would be 180p (90pixels for the bottom margin of the first <div> +
90 pixels for the top margin of the second <div>), while the margins
and for would be 100px.
69
Collapsing Margins
How it works
70
Width and Height
Box Model Properties #5 and #6
71
Width and Height
Potential Problem #1
72
Width and Height
Potential Problem #2
Since the width and the height refer to the size of the
content area, by default, the total size of an element is
equal to not only its content area, but also to the sum
of its padding, borders, and margins.
73
div {
box-sizing: content-box;
width: 200px;
height: 100px;
padding: 5px;
margin: 10px;
border: solid 2pt black; True element width = 10 + 2 + 5 + 200 + 5 + 2 + 10 = 234 px
} True element height = 10 + 2 + 5 + 100 + 5 + 2 + 10 = 134 px
div {
...
box-sizing: border-box; True element width = 10 + 200 + 10 = 220 px
}
True element height = 10 + 100 + 10 = 120 px
74
Width and Height
p {
background-color: silver;
}
} 100px
p {
background-color: silver;
width: 200px;
height: 100px;
}
75
Overflow Property
overflow: visible;
overflow: hidden;
overflow: scroll;
overflow: auto;
76
Sizing Elements
Time to embrace ems and percentages
77
<body>
<div class="pixels">
Pixels - 200px by 50 px
</div>
<style> 50% <div class="percent">
html,body { Percent - 50% of width and height
margin:0; </div>
50% 50%
width:100%; </body>
height:100%;
background: silver;
}
.pixels {
width:200px;
height:50px;
background: teal;
50%
}
.percent {
width:50%;
50% 50%
height:50%;
background: olive;
}
<body>
.parentFixed { <div class="parentFixed">
width:400px; <strong>parent has fixed size</strong>
height:150px; <div class="percent">
background: beige; PERCENT - 50% of width and height
} </div>
50% of parent (= 200px)
.parentRelative { </div>
width:50%; <div class="parentRelative">
height:50%; <strong>parent has relative size</strong>
background: yellow; <div class="percent">
} PERCENT - 50% of width and height
</style> </div>
</div>
50% 50% </body>
50% of parent
50% 50%
78
Developer Tools
Help is on the way
79
Developer Tools
Chrome – Inspect Element Firefox – Inspect
80
TEXT STYLING
Section 7 of 7
81
Text Properties
Two basic types
82
Font-Family
A few issues here
83
Specifying the Font-Family
1 Use this font as
the first choice
If it isn’t available, then
3 use this one
84
Generic Font-Family
Generic
Font-Family
Name
This cursive
This
Decorative and cursive fonts
fantasy vary from system to system;
rarely used as a result.
85
@font-face
The future is now
86
Font Sizes
Mo control, mo problems
87
Font Sizes
Welcome ems and percents again
88
How to use ems and percents
89
How to use ems
<body>
and percents
Browser’s default text size is usually 16 pixels
<p> 100% or 1em is 16 pixels
<h3> 125% or 1.125em is 18 pixels
<h2>
150% or 1.5em is 24 pixels
<h1>
200% or 2em is 32 pixels
/* using 16px scale */ <body>
<p>this will be about 16 pixels</p>
body { font-size: 100%; } <h1>this will be about 32 pixels</h1>
h3 { font-size: 1.125em; } /* 1.25 x 16 = 18 */ <h2>this will be about 24 pixels</h2>
h2 { font-size: 1.5em; } /* 1.5 x 16 = 24 */ <h3>this will be about 18 pixels</h3>
h1 { font-size: 2em; } /* 2 x 16 = 32 */ <p>this will be about 16 pixels</p>
</body>
90
How to use ems and percents
It might seem easy … but it’s not …
91
ems and percents
<body>
<p>this is 16 pixels</p>
<h1>this is 32 pixels</h1>
<article>
<h1>this is 32 pixels</h1>
<p>this is 16 pixels</p>
<div>
<h1>this is 32 pixels</h1>
<p>this is 16 pixels</p>
</div>
</article>
</body>
92
The rem unit
Solution to font sizing hassles?
93
The rem unit /* using 16px scale */
94