Cascading Style Sheets (CSS)
Cascading Style Sheets (CSS)
INTRO TO CSS
styles
To describe the presentation a document written in a
Cascading
other markup
WHY CSS?
Separate Content from Form
Content is the text and images, marked up to define regions of
specific types
Form defines the “style” for the content
<font size=“14px”>
My First Header
</font>
<font size=“12px” color=“red” face=“Verdana”>
My information 1 goes here.
The old way: </font>
<font size=“14px”>
My Second Header
</font>
<font size=“12px” color=“red” face=“Verdana”>
Different information goes here.
</font>
WHY CSS? CONTINUED.
Separate Content from Form
Content
Form or Style
.header { font-size:14px;}
.info { font-family: verdana;
font-color: blue;
font-size: 12px; }
WHAT DOES THIS SEPARATION GET US?
Separate Content from Form
Specify the style once for every instance of that class.
Example: Specify the font once for all text on the HTML page that
you’ve identified as a “header”.
Can change the style for your entire site by editing only
ONE FILE.
CSS SKINNING
“Skinning” - changing the look of a page or your site
Selecting an appearance by choosing which stylesheet to use.
<p>Content</p>
The Class Attribute – precede the class with a period
.myinfo { font-size: 10px;
font-color: White; }
<p class=“myinfo”>Content</p>
<div class=“myinfo”>Other content</div>
CASCADING INHERITANCE
Nested elements inherit
the properties from the
its parent
14
SHORTHAND FONT PROPERTY
font
Shorthand rule for setting multiple font properties at the
same time
font:italic normal bold 12px/16px verdana
font-style: italic;
font-variant: normal;
font-weight: bold;
font-size: 12px;
line-height: 16px;
font-family: verdana;
15
BACKGROUNDS
background-image
URL of image to be used as background, e.g.:
background-image:url("back.gif");
background-color
Using color and image and the same time
background-repeat
repeat-x, repeat-y, repeat, no-repeat
background-attachment
fixed / scroll
16
BACKGROUNDS
background-position: specifies vertical and
horizontal position of the background image
Vertical position: top, center, bottom
Horizontal position: left, center, right
Both can be specified in percentage or other numerical
values
Examples:
17
BACKGROUND SHORTHAND PROPERTY
background:shorthand rule for setting
background properties at the same time:
background: #FFF0C0 url("back.gif") no-repeat
fixed top;
is equal to writing:
background-color: #FFF0C0;
background-image: url("back.gif");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: top;
Some browsers will not apply BOTH color and
image for background if using shorthand rule
18
BACKGROUND-IMAGE OR <IMG>?
Background images allow you to save many image
tags from the HTML
Leads to less code
More content-oriented approach
All images that are not part of the page content (and
are used only for "beautification") should be moved to
the CSS
19
BORDERS
border-width: thin, medium, thick or numerical
value (e.g. 10px)
border-color: color alias or RGB value
20
BORDER SHORTHAND PROPERTY
22
MARGIN AND PADDING
margin and padding define the spacing around the
element
Numerical value, e.g. 10px or -5px
Can be defined for each of the four sides separately -
margin-top, padding-left, …
margin is the spacing outside of the border
padding is the spacing between the border and the
content
What are collapsing margins?
23
MARGIN AND PADDING: SHORT
RULES
margin: 5px;
Sets all four sides to have margin of 5 px;
24