css_en
css_en
Péter Jeszenszky
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 1 / 153
Terms of Use
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 2 / 153
What is CSS?
Website: https://www.w3.org/Style/CSS/
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 3 / 153
History
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 4 / 153
Development
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 5 / 153
Levels
CSS does not have versions in the traditional sense, instead, it has
levels:
CSS Level 1
CSS Level 2
CSS Level 3
Each level of CSS builds on the previous one, refining definitions and
adding features.
See: Levels, snapshots, modules. . . (W3C)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 6 / 153
CSS Level 1
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 7 / 153
CSS Level 2
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 8 / 153
CSS Level 3 (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 9 / 153
CSS Level 3 (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 10 / 153
CSS Level 3 (3)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 11 / 153
CSS Level 4 and Beyond
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 12 / 153
Browser Support
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 13 / 153
Coding Styles
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 14 / 153
Editors (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 15 / 153
Editors (2)
Non-free software:
WebStorm (platform: Linux, macOS, Windows)
https://www.jetbrains.com/webstorm/
See: https://www.jetbrains.com/help/webstorm/style-sheets.html
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 16 / 153
Editors (3)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 17 / 153
Editors (4)
Examples of Emmet abbreviations:
Abbreviation Expanded to
bd border: 1px solid #000;
bdt border-top: 1px solid #000;
bgc background-color: #fff;
fwb font-weight: bold;
p10 padding: 10px;
p1e padding: 1em;
mla margin-left: auto;
mra margin-right: auto;
pt.5e padding-top: 0.5em;
tac text-align: center;
tar text-align: right;
df display: flex;
w50% width: 50%;
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 18 / 153
CSS Frameworks
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 19 / 153
Other Tools
Minifier tools:
cssnano (written in: CSS/JavaScript; license: MIT License)
https://cssnano.github.io/cssnano/
https://github.com/cssnano/cssnano
CSSO (written in: CSS/JavaScript; license: License MIT)
https://css.github.io/csso/ https://github.com/css/csso
Visual Studio Code:
MinifyAll (written in: TypeScript; license: GPLv3) https:
//marketplace.visualstudio.com/items?itemName=josee9988.minifyall
https://minifyall.jgracia.es/ https://github.com/Josee9988/MinifyAll
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 20 / 153
Online CSS Sandboxes
CodePen
JSFiddle
Liveweave
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 21 / 153
File Properties
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 22 / 153
Characters
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 23 / 153
Escape Sequences (1)
Examples:
\9, \09, . . . , \000009 denote the vertical tab character.
\A9, \0A9, . . . , \0000A9 denote the copyright symbol (©).
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 24 / 153
Escape Sequences (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 25 / 153
Character Encoding
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 26 / 153
Comments
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 27 / 153
Box Model (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 28 / 153
Box Model (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 29 / 153
Box Model (3)
margin
border
padding
<Content>
Tartalom
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 30 / 153
Properties (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 31 / 153
Properties (2)
Shorthand property:
A property that allows authors to specify the values of several
properties simultaneously
For example, the margin property is a shorthand property for setting
the value of the margin-top, margin-right, margin-bottom and
margin-left properties.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 32 / 153
Declaration Blocks
Start with a '{' character and end with a '}' character, in between
there must be a list of zero or more declarations.
Declarations are of the form name:value, where whitespace characters
are allowed before and after tokens.
In a declaration name is an identifier..
Declarations must end with a ';' character.
The ';' character after the last declaration can be omitted.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 33 / 153
Rule Sets (Style Rules)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 34 / 153
At-rules
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 35 / 153
Values (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 36 / 153
Values (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 37 / 153
Values (3)
For each property, CSS specifications define the type, number, and
ordering of components allowed in property values.
Examples:
text-transform: none | capitalize | uppercase | lowercase
| full-width
letter-spacing: normal | <length>
border-color: <color>{1,4}
border-top: <line-width> || <line-style> || <color>
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 38 / 153
Identifiers
Syntax:
See: https://www.w3.org/TR/css-syntax-3/#ident-token-diagram
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 39 / 153
Strings
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 40 / 153
URL-s
Examples:
list-style-image: url("http://eg.com/images/item.png")
list-style-image: url('http://eg.com/images/item.png')
list-style-image: url(http://eg.com/images/item.png)
Relative references can be used and are resolved against the URL of
the stylesheet.
Example: list-style-image: url("images/item.png")
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 41 / 153
Numbers
Real numbers: consist of an optional sign, one ore more decimal digits,
an optional dot, optionally followed by an 'e' or 'E' character and an
integer.
At least one digit is required after the dot.
Integers are special cases of real numbers.
Examples: 1.5, -100.0, +.25, 1E-10
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 42 / 153
Percentages (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 43 / 153
Percentages (2)
Examples:
sup {
font-size: 75%;
}
nav {
width: 40%;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 44 / 153
Dimensions
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 45 / 153
Lengths (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 46 / 153
Lengths (2)
Relative units:
em: equals to the computed value of the font-size property of the
element on which it is used (in case of the font-size property itself it
refers to the computed font-size of the parent element).
ex: equals to the x-height of the element’s font (in case of the of the
font-size property itself it refers to the x-height of the parent
element).
It is so called because it is often equal to the height of the lowercase x
character.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 47 / 153
Colors (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 48 / 153
Colors (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 49 / 153
Colors (3)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 50 / 153
Colors (4)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 51 / 153
Selectors (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 52 / 153
Selectors (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 53 / 153
Selectors (3)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 54 / 153
Selectors (4)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 55 / 153
Selectors (5)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 56 / 153
Selectors (6)
Selector:
A chain of one or more sequences of simple selectors separated by
combinators.
One pseudo-element may be appended to the last sequence of simple
selectors.
Examples:
p
a img
h1 ~ table
div#main > h1
div > h1 + p
q::before
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 57 / 153
Type Selector
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 58 / 153
Universal Selector
Written as *.
Matches all elements.
May be omitted from a simple selector, if it is not the only component.
Thus, for example, the following selectors are equivalent:
*#nav and #nav
*.important and .important
*[title] and [title]
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 59 / 153
Attribute Selectors (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 60 / 153
Attribute Selectors (2)
[att|=val]:
Matches elements with the att attribute, its value either being exactly
val or beginning with val-.
Primarily intended to be used for attributes whose value is a language
tag, for example, en-US, en-GB.
For the xml:lang attribute and the HTML lang attribute the
:lang(C) pseudo-class should be used.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 61 / 153
Attribute Selectors (3)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 62 / 153
Attribute Selectors (4)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 63 / 153
Attribute Selectors (5)
Examples:
style[type=italic] {
font-style: italic;
}
style[type=bold] {
font-weight: bold;
}
style[type=normal] {
font-style: normal;
font-weight: normal;
}
a[hreflang|=en] {
text-decoration: line-through;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 64 / 153
Class Selector
.important {
color: red;
text-decoration: underline;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 65 / 153
ID Selector (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 66 / 153
ID Selector (2)
Examples:
div#main {
width: 50%;
margin-left: auto;
margin-right: auto;
}
#footer {
text-align: center;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 67 / 153
Pseudo-classes (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 68 / 153
Pseudo-classes (2)
Link pseudo-classes:
:link: applies to links that have not yet been visited.
:visited: applies to links that have not yet been visited.
For privacy reasons, a very limited set of CSS properties can be set via
the :visited pseudo-class.
Moreover, for the same reasons, modern web browsers lie about the
values of the CSS properties of visited links when they are queried from
JavaScript via the Window.getComputedStyle() method.
See:
:visited (MDN)
Privacy and the :visited selector (MDN)
Window: getComputedStyle() method (MDN)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 69 / 153
Pseudo-classes (3)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 70 / 153
Pseudo-classes (4)
a:visited {
color: gray;
text-decoration: line-through; /* Won't work */
}
a:hover {
border-width: medium;
border-style: solid;
}
a:active {
font-weight: bolder;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 71 / 153
Pseudo-classes (5)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 72 / 153
Pseudo-classes (6)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 73 / 153
Pseudo-classes (7)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 74 / 153
Pseudo-classes (8)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 75 / 153
Pseudo-classes (9)
Structural pseudo-classes:
Only elements are counted when calculating the position of an element
in its list of siblings.
For example, text, comments, and processing instructions are ignored.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 76 / 153
Pseudo-classes (10)
Structural pseudo-classes:
:root: represents the root element of the document.
:only-child: represents elements that have no siblings.
:only-of-type: represents elements that have no siblings with the
same element name.
:empty: represents elements that have no children at all.
These elements do not contain any text or elements, but may contain
comments and processing instructions.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 77 / 153
Pseudo-classes (11)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 78 / 153
Pseudo-classes (12)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 79 / 153
Pseudo-classes (13)
Selector Meaning
tr:nth-child(2n+0) The even rows of a table
tr:nth-child(2n)
tr:nth-child(even)
tr:nth-child(2n+1) The odd rows of a table
tr:nth-child(2n-1)
tr:nth-child(odd)
tr:nth-child(5) The the fifth row of a table
tr:nth-child(5n+1) The 1st, 6th, 11th, . . . rows of a table
tr:nth-child(5n-4)
tr:nth-child(-n+5) The first five rows of a table
tr:nth-last-child(-n+5) The last five rows of a table
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 80 / 153
Pseudo-classes (14)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 81 / 153
Pseudo-classes (15)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 82 / 153
Pseudo-classes (16)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 83 / 153
Pseudo-elements (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 84 / 153
Pseudo-elements (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 85 / 153
Pseudo-elements (3)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 86 / 153
Pseudo-elements (4)
div.proof::after {
content: "\220E" /* End of proof */
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 87 / 153
Combinators (1)
Descendant combinator:
Whitespace that separates two sequences of simple selectors.
If P and Q are two sequences of simple selectors, the selector P Q
represents elements matching Q that are descendants of elements
matching P.
Example:
thead th {
background-color: darkgrey;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 88 / 153
Combinators (2)
Child combinator:
A '>' character that separates two sequences of simple selectors.
If P and Q are two sequences of simple selectors, the selector P > Q
represents elements matching Q that are children of elements matching
P.
Example:
nav > div {
display: inline;
}
p > img:only-child {
margin-left: 0;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 89 / 153
Combinators (3)
Next-sibling combinator:
A '+' character that separates two sequences of simple selectors.
If P and Q are two sequences of simple selectors, the selector P + Q
represents elements matching Q that immediately follow an element
matching P.
The elements represented by the two sequences share the same parent in
the document.
Non-element constructs (e.g., text and comments) are ignored when
considering adjacency of elements.
Example:
h1 + p {
text-indent: 0;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 90 / 153
Combinators (4)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 91 / 153
Grouping Rules (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 92 / 153
Grouping Rules (2)
For example, the first three rules are equivalent to the fourth one:
h1 + p { text-indent: 0 }
h2 + p { text-indent: 0 }
h3 + p { text-indent: 0 }
h1 + p, h2 + p, h3 + p {
text-indent: 0;
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 93 / 153
Selectors Level 4 (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 94 / 153
Selectors Level 4 (2)
:is() pseudo-class:
A selector of the form :is(X) where the argument is a selector list.
Also called matches-any pseudo-class.
Matches any element that can be matched by any of the selectors in
the list argument.
Useful for representing long selector lists in a more compact form.
Pseudo-elements are not valid within :is().
The specificity of the :is() selector is the specificity of selector with
the highest specificity in the list argument.
Browser support: https://caniuse.com/css-matches-pseudo
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 95 / 153
Selectors Level 4 (3)
:is() pseudo-class: for example, the following two style rules are
equivalent:
div:is( .note, .warning, .hint)::before {
/* ... */
}
div.note::before ,
div.warning::before ,
div.hint::before {
/* ... */
}
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 96 / 153
Other Uses of CSS Selectors
Web scraping:
Free and open source software:
Scrapy (written in: Python; license: New BSD License)
https://scrapy.org/ https://github.com/scrapy/scrapy
Parsel (written in: Python; license: New BSD License)
https://parsel.readthedocs.io/ https://github.com/scrapy/parsel
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 97 / 153
Specificity (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 98 / 153
Specificity (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 99 / 153
Specificity (3)
Specificity of a declaration:
Each declaration has the same specificity as of the selector of the style
rule it appears in.
Declarations that do not belong to a style rule (such as the contents of
a style attribute in HTML) are considered to have a specificity higher
than any selector.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 100 / 153
Calculating a Selector’s Specificity (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 101 / 153
Calculating a Selector’s Specificity (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 102 / 153
Calculating a Selector’s Specificity (3)
Selector Specificity
* (a = 0, b = 0, c = 0)
div (a = 0, b = 0, c = 1)
p::first-letter a (a = 0, b = 0, c = 2)
img h1 + p
div[class=nav] (a = 0, b = 1, c = 1)
div.nav
#main *:lang(en) (a = 1, b = 1, c = 0)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 103 / 153
Stylesheets of Different Origins
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 104 / 153
User agent stylesheets (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 105 / 153
User agent stylesheets (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 106 / 153
User Stylesheets
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 107 / 153
Author Stylesheets (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 108 / 153
Author Stylesheets (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 109 / 153
Importing Stylesheets
The @import at-rule allows users to import style rules from other
stylesheets.
The URL of the stylesheet can be specified with the url() function or
as a string.
If an @import at-rule refers to a valid stylesheet, user agents must
treat the contents of the stylesheet as if they were written in place of
the @import at-rule.
Any @import at-rules must precede all other at-rules and style rules
with the exception of the @charset at-rule.
Example:
@import url(https://fonts.googleapis.com/css?family=Tangerine);
@import "default.css";
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 110 / 153
Important Declarations (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 111 / 153
Important Declarations (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 112 / 153
Important Declarations (3)
Example:
<html lang="en">
<head>
<title>!important</title>
<meta charset="UTF-8">
<styl>
p {
background-color: cornsilk;
color: green !important;
}
</style>
</head>
<body>
<p style="background-color: aliceblue; color: red">
Hello, World!
</p>
</body>
</html>
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 113 / 153
Calculating Property Values
The final value of a CSS property for a given element or box is the result of
a multi-step calculation:
Cascaded value: the result of the cascade.
Specified value: the result of the defaulting.
Computed value: the result of transforming a relative value to an
absolute one.
Used value: the result of formatting the document.
Actual value: the result of transforming the used value to be suitable
for a display environment.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 114 / 153
Cascading, Inheritance, and Initialization
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 115 / 153
Cascade (1)
Several different declarations can set the value of the same variable.
These declarations can be of different origins.
The cascade is the process during which it is determined which
declaration sets the value of a given property of a given element.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 116 / 153
Cascade (2)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 117 / 153
Rule Ordering (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 118 / 153
Rule Ordering (2)
For example, the first two rules will never be taken into account here:
a:active { color: red } /* (a = 0, b = 1, c = 1) */
a:hover { color: green } /* (a = 0, b = 1, c = 1) */
a:visited { color: black } /* (a = 0, b = 1, c = 1) */
a:link { color: blue } /* (a = 0, b = 1, c = 1) */
The correct order of these rules is the following (the first two rules are
interchangeable):
a:visited { color: black }
a:link { color: blue }
a:hover { color: green }
a:active { color: red }
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 119 / 153
Specified Value
The specified value the value of a given property that the style sheet
authors intended for that element.
If the cascade results in a value, then that is the specified value.
Otherwise, the specified value is determined by defaulting process.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 120 / 153
Defaulting
When the cascade does not result in a value, the specified value must
be found some other way.
Inherited properties draw their defaults from their parent element
through inheritance.
All other properties take their initial value.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 121 / 153
Computed Value (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 122 / 153
Computed Value (2)
Example:
CSS:
section { font-size: 12px }
h1 { font-size: 1.5em }
HTML:
<section>
<h1>Introduction</h1>
</section>
The specified and the computed value of the font-size property of
the section element is 12px.
The specified value of the font-size property of the h1 element is
1.5em, it’s computed value is 1.5 × 12px = 18px.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 123 / 153
Used Value
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 124 / 153
Actual Value
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 125 / 153
Inheritance (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 126 / 153
Inheritance (2)
Example:
When no rules apply to the em HTML element below that set the value of
the color property, the element inherits the value of the property from the
span element (i.e., red).
<span style="color: red">
May the <em>force</em> be with you!
</span>
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 127 / 153
Inheritance (3)
Example:
When the style rule below applies, a hyperlink inherits the value of the
color property from it’s parent. Thus, for example, the value of the color
property of the a element in the div element is green.
a:visited , a:link {
color: inherit;
}
<p style="color: green">
Click here: <a href="https://www.w3.org/">W3C</a>
</p>
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 128 / 153
Initial Value
Each property has an initial value, defined in the relevant CSS
specification. The initial value can be defined to depend on the user
agent.
For example, the initial value of the background-color property is the
transparent keyword.
See:
https://www.w3.org/TR/css-backgrounds-3/#the-background-color
For example, the initial value of the color property depends on the user
agent.
See: https://www.w3.org/TR/css-color-3/#foreground
If the property is not an inherited property, and the cascade does not
result in a value, then the specified value of the property is its initial
value.
If the cascaded value of a property is the initial keyword, the
property’s initial value becomes its specified value.
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 129 / 153
Assignment (1)
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 130 / 153
Assignment (2)
Consider the following style rules in an author stylesheet associated with a HTML
document:
.movies > ul > li:not(:first-child) { color: navy }
h1 + ul li:first-child { color: gold }
ul > :nth-child( 2n + 1) { color: purple }
#ot li + :last-child { color: tomato }
*:not( [class]) + ul:first-child > li:first-child { color: silver }
What is the color of the text in each li element?
<section id="ot" class="movies">
<h1>The Original Trilogy</h1>
<ul>
<li>A New Hope</li>
<li>The Empire Strikes Back</li>
<li>Return of the Jedi</li>
</ul>
</section>
Péter Jeszenszky (DE-IK) Cascading Style Sheets (CSS) October 28, 2024 131 / 153