Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

Selectors

2m:18s

There are three types of CSS selectors you will encounter most regularly:

  1. Element selectors, these match all the elements of the same name on the page
p {
color: maroon;
}
  1. Class selectors, these match all the elements with the specified class attribute, e.g. <div class="example">
.example {
width: 75%;
}
  1. ID selectors, this matches the element with the specified id attribute, e.g. <ul id="gameList'>
#gameList {
list-style: cjk-heavenly-stem;
}

If you can recall the basic structure of a HTML document in the earlier chapter, you can see that HTML elements are nested. And there is no limit to how deep you can nest HTML elements. If you try looking at the source code for a complicated code base, like Facebook, for example, you’ll see a fairly deeply nested HTML structure.

<ul>
<li>4 large eggs</li>
<li>1/4 cup milk</li>
<li>2 tsp. butter</li>
</ul>
<ol>
<li>BEAT eggs, milk, salt and pepper in medium bowl until blended.</li>
<li>
HEAT butter in large non-stick skillet over medium heat until hot. POUR IN
egg mixture. As eggs begin to set, GENTLY PULL the eggs across the pan with
a spatula, forming large soft curds.
</li>
<li>
CONTINUE cooking – pulling, lifting and folding eggs – until thickened and
no visible liquid egg remains. Do not stir constantly. REMOVE from heat.
SERVE immediately.
</li>
</ol>

This structure is relevant in the context of CSS as well. Since elements can be children of other elements when they are nested within them, we can target them using descendent selectors. In the above example, we have two different lists. But each list uses li for the list items. If we use just li as the selector, the CSS we write will affect both lists. But if we only want to change the styles on the ingredients list at the top, and not the instructions list at the bottom, we can do this:

ul li {
color: maroon;
}

The selector list is read from right-to-left, with the left-most selector being the parent. If you were to translate the style rule above to plain English, it would read: select all the li elements within ul elements and make their text colour maroon.

Descendent selectors, together with the different categories of selectors we introduced earlier gives us a fairly wide range of combinations to target specific elements, or groups of elements depending on what we want to achieve.

Each combination will yield a different specificity value, which can be predictably calculated, once you understand how it works. Throughout the course, as you delve into different CSS properties, you will be introduced to more selectors in the context where they are used, to make it easier to understand how they work.