DEV Community


Discussion on: Stop using so many divs! An intro to semantic HTML

brycole profile image
Bryan Cole

Hi Ken. This article was really great. I'm super new to HTML/CSS (less then a week!) and I found it really informative and easy to read. I'm currently taking Le Wagons Full-Stack Web Developer program. (Part time!)

The only confusing part for me about the article is how to write the CSS code to style these new sections. Maybe a quick primer or best practices link for for CSS styling based on this HTML?

For example... if you styled the h1 tag would that cover the h1 tag in and ?

And if you wanted to do the h1 tag in main only you it would be something like .main .article .header . h1? Does this add a lot of complexity to the CSS code?


kenbellows profile image
Ken Bellows Author

Hey Bryan! Great questions, very fundamental to understand how CSS interacts with HTML. I won't be able to fully answer in a comment, because you've really struck at the heart of CSS selectors and how they work and I really recommend going through a full CSS tutorial for that, but I'll try to cover the main points.

The most important thing to remember is that CSS selectors are greedy, in a technical sense, meaning that they try to grab as much as they can. Anything that could possibly fit within the definition of a selector will. So if you write this block of CSS:

h1 {
  color: red;

then every <h1> tag on the whole page will be turned red, except where you define other blocks with more specific selectors that override the color, such as:

main h1 {
  color: blue;

This is a core term to know in CSS: specificity. Emma Wedekind wrote a great article going over it, which I highly recommend: CSS Specificity. But the basic idea is this: More specific selectors are stronger. main h1 beats h1 because it's more specific. h1 says, "modify all <h1> tags"; main h1 says, "modify all <h1> tags within <main>". More specific, so stronger. (Emma's article covers the details on exactly how specificity is determined for a selector, so check that out.)

To answer your specific cases:

Yes, an h1 selector applies to every <h1> on the page. Though remember that any properties can be overridden in specific cases by more specific selectors.

You're mostly correct about styling that specific <h1>, but remember that .main is not the same as main: the dot prefix refers to HTML classes, so if you used the selector .article, that would not match <article>, but would match <div class="article">. The better way to style the <h1> in this case:


would be like this:

  main article header h1 {
    font-size: 36px;

Hope that helps! As for best practices, that's an even bigger question, and there are a thousand different opinions out there. To be honest, I wouldn't worry about best practices and conventions for now; focus on learning how CSS itself actually works at a deep level before you get into the subjective stuff.

Hope this helps!