CSS Cheat-Sheet Notes (Quick Reference)
CSS Cheat-Sheet Notes (Quick Reference)
Selectors
Basic
h1
select all <h1> tags
h1.special
select h1 tags of the special class
h1#special
select h1 tags of special id
p a
select <a> tags descendant from p
h1, h2
select both h1, h2
*
select all tags
implicit in
.class = *.class
#class = *#class
#banner *
select all tags inside banner
a:link
selects link that guest hasn’t visited
regular style
a:visited
link that visitor has clicked before
a:hover
link when mouse is hovered over
a:active
link when it’s clicked (just a ms)
.class1.class2
select elements that have both class1 and class2
1
Pseudo-classes
generate content
:before
inserts content before
p.tip:before {content: "HOT TIP!" }
:after
like :before but after
properties
generated pseudo-classes can be styled like any other
display
color
border
content
string
content: "(link) "
urls
content: url(path/to/image)
attributes
content: attr(href)
combinations
content: " [" attr(href) "]"
:first-child
select and format just the first child of an element
e.g.,
li:first-child { font-weight: bold; }
formats all first elements in bold
:focus
when element receives focus (e.g., click or tab)
e.g.,
input:focus { background-color: #FFFFCC; }
Advanced selectors
child selectors
body > h1
select any h1 that is a child of body
adjacent siblings
h2 + p
select p right after h2
attribute selectos
img[title]
2
select only img tags with "title" attribute
.photo[title]
select any element with photo class and a title attribute
input[type="text"]
input fields of type text
a[href][title]
select a that has both href and title attrs
*[title]
any element that has title attribute
img[title~="Figure"]
any img with title that contains Figure in it
regexp match?
img[title^="bar"]
any img with title that starts with "bar"
img[title$="bar"]
any img with title that ends with "bar"
img[title*="bar"]
any img with title that contains substrig "bar"
*[lang|="en"]
any element with lang attr that
equals en
begins with en-
Pseudo-classes
:first-line
:first-letter
just the first letter
Psuedo-elements
:before
:after
# examples
3
Floats
containing block
nearest block-level ancestor
Rules
1) stay within borders of containing element
left/right outer edge may not be to the left of inner left/right containing block.
2) give precedence to existing floats
6) floats can’t float higher than the top of the line box generated by a preceding element
7) floats get pushed down to a new line height if there isn’t room for them in the containing
box
8) given the above constraints, float as high as possible
9) left floating elements try to float as left as possible, right floating elements try to float as
right as possible
BUT
a higher position is preferred to floating more right
Rule consequences
when floated element is taller than container?
the bottom sticks out of the container
countermeasure
floated element will expand to contain floated
descendants
negative margins
can cause floats to appear to escape their parents
4
just like negative margins on non-floated elements
can make the child appear wider than it’s parent
clear property
don’t wrap around a floated item
left - drop below left floated items
right - drop below right floated items
both - drop below both
Positioning
position := static | relative | absolute | fixed | inherit
static
generated as normal
relative
offset by some distance
element retains it’s shape
space it would ordinarily have occupied preserved
absolute
removed from the flow
fixed
element box behaves like absolute
5
but containing block is the viewport itself
containing block
nearest ancestor (of any kind) that has position value != static
block-level containing ancestor
padding edge (I.e., border bounded area)
no ancestors?
initial containing block
offset properties
percentage
containing width for left / right
height for top / bottom
auto
element position if it were static
width 100%
left: 0
right: 0
fixed position
6
can be used to simulate frames of old
just like absolute position except
the containing block is the viewport
Properties
formatting text
line-height
letter-spacing # how much space to add between letters
font-family
sans # clean and simple appearance (for headlines)
Arial
Helvetica
Verdana
Tahoma
Formata
Sans
popular combinations
Arial, Helvetica, sans-serif
"Times New Roman", Times, serif
"Courier New", Courier, monospace
Georgia, "Times New Roman", Times, serif
font size
keywords
xx-small x-small small medium large x-large xx-large
font styles
font-style: italic|bold|normal
text-transform: uppercase
font-variant: small-caps
text-decoration: underline|overline|line-through|blink
spacing attributed
letter-spacing
word-spacing
line-height
normal setting is 120%
7
alignment
text-align: left|center|right|justify
text-indent
text-shadow
<color> <offset-x> <offset-y> <blur-radius>?
white-space
normal | nowrap | pre | pre-wrap | pre-line | inherit
pre-wrap :=
like pre except lines wrap
lists
list-style-type: square|disc|circle
list-style-position: outside|inside
padding is added
inline vs block
display: inline|block;
8
inline elements don’t get any taller with padding or margins
colors
rgb(30%, 30%, 30%)
#FFF #FFFFFF
names
aqua
fuchsia
lime
olive
red
white
black
gray
maroon
orange
silver
yellow
blue
green
navy
purple
teal
border
each side can have a different property
width / height
em is the text size
percentages = size of containing element
max-width
min-width
max-height
min-height
height property
dangerous because it’s hard to know in advance the size of content
in a box
9
for consistent predictable results set this yourself
background images
background-image: url(images/bg.gif);
url is relative to the stylesheet not the HTML page
root relative
absolute
background-repeat
repeat
normal setting, repeat both on x and y
repeat-x
repeat-y
no-repeat
displays the image a single time
background-position
precise values
<distance-from-left> <distance-from-top>
keywords
<horizontal> <vertical>
<horizontal> := left|center|right
<vertical> := top|center|bottom
bottom is the bottom of the content
not necessarily the bottom of the pag
e.g.,
background-position: left center
percentages
short hand
background := <background-color> <background-image>
<background-attachment> <background-position>
10
it effects layout normally
Outlines
Like borders except:
• they don’t cause reflow: good for adding an outline to emphasize focus
• they can be non-rectangular
• they can invert the background
Syntax:
outline := <outline-color> || <outline-style> || <outline-width>
Tips
separation of concerns
HTML should describe structure
don’t use class names that describe style
classes vs ids
classes for repeating elements
id selectors for unique elements
margin: 0 auto
lets content float
equivalent to <center>
margins collapse
11
Workflow
put temporary code on top of the stylesheet
makes it easiest to work with firebug
refactor
eliminate code
CSS
HTML
let the future take care of itself
minimize reloads
edit CSS mode
better than editing on server
refactor in gvim
debugging
draw borders to better understand box model
Misc
element types
replaced vs non-replaced
block vs inline
block
by default
fills its parent element
can not have other elements at its sides
I.e., generates a break before and after box
12
e.g.,
p
div
li
special case that generates marker
inline
generated line boxes that are flowed within parent
class names
letters, numbers, hyphens and underscores
must start with a letter
case sensitive
terminology
ancestor
descendent
parent
closest ancestor
child
closest descendent
siblings
inheritance
not passed
border properties
placement properties
margins
paddings
weights
tag selector = 1
class selector = 10
ID selector = 100
inline style = 1000
overruling specifiy
!important after any prperty
13
organizing styles and stylesheets
name styles by purpose not appearance
refactor common styles to separate classes
combine multiple classes
group styles
apply to related parts ofa page
group styles with related purpose
xhtml
differences from HTML
lowercase tags
quotation marks required for xhtml
all tags must be closed
<br />
validator.w3.org
tags
p # for paragraphs
li
dl # definition list
dt # definition term
blockquote # quotes
q # one line quotes
cite # referencing
address # identify and supply contact info
table
tags
caption
attribute align=top|bottom
colgroup
col
thead
tr
th
tbody
tr
td
cell properties
text-align
vertical-align top|baseline|middle|bottom
border-collapse: collapse|separate
14