Cascading Style Sheets
Standards & backward compatibility
/* break */How?
<link> element<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
...
USE this method.
<style> element<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<style type="text/css">
/* ... */
</style>
</head>
<body>
...
DO NOT use this method.
<body>
<h1 style="color: red;">
Hello World
</h1>
<p style="margin: 2em 0;">
Paragraph here
</p>
</body>
DO NOT use this method.
@import rules@import url(normalize.css);
@import url(layout.css);
@import url(typography.css);
nav {
position: fixed;
top: 0;
left: 0;
height: 3em;
}
DO 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.titlearticle:hoverWhat's the color and text-decoration of the “Go to post” link?
/* break */15px1.25emrebeccapurple == #663399url(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?
See the Pen Using string values for CSS selection and content declaration by Chris Lindgren (@lndgrn) on CodePen.
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: absolutefixed positions an element based on the viewport.
absolute positions an element based on the closest parent containing block.fixed & absoluteSee the Pen Simple display fixed and absolute example by Chris Lindgren (@lndgrn) on CodePen.
display behaviors. Notably, the CSS Grid display.
See the Pen Empty CSS Selection Practice by Chris Lindgren (@lndgrn) on CodePen.