Cascading Style Sheets (CSS) : Irina Mcguire
Cascading Style Sheets (CSS) : Irina Mcguire
December 3, 2010
Introduction
• What do you know about CSS?
• What do you hope to do with CSS?
• How familiar are you with HTML?
commands or rules. }
#header {
These rules tell the height:232px;
HTML how to display. }
#footer {
width: 100%;
padding: 0 10px;
*To create a style sheet, create a file margin-bottom: 10px;
using Notepad (PC) or Text Edit
}
(Mac), save it as a .css document and
start writing the CSS code (see right). And so on….
CSS Benefits
• Separates structure from presentation
• Provides advanced control of presentation
• Easy maintenance of multiple pages
• Faster page loading
• Better accessibility for disabled users
• Easy to learn
HTML Without CSS
“HTML without CSS is like a
piece of candy without a
pretty wrapper.”
Header
Navigation
Content
Footer
Attaching a Style Sheet
Attach a style sheet to a page by adding the code to the <head>
section of the HTML page. There are 3 ways to attach CSS to a page:
declaration
Selectors
body {
*CSS code can be written in a
background: purple; linear format (above) or in a block
color: green; format (below).
}
Grouping Selectors
Group the same selector with different declarations
together on one line.
h1 {color: black;}
h1 {font-weight: bold;}
h1 {background: white;}
Example of grouping selectors (both are correct):
h1 {
color: black;
font-weight: bold;
background: white;
}
Grouping Selectors
Group different selectors with the same declaration on
one line.
h1 {color: yellow;}
h2 {color: yellow;}
h3 {color: yellow;}
header
menu main
footer
Typical Web Page (HTML)
Typical HTML Web page is made up of containers (boxes)
or DIVs. Each DIV is assigned an ID or a Class.
<div id=“container”>
<div id=“header”>Insert Title</div>
<div id=“main">content
<div id=“menu”>content</div>
</div>
<div id=“footer”>content</div>
</div>
Typical Web Page (CSS)
The CSS file uses the same DIV/ID/Class names as the
HTML and uses them to style the elements.
HTML Code:
<h1 id=“mainHeading”>Names</h1>
<p class=“name”>Joe</p>
CSS Code:
#mainHeading {color: green}
.name {color: red}
CSS Box Properties
• Background-color
• Width
• Padding
• Margin
• Border-width
• Border-color
• Border-style
HTML CSS
#content {
background-color: #ccc;
div id=“header”
margin-bottom: 10px;
border: 1px dashed blue;
color: #fff;
width: auto;
}
div id=“content”
div id=“footer”
Common CSS Layout Properties
• Width margin
padding
• Height
• Float
width
• Clear
height
• Border border
• Padding
• Margin
Width & Height
Width and height define the width and height of an element.
div id=“box”
div id=“box3”
div id=“box”
div id=“box”
padding: 10px 10px 10px 10px;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 10px;
padding-top: 10px;
Margin (top, right, bottom, left)
Margin is the space outside the text/content and the border. You can use
margin for all around the element or specify each side of the rectangle
separately.
The code could be any of the following:
• White • #ffffff
• Black • #fff
• • #cccf0f3
Blue
• Fuchsia
• Gray
• Green
• Lime
• Aqua
Styling Links
The links property defines how inactive, hovered,
active, and visited link states appear to the user.
• Background-image
• Background-repeat
• Background-position
• Background-attachment
Layering div id=“bg”
li {
background-image:url(flower.jpg);
background-repeat:no-repeat;
}
• repeat
Possible Values > • repeat-x (horizontal)
• repeat-y (vertical)
• no-repeat
Image Positioning left center
top top
The background-position
property positions the image left center right
using either combined bottom bottom bottom
keywords (top, bottom, left,
right, and center); length
values; or percentage values.
The background-
background-position: right top;
/*can also use number values*/
attachment property
fixes or scrolls an
background-attachment: fixed; / image in the browser
*can also use ‘scroll’*/ window. Values
include fixed and scroll.
The Power of Cascade
When multiple styles or style sheets are used, they start to
cascade and sometimes compete with one another due to CSS’s
inheritance feature. Any tag on the page could potentially be
affected by any of the tags surrounded by it.
Great Book
“CSS: The Missing Manual” - by David Sawyer McFarland
CSS Galleries
http://www.cssbeauty.com/gallery/
www.cssdrive.com
http://www.css-website.com
Thank You