Introduction to CSS

17 April 2024

Updated: 17 April 2024

ZOOOOOOM!

Is everything big enough and are we in dark mode?

An Introduction to CSS

Agenda

  • Basic Syntax
  • Selectors
  • Cascade
  • Box Model
  • Flow Layout
  • Debugging

Let’s get some markup

Well, we need something to style

css-basic-layouts/Syntax.astro
1
<div class="overview">
2
<h1>Syntax Overview</h1>
3
4
<p>Some things you can see in this example:</p>
5
6
<ol class="features">
7
<li>Declarations</li>
8
<li>Rules</li>
9
<li>Resets/Normalizations</li>
10
<li class="fancy">Class selectors</li>
11
<li>Psuedo-class selectors</li>
12
</ol>
13
14
<blockquote>Then - like just draw the rest of the owl</blockquote>
15
</div>

Syntax Overview

Some things you can see in this example:

  1. Declarations
  2. Rules
  3. Resets/Normalizations
  4. Class selectors
  5. Psuedo-class selectors
Then - like just draw the rest of the owl

Now let’s style it

css-basic-layouts/Syntax.astro
1
<div class="overview">
2
<h1>Syntax Overview</h1>
3
4
<p>Some things you can see in this example:</p>
5
6
<ol class="features">
7
<li>Declarations</li>
8
<li>Rules</li>
9
<li>Resets/Normalizations</li>
10
<li class="fancy">Class selectors</li>
11
<li>Psuedo-class selectors</li>
12
</ol>
13
14
<blockquote>Then - like just draw the rest of the owl</blockquote>
15
</div>
css-basic-layouts/syntax.css
1
h1 {
2
color: lightblue;
3
margin: 0px;
4
}
5
6
.overview {
7
padding: 1rem;
8
transition: all 300ms;
9
border: solid 4px transparent;
10
}
11
12
.fancy {
13
text-decoration: underline;
14
}
15
16
.overview:hover {
17
border-color: lightblue;
18
}

Selectors

css-basic-layouts/selectors.css
1
/* Element */
2
h1 {}
3
4
/* ID */
5
#thing-id {}
6
7
/* Class */
8
.some-class {}
9
10
/* Attribute */
11
some-selector[myattr="value"] {}
12
13
/* Psuedo-class */
14
some-selector:psuedoclass {}

Combining Selectors (Combinators)

css-basic-layouts/Combinators.astro
1
<div class="a">
2
<div class="b">First child of A that is B</div>
3
<div class="b">Other B Child</div>
4
<div>
5
<div class="b">Nested B Child</div>
6
</div>
7
<div class="b">Another B Child</div>
8
<div class="c">Some other content in A</div>
9
</div>
10
11
<div class="b">B as a sibling of A</div>
12
13
<div class="b">Some other B sibling of A</div>
First child of A that is B
Other B Child
Nested B Child
Another B Child
Some other content in A
B as a sibling of A
Some other B sibling of A

Combinators

css-basic-layouts/Combinators.astro
1
<div class="a">
2
<div class="b">First child of A that is B</div>
3
<div class="b">Other B Child</div>
4
<div>
5
<div class="b">Nested B Child</div>
6
</div>
7
<div class="b">Another B Child</div>
8
<div class="c">Some other content in A</div>
9
</div>
10
11
<div class="b">B as a sibling of A</div>
12
13
<div class="b">Some other B sibling of A</div>
css-basic-layouts/combinators.css
1
/* Descendant */
2
.a .b { background-color: green; }
3
4
/* Child */
5
.a > .b { background-color: blue; }
6
7
/* Next-sibling */
8
.a + .b {background-color: red; }
9
10
/* Multiple */
11
.a, .b, .c { padding: 1rem; }

The Cascade

  • CSS stands for “Cascading Style Sheets”
  • The Cascade determines which specific styles get applied to an element
  • Specificity of a selector matters
  • Order of rules matter

Specificity

  • Used by browser to determine the most relevant declaration
  • Based on the selector used
  • Type of selector, combinators used, and level of impact this

Order

  • Where is the style in the stylesheet
  • Used to break ties in specificity

