The fundamentals
Blive bedre til CSS
Forstå, hvordan Nesting hjælper med organisering
Blive bedre til at bruge
Øvelser, øvelser og flere øvelser for at opbygge praktisk erfaring
Forstå bedre
Blive bedre til CSS og udvide kendskabet
Forstå, hvordan hjælper med organisering
Øvelser, øvelser og flere øvelser for at opbygge praktisk erfaring
Mål: Forstå the cascade bedre
* {
margin: 0;
box-sizing: border-box;
}
img {
display: block;
max-width: 100%;
height: auto;
}
html {
display: block;
}
head {
display: none;
}
body {
display: block;
margin: 8px;
}
h1 {
display: block;
font-size: 2em;
margin-block-start: 0.67em;
margin-block-end: 0.67em;
font-weight: bold;
}
a:-webkit-any-link {
color: -webkit-link;
cursor: pointer;
text-decoration: underline;
}
En masse regler er defineret, inden du selv har skrevet en linje CSS
Browser
.example {
display: flex;
}
.example > .child {
}
.example {
display: flex;
align-items: stretch;
justify-content: normal;
align-content: normal;
align-items: normal;
flex-direction: row;
flex-wrap: nowrap;
}
.example > .child {
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
}
* {
border-style: solid;
border-width: 0;
}
* {
border-style: solid;
border-width: 0;
}
:not(progress, meter) {
border-style: solid;
border-width: 0;
}
Et bedre udgangspunkt
body {
margin: unset;
min-height: 100svh;
}
* {
margin: 0;
box-sizing: border-box;
}
img {
display: block;
max-width: 100%;
height: auto;
}
:not(progress, meter) {
border-style: solid;
border-width: 0;
}
body {
min-height: 100svh /* 100dvh */;
}
...
* {
margin: 0;
box-sizing: border-box;
}
img {
display: block;
max-width: 100%;
height: auto;
}
:not(progress, meter) {
border-style: solid;
border-width: 0;
}
Temaets reset
:not(.special) a {
color: red;
}
<ul>
<li>
<a>Link</a>
</li>
<li class="special">
<a>Link</a>
</li>
<li>
<a>Link</a>
</li>
</ul>
p {
color: green;
}
#context {
color: red;
}
<div id="context">
<p>Some text</p>
</div>
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<p>Brødtekst <span>her</span>...</p>
</body>
</html>
<p>Brødtekst <span>her</span>...</p>
Brødtekst her...
<span>
body {
color: red;
}
Brødtekst her...
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<p>Brødtekst <span>her</span>...</p>
</body>
</html>
<p>Brødtekst <span>her</span>...</p>
Brødtekst her...
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<p>Brødtekst <span>her</span>...</p>
</body>
</html>
<p>Brødtekst <span>her</span>...</p>
body {
color: red;
}
p {
color: blue;
}
Brødtekst her...
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<p>Brødtekst <span>her</span>...</p>
</body>
</html>
<p>Brødtekst <span>her</span>...</p>
p {
color: blue;
}
span {
color: #000;
}
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<h1>Heading 1</h1>
<main>
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit.</p>
<section>
<h3>Hello</h3>
<p>Lorem ipsum dolor sit amet consectetur.</p>
</section>
</main>
</body>
</html>
body {
color: red;
}
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<h1>Heading 1</h1>
<main>
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit.</p>
<section>
<h3>Hello</h3>
<p>Lorem ipsum dolor sit amet consectetur.</p>
</section>
</main>
</body>
</html>
main {
color: red;
}
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<h1>Heading 1</h1>
<main>
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit.</p>
<section>
<h3>Hello</h3>
<p>Lorem ipsum dolor sit amet consectetur.</p>
</section>
</main>
</body>
</html>
section {
color: red;
}
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<h1>Heading 1</h1>
<main>
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit.</p>
<section>
<h3>Hello</h3>
<p>Lorem ipsum dolor sit amet consectetur.</p>
</section>
</main>
</body>
</html>
section p {
color: red;
}
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<h1>Heading 1</h1>
<main>
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit.</p>
<section>
<h3>Hello</h3>
<p>Lorem ipsum dolor sit amet consectetur.</p>
</section>
</main>
</body>
</html>
main {
font-size: 1.1rem;
}
section p {
color: red;
}
<!DOCTYPE html>
<html lang="da">
<head>...</head>
<body>
<h1>Heading 1</h1>
<main>
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit.</p>
<section>
<h3>Hello</h3>
<p>Lorem ipsum dolor sit amet consectetur.</p>
</section>
</main>
</body>
</html>
main {
font-size: 1.1rem;
}
section p {
color: red;
font-size: 1rem;
}
Properties, der nedarves:
color
font-family
font-size
font-weight
line-height
list-style
text-align
m.m.
html {
color: gray;
line-height: 1.5;
font-family: system-ui, sans-serif;
}
h1,
h2,
h3 {
color: #000;
line-height: 1.1;
font-family: "My Display Font";
}
Mål: Forstå principperne bag
Mål: Forstå principperne bag
Jeres tur
p:not(#id) {
color: green;
}
p.class {
color: red;
}
<p class="class">text</p>
<p id="id">last text</p>
p.test a {
color: green;
}
a:hover {
color: deeppink;
}
<p class="test">
<a href="#0">Hvilken farve?</p>
</p>
0,0,0
0,0,1
Layer A
Layer A
Layer C
Layer A
Layer B
Layer C
input[type="text"] {
border: 1px solid gray;
}
.my-input {
border: 4px solid blue;
}
Højeste prioritet
Laveste prioritet
Layer A
Layer B
Layer C
input[type="text"] {
border: 1px solid gray;
}
.my-input {
border: 4px solid blue;
}
Højeste prioritet
Laveste prioritet
Layer A
Layer B
Layer C
input[type="text"] {
border: 1px solid gray;
}
.my-input {
border: 4px solid blue;
}
Højeste prioritet
Laveste prioritet
@layer reset {
/* ... other reset rules */
input[type="text"] {
border: 1px solid gray;
}
}
@layer components {
/* ... other component rules */
.my-input {
border: 4px solid blue;
}
}
input[type="text"] {
border: 1px solid gray;
}
.my-input {
border: 4px solid blue;
}
Layer A
Layer B
@layer reset, components, utilities;
@layer reset {
/* ... other reset rules */
input[type="text"] {
border: 1px solid gray;
}
}
@layer components {
/* ... other component rules */
.my-input {
border: 4px solid blue;
}
}
configure up front
@layer reset, components, utilities;
@layer components {
/* ... other component rules */
.my-input {
border: 4px solid blue;
}
}
@layer reset {
/* ... other reset rules */
input[type="text"] {
border: 1px solid gray;
}
}
1
2
3
configure up front
@layer a, b;
@layer a {
header#my-header nav#my-nav ul li.link-item:hover {
color: red;
}
}
@layer b {
.link-item {
color: blue;
}
}
@layer reset, global, components, utilities;
@layer reset, global, components, utilities;
Hvert projekt
Projektspecifikt?
Projektspecifikt!
One-offs, hvert projekt?
@layer reset, global, components, utilities;
@layer reset {
ul[class] {
margin: 0;
padding: 0;
list-style: none;
text-align: start;
}
}
@layer utilities {
.flow {
> * + * {
margin-top: 1em;
}
}
}
eksempel
øvelse
ul[class] {
margin: 0;
padding: 0;
list-style: none;
}
øvelse
.flow {
> * + * {
margin-top: 1rlh;
}
}
øvelse
ul[class] {
margin: 0;
padding: 0;
list-style: none;
}
.flow {
> * + * {
margin-top: 1rlh;
}
}
0,1,1
0,1,0
øvelse
@layer reset, global, components, utilities;
@layer reset, global, components, utilities;
@import "./reset.css" layer(reset);
@layer global {
...
}
Astro
Astro
---
---
<article class="card">
...
</article>
<style>
@layer components.card {
article {
/* component specific styles */
}
}
</style>
Blive bedre til CSS
Forstå, hvordan Nesting hjælper med organisering
Blive bedre til at bruge
Øvelser, øvelser og flere øvelser for at opbygge praktisk erfaring
Oversæt selectoren (Fronter)
Vi kigger på det i plenum
Mål: Blive bedre og udvide kendskabet
li:nth-last-child(odd):first-child
Vælg det første liste-element i rækken, men kun hvis det samtidigt er ulige, hvis man tæller fra slutningen af listen.
:nth-child(2 of .featured)
Vælg det andet element, men kun hvis det er en af de elementer, der har 'featured'-klassen.
Mål: Blive bedre og udvide kendskabet
artcle:has(> img) {}
p:not(article > *) { ... }
article > :is(.class, #id) { ... }
article > :where(.class, #id) { ... }
article:has(h2) { ... }
article:has(h2, h3) { ... }
article:has(h2):has(h3) { ... }
article:not(:has(h2)) img { ... }
article:has(> :last-child:nth-child(3)) { ... }
body {
display: grid;
}
body:has(main + aside) {
grid-template-columns: 1fr 300px;
}
50ch
:not(:has(img))
p:has(img) {
display: contents;
}
<article>
<img>
</article>
<p>
</p>
p:has(img) {
display: contents;
}
<article>
<img>
</article>
<p>
</p>
p:has(img) {
display: contents;
}
<article>
<img>
</article>
<p>
</p>
Opfører sig nu som barn af <article>
ul:not(:has(> li:nth-child(5))) {
display: flex;
justify-content: center;
}
:root:has(dialog[open]) {
overflow: hidden;
}
Loading...
Blive bedre til CSS
Forstå, hvordan Nesting hjælper med organisering
Blive bedre til at bruge
Øvelser, øvelser og flere øvelser for at opbygge praktisk erfaring
Mål: Forstå, hvordan det hjælper med organisering
section {
border: 2px solid red;
}
section
color: red;
}
h2 {
section {
border: 2px solid red;
color: red;
}
h2 {
color: red;
}
section {
border: 2px solid red;
color: red;
}
}
&
h2 {
<section>
<h2></h2>
<h3></h3>
<p></p>
<article>
<h2></h2>
</article>
</section>
section {
border: 2px solid red;
color: red;
}
}
>
h2 {
<section>
<h2></h2>
<h3></h3>
<p></p>
<article>
<h2></h2>
</article>
</section>
section {
border: 2px solid red;
color: red;
}
}
:is( , h3)
h2 {
<section>
<h2></h2>
<h3></h3>
<p></p>
<article>
<h2></h2>
</article>
</section>
:is()
:where()
:nth-child()
.
>
+
~
[data-*]
&
Symboler
h1,
h2 {
& span {
color: blue;
}
}
& === :is(h1, h2)
h1 {
color: red;
& {
color: blue;
}
}
h1 {
color: red;
@media (width > 500px) {
color: blue;
}
}
h1 {
@media (width > 500px) {
color: blue;
}
color: red;
}
h1 {
@media (width > 500px) {
color: blue;
}
color: red;
}
h1 {
color: red; /* hoisted */
@media (width > 500px) {
color: blue;
}
}
1
2
h1 {
& {
color: blue;
}
color: red;
}
h1 {
article & {
color: red;
}
}
h1 {
color: blue;
article & {
color: red;
}
}
if / else
<h1>I'm blue</h1>
<div class="parent">
<h1>I'm red</h1>
</div>
h1 {
color: blue;
article & {
color: red;
}
}
if / else
<h1>I'm blue</h1>
<article>
<h1>I'm red</h1>
</article>
h2 {
color: blue;
section :nth-last-child(2 of &) {
color: red;
}
}
a {
background: red;
&:hover {
background: blue;
}
}
Co-location
a {
background: red;
&.btn {
padding: .5rem;
&:hover {
background: blue;
}
}
}
Co-location
a {
background: red;
&.btn {
padding: .5rem;
&:is(:hover, :focus-visible) {
background: blue;
}
}
}
Co-location
Øvelse
Omskriv til nesting
Øvelse
Al logik skal være i .article {}