Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction  ›  Lesson

HTML — The Structure CSS Styles

HTML 12 minCSS reaches elements only through hooks the HTML provides: element names, classes, ids and their position in the document tree.

HTML — The Structure CSS Styles

Imagine being asked to "make the prices green" on a café's website. Before you can write a single line of CSS, you need to know where the prices are, what they are called, and what they sit inside. CSS never invents elements. It dresses the ones HTML already built.

The rule

CSS can only style what the HTML has already named.

Every CSS rule starts by pointing at elements. It can point using three kinds of hook that live in the HTML:

  • the element name, such as h2 or p
  • a class, such as class="price"
  • an id, such as id="menu"

It can also use position in the tree: an element's parent, its children and its siblings. So the HTML is not just content; it is the map CSS reads.

Here is the page we will style for the whole course, the home page of a small café:

<body>
  <header id="top">
    <h1>Harbour Café</h1>
    <nav class="site-nav">
      <a href="#menu">Menu</a>
      <a href="#visit">Visit</a>
    </nav>
  </header>
  <main>
    <section id="menu">
      <h2>Today's menu</h2>
      <article class="dish">
        <h3>Masala omelette</h3>
        <p class="price">₹180</p>
        <p>Three eggs, green chilli and coriander.</p>
      </article>
      <article class="dish special">
        <h3>Mango lassi</h3>
        <p class="price">₹120</p>
        <p>Thick, cold and only in summer.</p>
      </article>
    </section>
    <section id="visit">
      <h2>Visit us</h2>
      <p>Open 8 am to 6 pm, every day.</p>
    </section>
  </main>
  <footer>
    <p>Harbour Café, Marine Drive</p>
  </footer>
</body>

Read it as a family tree. body has three children: header, main and footer. Each article is a child of section#menu and the parent of an h3 and two paragraphs.

The shortcut

Before styling anything, find it, name its hooks, name its parent. Three questions, answered straight from the HTML, tell you every way CSS could reach an element.

Finding the lassi's priceStep 1 of 4
  1. Find it

    The price ₹120 sits in a p inside the second article.

The common mistake is writing CSS for something the HTML does not contain, such as styling a "price" before any element carries that class. If there is no hook, add one in the HTML first.

Drills

Answer fast — trust the rule.

quick drill

Which element is the parent of <h3>Masala omelette</h3>?

quick drill

How many classes does the Mango lassi article carry?

quick drill

The owner wants a phone number shown under the opening hours. Where does that change start?

quick drill

Which of these is not a hook CSS can use to reach an element?

Remember

  • CSS only styles elements the HTML already contains.
  • Find it, name its hooks (element, class, id), name its parent.
  • Missing hook? Fix the HTML first; do not fight it with CSS.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFinding the hooks

In the café page, the owner wants the footer text to look quieter than the rest. Describe the element you would style, the hooks it offers, and its parent, and say whether you would need to change the HTML first.