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?
<link>
element<style>
element@import
rules inside <style>
elementPerforms worse than the <link>
way!
See don’t use @import.
@charset
@import
@charset
)@media
@font-face
display
position
height
font-family
transition
Why vendor prefixes?
property: value
display: block
position: fixed
height: 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
::after
Compatibility: ::
vs. :
selector declaration block
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;
}
}
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]
, :hover
article
, ::before
Compare [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: 2
2
line-height: 200%
line-height: 2em
32px
1.2rem
60deg
rebeccapurple
1000ms
url(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
, ...
px
, 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!
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: absolute
position
other than static
transform
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: fixed
transform
other than none
position: 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