Cascading Style Sheets
Standards & backward compatibility
/* break */
How?
<link>
elementUSE this method.
<style>
elementDO NOT use this method.
DO NOT use this method.
@import
rulesDO NOT use this method.
Performs worse than the <link>
!
See don’t use @import.
/* break */
property: value
{ declaration; declaration; ... }
/* single-line */
{ display: block; height: 300px }
/* multi-line */
{
display: block;
height: 300px;
}
Semicolons (;
) are only required between declarations.
Pattern matching against document trees
Selectors help you point to (select) specific HTML elements.
Basic simple selectors | Examples |
---|---|
Type selectors | HTML tags: nav , section , header , p |
Class selectors | HTML classes that uses dot scheme in CSS: .ds-body-img |
ID selectors | HTML classes that uses dot scheme in CSS: #data-story-personal |
Universal selector | Selects all-the-HTML-things: * |
Attribute selectors | Selects HTML tag attributes: [title] , [rel~="copyright"] |
Pseudo classes | Adds a CSS-defined class to an HTML element: :hover |
Combinators | Example |
---|---|
Descendent combinator (␣ ) |
.data-story-personal p {...} |
Child combinator (> ) |
ul#resources > li {...} |
Adjacent sibling combinator (+ ) |
h1 + h2 {...} |
General sibling combinator (~ ) |
img ~ p {...} |
HTML
containing block rules.
Containing blocks are established by parent box-context. (See W3C spec.)
Based on this selector declaration block, tell me how the elements will be styled.
JS Bin on jsbin.com::first-line
::first-letter
::before
::after
Type | Example |
---|---|
Location | :link , :visited , :target , ... |
User action | :hover , :focus , ... |
Tree-structural | :first-child , :nth-child() , ... |
:nth-child()
(pseudo-class)/* break */
UA, user, author
and !important
See this article on "The Cascade"
#main
.title
article
:hover
What's the color
and text-decoration
of the “Go to post” link?
/* break */
15px
1.25em
rebeccapurple == #663399
url(logo.png)
border-radius: 15px; z-index: 1; order: 3;
line-height: 1.5; flex: 0.618;
width: 80%; font-size: 120%;
lightgreen
, gold
, currentColor
, etc.#69c
, #abcdef
, ...rgb(200, 150, 100)
, hsla(120, 100%, 50%, 0.5)
, ...See more in CSS Color Module Level 4.
em
& rem
vw
, vh
ex
, ch
, etc.
See the Pen Basic heading level hierarchies by Chris Lindgren (@lndgrn) on CodePen.
px
, pt
, in
, cm
, mm
, ...
Extra tidbit: Learn about how pixels are defined -- Physical or pixel?
body {
background: url(assets/img/starrynight.png);
}
/* break */
See the Pen Intro Lesson for Initial Containing Block Relationships by Chris Lindgren (@lndgrn) on CodePen.
float: left;
position: absolute;
position: fixed;
position: fixed
position: absolute
fixed
positions an element based on the viewport.
absolute
positions an element based on the closest parent containing block.fixed
& absolute
display
behaviors. Notably, the CSS Grid display.
See the Pen Empty CSS Selection Practice by Chris Lindgren (@lndgrn) on CodePen.