GU Yiling @ Baidu EFE
Cascading Style Sheets
font, color, align, box-model, #id, .class, ...
position, z-index, media, text-shadow, ...
No. Modules + Level!
| Module | Latest Level (2015/09) | Example |
|---|---|---|
| Text | 3 (LC) / 4 (FPWD) | text-justify, word-break, hyphens |
| Flexible Box | 1 (LC) | flex, justify-content |
| Color | 3 (REC) / 4 (ED) | rgba(), currentColor, hwb() |
| Animations | 1 (WD) | animation, @keyframes |
| Transforms | 1 (WD) | transform, perspective |
| Transitions | 1 (WD) | transition, timing functions |
| Cascading Variables | 1 (WD) | --foo, var() |
| Round Display | 1 (FPWD) | border-boundary, position: polar |
How?
<body>
<h1 style="font-size: 2em; font-weight: 700;">
Hello World
</h1>
<p style="margin: 2em 0;">
Paragraph here
</p>
</body>
<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>
...
<style> element<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World</title>
<style type="text/css">
/* ... */
</style>
</head>
<body>
...
@import rules inside <style> element<style>
@import url(normalize.css);
@import url(layout.css);
@import url(typography.css);
</style>
Performs worse than the <link> way!
See don’t use @import.
@charset@import@charset)@media@font-face@charset "UTF-8";
@import "styles.css" screen and (min-width: 960px);
displaypositionheightfont-familytransitionWhy vendor prefixes?
property: value
display: blockposition: fixedheight: 2em{ declaration; declaration; ... }
/* single-line */
{ display: block; height: 3em }
/* multi-line */
{
color: red;
opacity: 0.5;
}
Semicolons (;) are only required between declarations.
Pattern matching against document trees
Name a few?
| Basic simple selectors | Example |
|---|---|
| Type selectors | p, span |
| Universal selectors | * |
| Attribute selectors | [title], [rel~="copyright"] |
| Class selectors | .comment, .post |
| ID selectorss | #nav, #main |
| Pseudo classes | :hover, :lang(zh), :checked |
How many pseudo classes do you know?
| Type | Example |
|---|---|
| Linguistic | :dir(), :lang() |
| Location | :link, :visited, :target, ... |
| User action | :hover, :focus, ... |
| Input | :checked, :valid, :disabled, ... |
| Tree-structural | :first-child, :nth-child(), ... |
| Combinators | Example |
|---|---|
Descendent combinator ( ) |
.post p |
Child combinator (>) |
ul > li |
Adjacent sibling combinator (+) |
h1 + h2, :checked + label |
General sibling combinator (~) |
h1 ~ pre |
::first-line::first-letter::before::afterCompatibility: :: vs. :
selector declaration block
.post a {
color: #69c;
text-decoration: underline;
}
at-rule | ruleset
A style sheet consists of a list of statements.
@charset "UTF-8";
a {
color: #69c;
}
@media screen and (min-width: 35em) {
a {
font-size: 1.2em;
}
}

p:first-of-type::first-letter {
font-size: 40px;
font-weight: bold;
}

blockquote::before {
content: "“";
font-size: 60px;
font-weight: bold;
color: gray;
vertical-align: bottom;
position: relative;
top: 15px;
}

div:nth-child(2n+1) {
transform: rotate(-10deg);
}
div:nth-child(2n+2) {
transform: rotate(10deg);
}
div:nth-child(2n+3) {
transform: rotate(0);
}
declared → cascaded → specified → computed → used → actual
cascading → defaulting → absolutizing → calculation → UA adjustment
Finding value for an element/property combination
UA, user, author
... and we have !important ...
author vs. user?
What about
author & important vs. user & important?
That's all? Check CSS Cascading and Inheritance Level 3.
#main.title, [name], :hoverarticle, ::beforeCompare [a, b, c] and choose a winner!
style attribute > selectors
<style>
p.row :hover {
color: blue !important;
text-decoration: underline !important;
}
#link {
color: green;
}
</style>
<p class="row" stlye="color: red !important; text-decoration: underline !important;">
<a id="link" href="post.html" style="text-decoration: none;">Go to post</a>
</p>
What's the color and text-decoration of the “Go to post” link?
inherit keywordComputed value of the parent element
.post {
font-size: 16px;
line-height: 2; /* Which */
line-height: 200%; /* to */
line-height: 2em; /* choose? */
}
line-height: 22line-height: 200%line-height: 2em32px1.2rem60degrebeccapurple1000msurl(logo.png)a[rel~="next"]::after {
content: "→";
}
body {
background: url(starrynight.png);
}
z-index: 1; order: 3;line-height: 1.5; flex: 0.618;width: 80%; font-size: 120%;0px → 0
length only!
em, ex, ch, rem, vw, vh, ...
ex and chpx, pt, in, cm, mm, ...
Physical or pixel?
lightgreen, gold, currentColor, transparent ← transparent, ...#69c, #abcdef, ...rgb(200, 150, 100), hsla(120, 100%, 50%, 0.5), ...See more in CSS Color Module Level 4.
angle, time, image, ...
.box {
transform: rotate(30deg);
transition-duration: 2s;
background: linear-gradient(to top, red 0%, blue 100%);
}
Dimension units after zero values are optional for length only!
<canvas>) and the viewport
Why box-sizing?
What about * { box-sizing: border-box }?
Determined by position and float
What's “clear fix”?
position: absolute/fixed
Finding the containing block
(See detail here.)
position: absoluteposition other than statictransform other than none
body {
width: 120%;
background-color: khaki;
}
.box {
position: absolute;
bottom: 10px;
right: 10px;
width: 64px;
height: 64px;
background-color: lightgreen;
}
See the Pen dooBPz by GU Yiling (@Justineo) on CodePen.
position: fixedtransform other than noneposition: fixed & transform.out {
margin: 64px;
width: 256px;
height: 256px;
background-color: khaki;
transform: scale(1);
}
.in {
position: fixed;
top: 10px;
left: 10px;
width: 64px;
height: 64px;
background-color: lightgreen;
}
See the Pen yNNdNB by GU Yiling (@Justineo) on CodePen.
box-sizing