Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
selector {
property: value;
property: value;
...
property: value;
}
p {
font-family: sans-serif;
color: red;
}
* selects all elements
<link>
<head> ... <link href="filename" type="text/css" rel="stylesheet" /> ... </head>
<link href="style.css" type="text/css" rel="stylesheet" />
link tag says “import and use this file”
rel="stylesheet" says “use it as a stylesheet”.css file (preferred)| property | description |
|---|---|
text-align
|
alignment of text within its element |
text-decoration
|
decorations such as underlining |
text-indent
|
indents the first letter of each paragraph |
text-shadow
|
a colored shadow near an existing piece of text
|
line-height, word-spacing, letter-spacing
|
gaps between the various portions of the text |
| Complete list of text properties | |
text-align
blockquote { text-align: justify; }
h2 { text-align: center; }
[TO LUKE SKYWALKER] The alliance... will die. As will your friends. Good, I can feel your anger. I am unarmed. Take your weapon. Strike me down with all of your hatred and your journey towards the dark side will be complete.
left, right, center, or justify (which widens all full lines of the element so that they occupy its entire width) text-decoration
p {
text-decoration: underline;
}
This paragraph uses the style above.
overline, line-through, blink, or nonetext-decoration: overline underline;
text-shadow
p {
font-weight: bold;
text-shadow: -2px 5px gray;
}
This paragraph uses the style above.
list-style-type property
ol { list-style-type: lower-roman; }
none : No markerdisc (default), circle, squaredecimal : 1, 2, 3, etc.decimal-leading-zero : 01, 02, 03, etc.lower-roman : i, ii, iii, iv, v, etc.upper-roman : I, II, III, IV, V, etc.lower-alpha : a, b, c, d, e, etc.upper-alpha : A, B, C, D, E, etc.lower-greek : alpha, beta, gamma, etc.hebrew, armenian, georgian, cjk-ideographic, hiragana, katakana, hiragana-iroha, katakana-iroha| property | description |
|---|---|
background-color
|
color to fill background |
background-image
|
image to place in background |
background-position
|
placement of bg image within element |
background-repeat
|
whether/how bg image should be repeated |
background-attachment
|
whether bg image scrolls with page |
background-size
|
how large the background appears behind the element |
background
|
shorthand to set all background properties |
background-image
body {
background-image: url("images/draft.jpg");
}
This is the first paragraph
This is the second paragraph...
It occupies 2 lines
background-repeat
body {
background-image: url("images/draft.jpg");
background-repeat: repeat-x;
}
This is the first paragraph
This is the second paragraph...
It occupies 2 lines
repeat (default), repeat-x, repeat-y, or no-repeatbackground-position
body {
background-image: url("images/draft.jpg");
background-repeat: no-repeat;
background-position: 370px 20px;
}
This is the first paragraph
This is the second paragraph...
It occupies 2 lines
top, left, right, bottom, center, a percentage, or a length value in px, pt, etc.
body {
font-size: 16px;
}
body element
body { color: green; }
p, h1, h2 { color: blue; font-style: italic; }
h2 { color: red; background-color: yellow; }
This paragraph uses the first style above.
<style> (BAD!)
<head>
<style type="text/css">
p { font-family: sans-serif; color: red; }
h2 { background-color: yellow; }
</style>
</head>
head of an HTML pagestyle attribute (BAD!)
<p style="font-family: sans-serif; color: red;"> This is a paragraph</p>
.css files<link> tag)<style> tag in the page header)style attribute of an HTML element)
body { font-family: sans-serif; background-color: yellow; }
p { color: red; background-color: aqua; }
a { text-decoration: overline underline; }
h2 { font-weight: bold; text-align: center; }
A styled paragraph. Previous slides are available on the web site.
a:link { color: #FF0000; } /* unvisited link */
a:visited { color: #00FF00; } /* visited link */
a:hover { color: #FF00FF; } /* mouse over link */
| class | description |
|---|---|
:active
|
an activated or selected element |
:focus
|
an element that has the keyboard focus |
:hover
|
an element that has the mouse over it |
:link
|
a link that has not been visited |
:visited
|
a link that has already been visited |
:first-letter
|
the first letter of text inside an element |
:first-line
|
the first line of text inside an element |
:first-child
|
an element that is the first one to appear inside another |
:nth-child(N)
|
applies to every Nth child of a given parent |
id attribute
<p>Spatula City! Spatula City!</p> <p id="mission">Our mission is to provide the most spectacular spatulas and splurge on our specials until our customers <q>esplode</q> with splendor!</p>
<p>Visit <a href= "http://www.textpad.com/download/index.html#downloads"> textpad.com</a> to get the TextPad editor.</p> <p><a href="#mission">View our Mission Statement</a></p>
#
#mission {
font-style: italic;
font-family: "Garamond", "Century Gothic", serif;
}
Spatula City! Spatula City!
Our mission is to provide the most
spectacular spatulas and splurge on our specials until our
customers esplode
with splendor!
missionp#mission {
class attribute
<p class="shout">Spatula City! Spatula City!</p> <p class="special">See our spectacular spatula specials!</p> <p class="special">Today only: satisfaction guaranteed.</p>
I don't want ALL paragraphs to be yellow, just these three...)
id, a class can be reused as much as you like on the page
.special { /* any element with class="special" */
background-color: yellow;
font-weight: bold;
}
p.shout { /* only p elements with class="shout" */
color: red;
font-family: cursive;
}
Spatula City! Spatula City!
See our spectacular spatula specials!
Today only: satisfaction guaranteed.
special, or a p with class shout<h2 class="shout">Spatula City! Spatula City!</h2> <p class="special">See our spectacular spatula specials!</p> <p class="special shout">Satisfaction guaranteed.</p> <p class="shout">We'll beat any advertised price!</p>
See our spectacular spatula specials!
Satisfaction guaranteed.
We'll beat any advertised price!
.special {
background-color: yellow;
font-weight: bold;
}
.shout {
color: red;
font-family: cursive;
}
<div>
a section or division of your HTML page (block)
<div class="shout"> <h2>Spatula City! Spatula City!</h2> <p class="special">See our spectacular spatula specials!</p> <p>We'll beat any advertised price!</p> </div>
See our spectacular spatula specials!
We'll beat any advertised price!
<span>
an inline element used purely as a range for applying styles
<h2>Spatula City! Spatula City!</h2> <p>See our <span class="special">spectacular</span> spatula specials!</p> <p>We'll beat <span class="shout">any advertised price</span>!</p>
See our spectacular spatula specials!
We'll beat any advertised price!
span| element | description |
|---|---|
header, footer
|
top/bottom portion of the page |
nav
|
a navigation area |
heading
|
a compound heading (i.e., with more than one h1–h6 or other heading-like stuff) |
aside
|
a “tangentially related” section of the page (i.e., for sidebars, notes, etc.) |
section
|
a generic section of the page |
article
|
a piece of periodic content (like a blog post or article) |
selector1 selector2 {
properties
}
selector1 > selector2 {
properties
}
<p>Shop at <strong>Hardwick's Hardware</strong>...</p> <ul> <li>The <strong>best</strong> prices in town!</li> <li>Act while supplies last!</li> </ul>
li strong { text-decoration: underline; }
<div id="ad"> <p>Shop at <strong>Hardwick's Hardware</strong>...</p> <ul> <li class="important">The <strong>best</strong> prices!</li> <li>Act <strong>while supplies last!</strong></li> </ul> </div>
#ad li.important strong { text-decoration: underline; }