Selected content adapted from material by Marty Stepp, Jessica Miller, and Victoria Kirst © 2012. Used by permission.
<p> <font face="Arial">Welcome to Greasy Joe's.</font> You will <b>never</b>, <i>ever</i>, <u>EVER</u> beat <font size="+4" color="red">OUR</font> prices! </p>
Welcome to Greasy Joe's. You will never, ever, EVER beat OUR prices!
b
, i
, u
, and font
are discouraged in strict HTML
<link>
<head> ... <link href="filename" type="text/css" rel="stylesheet" /> ... </head>
<link href="style.css" type="text/css" rel="stylesheet" />
.css
file (preferred)selector { property: value; property: value; ... property: value; }
p { font-family: sans-serif; color: red; }
*
selects all elements
p { color: red; background-color: yellow; }
This paragraph uses the style above.
property | description |
---|---|
color
|
color of the element's text |
background-color
|
color that will appear behind the element |
p { color: red; } h2 { color: rgb(128, 0, 196); } h4 { color: #FF8800; }
This paragraph uses the first style above.
aqua
, black
, blue
, fuchsia
, gray
, green
, lime
, maroon
, navy
, olive
, purple
, red
, silver
, teal
, white
(white), yellow
property | description |
---|---|
font-family
|
which font will be used |
font-size
|
how large the letters will be drawn |
font-style
|
used to enable/disable italic style |
font-weight
|
used to enable/disable bold style |
Complete list of font properties |
font-family
p { font-family: Georgia; } h2 { font-family: "Courier New"; }
This paragraph uses the first style above.
font-family
p { font-family: Garamond, "Times New Roman", serif; }
This paragraph uses the above style.
font-family
value ensures that every computer will use a valid fontfont-size
p { font-size: 14pt; }
This paragraph uses the style above.
px
) vs. point (pt
) vs. m-size (em
)pt
specifies number of point, where a point is 1/72 of an inch onscreenpx
specifies a number of pixels on the screenem
specifies number of m-widths, where 1 em is equal to the font's current size16px
, 16pt
, 1.16em
xx-small
, x-small
, small
, medium
, large
, x-large
, xx-large
, smaller
, larger
90%
, 120%
font-weight
,
font-style
p { font-weight: bold; font-style: italic; }
This paragraph uses the style above.
normal
to turn them off (e.g. headings)p, h1, h2 { color: green; } h2 { background-color: yellow; }
This paragraph uses the above style.
h2
above)/*
... */
/* This is a comment.
It can span many lines in the CSS file. */
p {
color: red;
background-color: aqua;
}
//
single-line comment style is NOT supported in CSS<!-- ... -->
HTML comment style is also NOT supported in CSS<p> <a href="http://jigsaw.w3.org/css-validator/check/referer"> <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" /></a> </p>
property | description |
---|---|
text-align
|
alignment of text within its element |
text-decoration
|
decorations such as underlining |
line-height , word-spacing , letter-spacing
|
gaps between the various portions of the text |
text-indent
|
indents the first letter of each paragraph |
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 none
text-decoration: overline underline;
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
|
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-repeat
background-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.list-style-type
property
ol { list-style-type: lower-roman; }
none
: No markerdisc
(default), circle
, square
decimal
: 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
table { border: 2px solid black; caption-side: bottom; } tr { font-style: italic; } td { background-color: yellow; text-align: center; width: 30%; }
Column 1 | Column 2 |
---|---|
1,1 | 1,2 okay |
2,1 real wide | 2,2 |
border-collapse
propertytable, td, th { border: 2px solid black; } table { border-collapse: collapse; }
Column 1 | Column 2 |
---|---|
1,1 | 1,2 |
2,1 | 2,2 |
Column 1 | Column 2 |
---|---|
1,1 | 1,2 |
2,1 | 2,2 |
border-collapse
property merges these borders into onerowspan
and colspan
attributes<table> <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr> <tr><td colspan="2">1,1-1,2</td> <td rowspan="3">1,3-3,3</td></tr> <tr><td>2,1</td><td>2,2</td></tr> <tr><td>3,1</td><td>3,2</td></tr> </table>
Column 1 | Column 2 | Column 3 |
---|---|---|
1,1-1,2 | 1,3-3,3 | |
2,1 | 2,2 | |
3,1 | 3,2 |
colspan
makes a cell occupy multiple columns; rowspan
multiple rowstext-align
and vertical-align
control where the text appears within a cell<col>
,
<colgroup>
<table> <col class="urgent" /> <colgroup class="highlight" span="2"></colgroup> <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr> <tr><td>1,1</td><td>1,2</td><td>1,3</td></tr> <tr><td>2,1</td><td>2,2</td><td>2,3</td></tr> </table>
Column 1 | Column 2 | Column 3 |
---|---|---|
1,1 | 1,2 | 1,3 |
2,1 | 2,2 | 2,3 |
col
tag can be used to define styles that apply to an entire column (self-closing)colgroup
tag applies a style to a group of columns (NOT self-closing)table
has semantics; it should be used only to represent actual tabular datadiv
s, widths/margins, floats, etc. to perform layoutnth-child
, inline-block
, :not
, +
body { font-size: 16px; }
body
elementbody { 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 |
Busy Schedule: Given the following HTML code, write CSS to make the following style changes:
<h1>Being Busy</h1> <p>It's not easy being busy! But that doesn't mean it can't be fun.</p> <h2>Important Things To Do</h2> <ol> <li><a href="phys.html">Turn in</a> Physics HW!</li> <li>Do Reading for English</li> <li>Go To the Store for Groceries</li> <li>Pay Tuition</li> <li>Make sure program compiles...</li> </ol> <h2>Fun Things To Do</h2> <ul> <li>Sleep In</li> <li>Out to Dinner with Friends</li> <li>Go Swimming</li> <li>Finish Painting</li> <li>Go to the Movies: <a href="movies.html">see what's playing</a></li> </ul>
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>
Spatula City! Spatula City!
Our mission is to provide the most
spectacular spatulas and splurge on our specials until our
customers esplode
with splendor!
<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>
Visit textpad.com to get the TextPad editor.
#
#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!
mission
p#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>
Spatula City! Spatula City!
See our spectacular spatula specials!
Today only: satisfaction guaranteed.
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
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; }
Shop at Hardwick's Hardware...
<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; }
Shop at Hardwick's Hardware...
h2 { border: 5px solid red; }
property | description |
---|---|
border
|
thickness/style/color of border on all 4 sides |
px
, pt
, em
, or thin
, medium
, thick
)none
,
hidden
,
dotted
,
dashed
,
double
,
groove
,
inset
,
outset
,
ridge
,
solid
)
property | description |
---|---|
border-color ,
border-width , border-style
|
specific properties of border on all 4 sides |
border-bottom ,
border-left , border-right ,
border-top
|
all properties of border on a particular side |
border-bottom-color ,
border-bottom-style , border-bottom-width ,
border-left-color , border-left-style ,
border-left-width , border-right-color ,
border-right-style , border-right-width ,
border-top-color , border-top-style ,
border-top-width
|
properties of border on a particular side |
Complete list of border properties |
h2 { border-left: thick dotted #CC0088; border-bottom-color: rgb(0, 128, 128); border-bottom-style: double; }
border-bottom-width
above)property | description |
---|---|
padding
|
padding on all 4 sides |
padding-bottom
|
padding on bottom side only |
padding-left
|
padding on left side only |
padding-right
|
padding on right side only |
padding-top
|
padding on top side only |
Complete list of padding properties |
p { padding: 20px; border: 3px solid black; } h2 { padding: 0px; background-color: yellow; }
This is the first paragraph
This is the second paragraph
p { padding-left: 200px; padding-top: 30px; background-color: fuchsia; }
This is the first paragraph
This is the second paragraph
padding:10px 5px 15px 20px; |
top padding is 10px right padding is 5px bottom padding is 15px left padding is 20px |
padding:10px 5px 15px; |
top padding is 10px right and left padding are 5px bottom padding is 15px |
padding:10px 5px; |
top and bottom padding are 10px right and left padding are 5px |
padding:10px; | all four paddings are 10px |
property | description |
---|---|
margin
|
margin on all 4 sides |
margin-bottom
|
margin on bottom side only |
margin-left
|
margin on left side only |
margin-right
|
margin on right side only |
margin-top
|
margin on top side only |
Complete list of margin properties |
p { margin: 50px; background-color: fuchsia; }
This is the first paragraph
This is the second paragraph
p { margin-left: 8em; background-color: fuchsia; }
This is the first paragraph
This is the second paragraph
p { width: 350px; background-color: yellow; } h2 { width: 50%; background-color: aqua; }
This paragraph uses the first style above.
property | description |
---|---|
width ,
height
|
how wide or tall to make this element (block elements only) |
max-width ,
max-height , min-width ,
min-height
|
max/min size of this element in given dimension |
auto
marginsp { margin-left: auto; margin-right: auto; width: 750px; }or
p { margin: 0 auto; width: 750px; }
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore...
width
is set (otherwise, may occupy entire width of page)
text-align: center;
float
property
(reference)
property | description |
---|---|
float
|
side to hover on; can be left , right , or none (default)
|
<img src="images/borat.jpg" alt="Borat" class="headericon" /> Borat Sagdiyev (born July 30, 1972) is a ...
img.headericon { float: left; }
div
s from being aligned to floated...
float
ing content and widthI am not floating, no width set
I am floating right, no width set
I am floating right, no width set, but my text is very long so this paragraph doesn't really seem like it's floating at all, darn
I am not floating, 45% width
I am floating right, 45% width
width
property value
width
is specified, other content may be unable to wrap around the floating elementclear
property
p { background-color: fuchsia; } h2 { clear: right; background-color: yellow; }
Homestar Runner is a Flash animated Internet cartoon. It mixes surreal humour with ...
property | description |
---|---|
clear
|
disallows floating elements from overlapping this element; can be left , right , both , or none (default)
|
div#sidebar { float: right; } p { clear: right; }
<p><img src="images/homestar_runner.png" alt="homestar runner" /> Homestar Runner is a Flash animated Internet cartoon. It mixes surreal humour with ....</p>
p { border: 2px dashed black; } img { float: right; }
Homestar Runner is a Flash animated Internet cartoon. It mixes surreal humour with ....
p
containing the image to extend downward so that its border encloses the entire image
overflow
property
p { border: 2px dashed black; overflow: hidden; }
Homestar Runner is a Flash animated Internet cartoon. It mixes surreal humour with ....
property | description |
---|---|
overflow
|
specifies what to do if an element's content is too large; can be auto , visible , hidden , or scroll
|
<div> <p>the first paragraph</p> <p>the second paragraph</p> <p>the third paragraph</p> Some other text that is important </div>
p { float: right; width: 20%; margin: 0.5em; border: 2px solid black; } div { border: 3px dotted green; overflow: hidden; }
the first paragraph
the second paragraph
the third paragraph
Some other text that is importantposition
property
div#ad { position: fixed; right: 10%; top: 15%; }
property | value | description |
---|---|---|
position
|
||
static
|
default position | |
relative
|
offset from its normal static position | |
absolute
|
a fixed position within its containing element | |
fixed
|
a fixed position within the browser window | |
top ,
bottom , left ,
right
|
positions of box's corners |
#menubar { position: absolute; left: 400px; top: 50px; }
absolute
or relative
positioning)top
, bottom
, left
, right
valueswidth
property as well#area2 { position: relative; }
absolute
element in an element whose position
is relative
text-align
vertical-align
vertical-align
propertyproperty | description |
---|---|
vertical-align
|
specifies where an inline element should be aligned vertically, with respect to other content on the same line within its block element's box |
top
, middle
, bottom
, baseline
(default), sub
, super
, text-top
, text-bottom
, or a length value or %
baseline
means aligned with bottom of non-hanging lettersvertical-align
example<p style="background-color: yellow;"> <span style="vertical-align: top; border: 1px solid red;"> Don't be sad! Turn that frown <img src="images/sad.jpg" alt="sad" /> upside down! <img style="vertical-align: bottom" src="images/smiley.jpg" alt="smile" /> Smiling burns calories, you know. <img style="vertical-align: middle" src="images/puppy.jpg" alt="puppy" /> Anyway, look at this cute puppy; isn't he adorable! So cheer up, and have a nice day. The End. </span></p>
Don't be sad! Turn that frown
upside down!
Smiling burns calories, you know.
Anyway, look at this cute puppy; isn't he adorable! So cheer up,
and have a nice day. The End.
<p style="background-color: red; padding: 0px; margin: 0px"> <img src="images/smiley.png" alt="smile" /> </p>
padding
and margin
of 0vertical-align
to bottom
fixes the problem (so does setting line-height
to 0px
)width
, height
, min-width
, etc.) are ignored for inline elementsmargin-top
and margin-bottom
are ignored, but margin-left
and margin-right
are nottext-align
property controls horizontal position of inline elements within it
vertical-align
property aligns it vertically within its block box
display
property
h2 { display: inline; background-color: yellow; }
property | description |
---|---|
display
|
sets the type of CSS box model an element is displayed with |
none
, inline
, block
, run-in
, compact
, ...<ul id="topmenu"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul>
#topmenu li { display: inline; border: 2px solid gray; margin-right: 1em; }
visibility
propertyp.secret { visibility: hidden; }
Since nobody can see this anyway: Professor Miner is a weirdo!!!
property | description |
---|---|
visibility
|
sets whether an element should be shown onscreen; can be visible (default) or hidden
|
hidden
elements will still take up space onscreen, but will not be shown
display
to none
insteadclass
and id
attributes, rather than through the tag elements themselves
max-width
Given a list of people with their birth and end years (all between 1900 and 2000), find the year with the most number of people alive.
If you can, do this using c#