Calculated Specificity

Rules for specificity are pretty tricky, but your IDE should show this to you when hovering over a class

css-basic-layouts/Specificity.astro
1
<div id="a" class="a">
2
<div class="b">First child of A that is B</div>
3
<div class="b">Other B Child</div>
4
<div>
5
<div class="b">Nested B Child</div>
6
</div>
7
<div class="b">Another B Child</div>
8
<div class="c">Some other content in A</div>
9
</div>
10
11
<div class="b">B as a sibling of A</div>
12
13
<div class="b">Some other B sibling of A</div>
14
15
<blockquote>
16
Note that all content in A is now Green. The Selector with the higher
17
specificity takes priority even though it is defined earlier in the stylesheet
18
</blockquote>
css-basic-layouts/specificity.css
1
/*1,1,0*/
2
#a .b { background-color: green; }
3
4
/*0,1,0*/
5
.b { background-color: blue; }
6
7
/*0,2,0*/
8
.a + .b {background-color: red; }
9
10
.a, .b, .c { padding: 1rem; }

Psuedo-class Selectors

  • Used in addition to other selectors
  • Select an element based on DOM state

Some common Psuedo-classes

css-basic-layouts/Psuedo.astro
1
<label>
2
An Email Input
3
<input type="email" id="email" pattern=".+@.+\..+" size="30" required />
4
</label>
css-basic-layouts/psuedo.css
1
input {
2
color:black;
3
padding: 1rem;
4
font-size: large;
5
font-weight: bold;
6
border: 4px solid black;
7
outline: none;
8
}
9
10
input:focus {
11
background-color: plum;
12
}
13
14
input:hover {
15
background-color: lightblue;
16
}
17
18
input:invalid {
19
border-color: lightcoral;
20
}
21
22
input:valid {
23
border-color: aquamarine;
24
}

The Box Model

Rectangles all the way down

css-basic-layouts/BoxModel.astro
1
<div class="example">
2
<div class="before">before block</div>
3
<div class="box"><div class="content">content</div></div>
4
<div class="after">after block</div>
5
</div>
css-basic-layouts/box-model.css
9 collapsed lines
1
.example {
2
background-color: lightblue;
3
}
4
5
.before, .after {
6
padding: 10px;
7
background-color: lightgrey;
8
}
9
10
.content {
11
background-color: lightgrey;
12
height: 50px;
13
}
14
15
.box {
16
background-color: lightcoral;
17
border: 1rem solid lightgreen;
18
padding: 2rem;
19
margin: 3rem;
20
}

Flow - Block vs Inline Block

  • Default layout Mode
  • Block elements are placed top-to-bottom
  • Inline elements are placed left-to-right and wrap with reading width
  • Layout behaves and interacts differently
  • Sizing is calculated differently
  • Some properties will behave differently based on this
css-basic-layouts/inline-v-block.css
21 collapsed lines
1
.example {
2
background-color: lightblue;
3
}
4
5
.spacer {
6
padding: 10px;
7
background-color: lightgrey;
8
}
9
10
.content {
11
background-color: lightgrey;
12
height: 50px;
13
}
14
15
.block, .inline, .inline-block {
16
background-color: lightcoral;
17
border: .5rem solid lightgreen;
18
padding: 1rem;
19
margin: 1.5rem;
20
}
21
22
.block {
23
display: block;
24
}
25
26
.inline {
27
display: inline;
28
}
29
30
.inline-block {
31
display: inline-block;
32
}

Debugging

Firefox has the best CSS Tooling (in general)

  • Hover tooltips
  • CSS Overrides
  • HTML Breakpoints
  • Flex and Grid Tools
before block
content
after block

And if you’re still stuck

Well, you can just:

css-basic-layouts/DebugCSSButton.astro
1
<button onclick="body.classList.toggle('debug-mode')"
2
>Give Everything a Red Border</button
3
>
4
5
<style>
6
body.debug-mode * {
7
border: red 1px solid;
8
}
9
</style>

References and Further Reading

Anything by Josh W. Comeau pretty much

MDN

CSS Tricks