Cascading Style Sheets
Standards & backward compatibility
/* questions? */
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.
/* questions? */
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 hash 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 |
See the Pen Demo basic CSS selection by Chris Lindgren (@lndgrn) on CodePen.
Combinators | Example |
---|---|
Descendent combinator (␣ ) |
.data-story-personal p {...} |
Child combinator (> ) |
ul#resources > li {...} |
Adjacent sibling combinator (+ ) |
h1 + h2 {...} |
General sibling combinator (~ ) |
img ~ p {...} |
See the Pen Demo basic CSS selection by Chris Lindgren (@lndgrn) on CodePen.
HTML
containing block rules.
Containing blocks are established by parent box-context. (See W3C spec.)
Based on these selectors declaration blocks, 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)/* questions? */
UA, user, author
and !important
See this article on "The Cascade"
#main
.title
article
:hover
See the Pen Using the cascade in CSS by Chris Lindgren (@lndgrn) on CodePen.
What is the color
and text-decoration
of the “Go to post” anchor/link?
/* questions? */
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
, olivedrab
, etc.#69c
, #abcdef
, #eee
, #123456
...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(../img/starrynight.png);
}
/* questions? */
Test it out with your Inspect Element.
position: default
(normal)position: absolute
position: fixed
position: relative
float: [left, right, none]
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.
{
);{
) on the same line as the last selector;:
);;
) at the end of all declarations;}
) on its own new line;With this new knowledge and guidelines in mind: