CSS Overview
CSS Overview
CSS 4: 2012–Now
CSS 3 (& 4) is not one large single spec
www.w3.org/Style/CSS/current-work
Recommendation
Candidate
Recommendation
Last Call
Working Draft
Why CSS?
Separation of Concerns
Presentation: CSS
Behavior: JavaScript
Separate content (HTML) from presentation (CSS)
Site-wide consistency: control how all content looks
using only a few CSS files
Apply different styles to same content in different
media:
» web browser
» cell phone
» projector
» auditory
» print
» & more!
Adherence to standards
It’s fun!
The DOM
To understand CSS, you have to understand the DOM
(Document Object Model)
Before a webpage appears in a viewport, the rendering
engine downloads the HTML & parses it to display the
webpage on screen
CSS 2: 13
CSS 3: 21
70 in total
Basic selectors
» Universal
» Type
» Class
» ID
» Selector grouping
» Descendant combinator
» Child combinator
Universal Selectors
*
HTML
<p class="center note">
CSS
.center.note {
display: none;
}
There is no default list of class names
You come up with the class names your project uses (or
you use those provided by a framework like Bootstrap)
Don’t use spaces
Just be consistent!
ID Selectors
#id
Multiples with
Yes No
an element
Allowed:
» A –Z a –z
» 0 –9
» -_:.
Not allowed:
» spaces
» starting with anything other than A–Z or a–z
Just be consistent!
Use classes instead of IDs (in fact, try to avoid IDs as
much as possible when it comes to CSS)
IDs can make the cascade (more about that soon!) very
complicated
p {
font-family: Verdana, sans-serif;
font-size: 1em;
}
blockquote {
font-family: Verdana, sans-serif;
font-size: 1em;
}
Do this:
p, blockquote {
font-family: Verdana, sans-serif;
font-size: 1em;
}
Any selectors can be grouped
blockquote, p {
font-family: Verdana, sans-serif;
}
p {
line-height: 1.5;
}
h1 { h1, h2 {
font-weight: normal; font-weight: normal;
font-size: 2.5em; font-family: serif;
font-family: serif; }
}
h1 {
h2 { font-size: 2.5em;
}
font-weight: normal;
border-bottom: 1px
h2 {
dotted black; border-bottom: 1px
font-family: serif; dotted black;
font-size: 1.8em; font-size: 1.8em;
} }
Good practice Elements, then IDs, then classes
blockquote,
Alphabetical order within
option,
p,
each grouping of selectors
td,
#sidebar,
.legalese {
font-family: Verdana, sans-serif;
font-size: 1em;
}
Descendant
Combinator
selectorA selectorB
Combinators include:
(space)
+ (plus)
> (greater than)
~ (tilde)
You can often use a descendant combinator instead of a
class (& you must if you cannot change the HTML)
� �
HTML Cleaner HTML
<aside> <aside>
<img <img src="…">
class="headshot" </aside>
src="…">
</aside> Better CSS using the
descendant combinator
CSS aside img {}
.headshot {}
SIDE NOTE
h1 {color: dimgray;}
h1 {font-size: 1.4em;}
h1 {font-weight: bold;}
h1 {font-family: Verdana, sans-serif;}
Instead, combine related declarations
h1 {
color: dimgray;
font-size: 1.4em;
font-weight: bold;
font-family: Verdana, sans-serif;
}
Formatting CSS
selector {
property: value;
property: value;
property: value;
…
}
<span class="foo">…</span>
<span class="bar">…</span>
You want to style Miller’s Crossing so it’s purple & big
<p>
Let’s go see Miller’s Crossing!
</p>
HTML
<p>
Let’s go see <span class="movie-title">
Miller’s Crossing</span>!
</p>
<div>
<div> is an element that creates a block box & does
nothing else without CSS
<div class="call-out">
<p>
When a traveller in north central Massachusetts takes
the wrong fork at the junction of the Aylesbury pike just
beyond Dean’s Corners he comes upon a lonely and
curious country.
<p>
</div>
1. Inline styles
2. Embedded styles
3. Linking to external styles
4. @import
Inline
Uses the style global attribute
Quick & easy to create, but difficult & time-consuming
to manage
Testing
HTML email
Embedded
Styles inserted inside <style> … </style>
.emphasis {
font-weight: bold;
}
</style>
Embedded styles are great for one page …
Step 2
<head>
<link rel="stylesheet" type="text/css"
href="stylesheet.css">
</head>
HTML 5
<head>
<link rel="stylesheet" href="stylesheet.css">
</head>
What should you name your CSS file?
main.css
typography.css
client.css
search.css
navigation.css
Where should you place your CSS file?
/* Common */
p, blockquote, td {font-family: Verdana, sans-serif;
font-size: 1em;}
#footer {font-size: .9em;}
.emphasis {font-weight: bold;}
/* Navigation */
.on {font-size: 1.2em; font-weight: bold;}
.off {font-size: 1.2em;}
HTML comments
<!-- blah blah html blah blah html -->
CSS comments
/* blah blah css css blah blah css css */
Correctly formatted & ordered
/* Common */
p, blockquote, td {
font-family: Verdana, sans-serif;
font-size: 1em;
}
#footer {
font-size: .9em;
}
.emphasis {
font-weight: bold;
}
You can link to more than one style sheet, but you
should try to keep those links to a minimum
If you have more than one webpage, you really ought to
use an external style sheet
<style>
@import url("http://hpl.com/cthulhu.css");
/* colostomo.css */
@import url("http://hpl.com/cthulhu.css");
@import url("http://hpl.com/nodens.css");
h1 {font-size: 1.3em;}
p {font-family: Verdana, sans-serif;}
Inheritance
Some properties, like font-size & color, are
inherited: elements with those properties pass those
properties down through the DOM to their descendant
elements (unless overridden)
1. Importance
2. Specificity
3. Order
Importance
CSS can come from 3 places:
Browser
User
Author
All Web browsers have built-in CSS rules
Firefox
Safari
My safari.css file
html {
font-family: "Source Sans Pro", sans-serif;
}
p {
font-size: 36px !important;
}
html {
font-family: "Source Sans Pro", sans-serif;
}
AKA
User
Author
Author !important
User !important
Author & Author !important?
A + B + C + D = specificity
* 0+0+0+0 0
li 0+0+0+1 1
ul li 0+0+0+2 2
ul ol li.steps 0 + 0 + 1 + 3 13
li.steps.mech 0 + 0 + 2 + 1 21
#chapter1 0 + 1 + 0 + 0 100
style="foo" 1 + 0 + 0 + 0 1000
Order
Later CSS in the stylesheet wins over earlier CSS
.blue {color: blue}
.red {color: red}
<div class="callOut">
<p>
For the next 2 weeks…
</p>
</div>
.callOut {
background-color: #E6E8F2;
margin: 1em 1em 2em 1em;
padding: 1em;
border: 1px #ccc solid;
border-radius: 1em;
}
The result!
However…
That extra space at the
bottom really bothers me
That extra space at the
bottom really bothers me
To fix it, I put this in my CSS at lines 194–196:
.callOut > p {
margin-bottom: 0;
}
Lines 194–196:
.callOut > p:last-child {
margin-bottom: 0;
}
Line :
.callOut > p:last-child {
margin-bottom: 0;
}
Nope
Line :
#content p {
margin-bottom: 12px;
}
Line :
.callOut > p:last-child {
margin-bottom: 0;
}
Let’s look
#content p { .callOut > p:last-
margin-bottom: 12px; child {
} margin-bottom: 0;
}
Specificity Specificity
Line :
.callOut > p:last-child {
margin-bottom: 0;
}
Line :
.callOut > p:last-child {
margin-bottom: 0 !important;
}
Much better!
I won before
we could get to
anything else
ever got to it
Then I realized that <div class="callOut"> doesn’t
always end with <p>
$80
www.bradsoft.com
Espresso
Color picker
$0 (with $9.99 in-app purchase)
theolabrothers.com
ColorPro
$30
www.iconico.com/colorpro/
Thank you!
[email protected]
www.granneman.com
ChainsawOnATireSwing.com
@scottgranneman
[email protected]
websanity.com
CSS Overview
Selectors, Integration, Inheritance, Cascading
Attribution. You must give appropriate credit, provide a link to the license, and indicate if changes were
made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or
your use. Give credit to:
Share Alike. If you remix, transform, or build upon the material, you must distribute your contributions
under the same license as the original.
No additional restrictions. You may not apply legal terms or technological measures that legally restrict
others from doing anything the license permits.