Unit 1 - Chapter3 Css
Unit 1 - Chapter3 Css
Chapter 3
declaration
syntax
selector { property: value; property2: value2; } rule
declaration block
selector
em { color: red; }
declaration
syntax
selector { property: value; property2: value2; } rule
declaration block
selector
em { color: red; }
declaration
syntax
selector { property: value; property2: value2; } rule
declaration block
selector
em { color: red; }
declaration
syntax
selector { property: value; property2: value2; } rule
declaration block
selector
em { color: red; }
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
Values
What style value for the properties
Some of these are relative units, in that they are based on the value
of something else, such as the size of a parent element.
Pixel. In CSS2 this is a relative measure, while in CSS3 it is absolute Relative (CSS2)
px (1/96 of an inch).
Absolute (CSS3)
Another rarely used relative measure; this one expresses size in Relative
ch relation to the width of the zero ("0") character of an element’s font.
(CSS3 only)
Stands for root em, which is the font size of the root element. Relative
rem Unlike em, which may be different for each element, the rem is
constant throughout the document. (CSS3 only)
in Inches Absolute
cm Centimeters Absolute
mm Millimeters Absolute
LOCATION OF STYLES
Actually there are three …
Different types of style sheet are :
Author-created style sheets (what we are learning in this presentation).
User style sheets allow the individual user to tell the browser to display
pages using that individual’s own custom style sheet. This option is
available in a browser usually in its accessibility options area.
The browser style sheet defines the default styles the browser uses for
each HTML element.
Style Locations
Author Created CSS style rules can be located in three different locations
• Inline
• Embedded
• External
You can combine all 3!
Inline Styles
- Style rules placed within an HTML element via the style attribute
- An inline style only affects the element it is defined within and will override
any other style definitions for the properties used in the inline style.
Dis advantage : Using inline styles is generally discouraged since they
Advantage : Inline styles can however be handy for quickly testing out a style
change.
Embedded Style Sheet
- Style rules placed within the <style> element inside the <head> element .
- While better than inline styles, using embedded styles is also by and large discouraged.
- Since each HTML document has its own <style> element, it is more difficult to
consistently style multiple documents when using embedded styles.
External Style Sheet
Style rules placed within a external text file with the .css extension
Advantages :
- Maintainability :This is the most common place to locate style rules because
it provides the best maintainability.
- Updations :When changes are made to an external style sheet, all HTML
documents that reference that style sheet will automatically use the updated
version.
- Performance :The browser is able to cache the external style sheet which can
improve the performance of the site
Selectors
Things that make your life easier
- Element Selector
- Group Selector
- Class Selector
- Id Selector
- Attribute Selector
- Pseudo Selector
- Pseudo Element Selector
- Pseudo Class Selector
- Universal Selector
Element Selectors
Selects all instances of a given HTML element
Selector which uses element name to select the 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;
}
Example 2: Here, all <p> elements on the page will be center-aligned, with a red
text color:
p{
text-align: center;
color: red;
}
Grouped Selectors
Selecting multiple things
You can select a group of elements by separating the different element names
with commas.
This is a sensible way to reduce the size and complexity of your CSS files, by
combining multiple identical rules into a single rule.
Reset
Grouped selectors are often used as a way to quickly reset or remove browser defaults.
The goal of doing so is to reduce browser inconsistencies with things such as margins, line
heights, and font sizes.
These reset styles can be placed in their own css file (perhaps called reset.css) and linked to
the page before any other external styles sheets.
Example : Here, all <h1> , <h3>elements on the page will be center-aligned, with a red
text color and with font style with Arial /Helvetica family:
h1,h3 {
text-align: center;
color: red;
font-family : Arial, Helvetica ;
}
Class Selectors
Simultaneously target different HTML elements
<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;
}
Class Selectors
• You can also specify that only specific HTML elements should be
affected by a class. In the example below, only <p> elements with
class="center" will be center-aligned:
• HTML elements can also refer to more than one class. In the
example below, the <p> element will be styled according to
class="center" and to class="large":
If an HTML element has been labeled with an id attribute, then you can
target it for styling by using an id selector, which takes the form:
pound/hash (#) followed by the id name.
<div>
#latestComment {
<p>By Susan on <time>October 1, 2012</time></p>
<p>I love Central Park.</p>
font-style: italic;
</div>
<hr/>
</body>
color: brown;
}
Id versus Class Selectors
How to decide
A pseudo-element selector is a way to select something that does not exist explicitly as an
element in the HTML document tree but which is still a recognizable selectable object.
Example : The following example formats the first line of the text in all <p> elements:
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
Pseudo Selectors
Select something that does not exist explicitly as an element
Example 1 :
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited link */
a:visited {
color: #00FF00;
}
/* mouse over link */
a:hover {
color: #FF00FF;
}
/* selected link */
a:active {
color: #0000FF;
}
Example 2 : The selector for hover pseudo class applied to an element : Any time the mouse
cursor is positioned over an h2 element, the styles defined in the given property -value
/* mouse over h2 */
h2:hover {
color: #FF00FF;
font-style :italic;
}
Example 3:
Contextual Selectors
Select elements based on their ancestors, descendants, or siblings
A contextual selector (in CSS3 also called combinators) allows you to select
elements based on their ancestors, descendants, or siblings.
That is, it selects elements based on their context or their relation to other
elements in the document tree.
div p
A specified element that is
contained somewhere Selects a <p> element that is contained
Descendant within another specified somewhere within a <div> element. That is,
element the <p> can be any descendant, not just a
child.
<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>
Universal Selectors
The * selector selects all elements.
The * selector can also select all elements inside another element.
Syntax:
{
css declarations;
}
Example 1: Select all elements, and set their background color to yellow:
*{
background-color: yellow;
}
Example 2: Select all elements inside <div> elements and set their
background color to yellow:
div * {
background-color: yellow;
}
Section 5 of 7
THE CASCADE: HOW STYLES
INTERACT
Why Conflict Happens
In CSS that is
Because
Cascade Principles
CSS uses the following cascade principles to help it deal with conflicts:
• inheritance,
• specificity,
• location
Inheritance
Cascade Principle #1
Many (but not all) CSS properties affect not only themselves but their descendants
as well.
<meta> <title> <h1> <h2> <p> <img> <h3> <div> <div> <p>
<time> <time>
Inheritance
How to force inheritance
It is possible to tell elements to inherit properties that
are normally not inheritable.
div {
font-weight: bold;
margin: 50px;
border: 1pt solid green;
}
p {
border: inherit;
margin: inherit;
}
<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/>
Inheritance
<html> div {
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>
Specificity
Cascade Principle #2
The way that specificity works in the browser is that the browser
assigns a weight to each style rule.
When several rules apply, the one with the greatest weight takes
precedence.
Universal and inherited elements have zero priority.
Element Selector, pseudo element
Class/Attribute , pseudoclass
Low to High
Id Selector
Inline elements
!important
Specificity
How it works
Specificity
How it works
Specificity
These color and font-weight
properties are inheritable and thus
potentially applicable to all the child
elements contained within the body.
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
Examples on Specificity
Inline ID Class/ Elemen
elemen Selector Attribute / ts/
HTML Statement ts pseudoclass Pseudo
elemen
t
< tag style = “color:red”> 1 0 0 0
body .example 0 0 1 1
body .example strong 0 0 1 2
div #first 0 1 0 1
div #first .error 0 1 1 1
#footer .twitter a 0 1 1 1
#footer .twitter a:hover 0 1 2 1
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 ?
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>
Location
There’s always an exception
background-attachment Specifies whether the background image scrolls with the document
(default) or remains fixed. Possible values are: fixed, scroll.
background-color Sets the background color of the element.
Specifies the background image (which is generally a jpeg, gif, or png file)
background-image 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-image: url(../images/backgrounds/body-background-tile.gif);
background-repeat: repeat;
300px
body {
background: white url(../images/backgrounds/body-background-tile.gif) no-repeat;
background-position: 300px 50px;
}
Borders
Box Model Property #2
You can put borders around all four sides of an element, or just one, two,
or three of the sides.
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.
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
Margins and Padding : Margin Properties are used to create space around elements,
outside of any defined borders. Padding properties are used to generate space around
an element's content, inside of any defined borders.
p {
padding: 0;
}
p {
border: solid 1pt red;
margin: 30px;
padding: 0;
}
p {
border: solid 1pt red;
margin: 30px;
padding: 30px;
}
Margins
Why they will cause you trouble.
p {
border: solid 1pt red;
margin: 0;
padding: 0;
}
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.
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.
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
p {
background-color: silver;
}
} 100px
p {
background-color: silver;
width: 200px;
height: 100px;
}
Overflow Property
overflow: visible;
overflow: hidden;
overflow: scroll;
overflow: auto;
Sizing Elements
Time to embrace ems and percentages
<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%
Developer Tools
Help is on the way
TEXT STYLING
Text Properties
Two basic types
web font stack, that is, a series of alternate fonts to use in case
the original font choice in not on the user’s computer.
Specifying the Font-Family
Generic
Font-Family
Name
This cursive
This
Decorative and cursive fonts
fantasy vary from system to system;
rarely used as a result.
@font-face
The future is now
Over the past few years, the most recent browser versions have
begun to support the @font-face selector in CSS.
This selector allows you to use a font on your site even if it is not
installed on the end user’s computer.
Due to the on-going popularity of open source font sites such as
Google Web Fonts (http://www.google.com/webfonts) and Font
Squirrel (http://www.fontsquirrel.com/), @font-face seems to have
gained a critical mass of widespread usage.
Font Sizes
Mo control, mo problems
CSS3 now supports a new relative measure, the rem (for root em unit).
This unit is always relative to the size of the root element (i.e., the
<html> element).
However, since Internet Explorer prior to version 9 do not support the
rem units, you need to provide some type of fallback for those
browsers.
The rem unit
/* using 16px scale */