omar ashraf omar ashraf - 3 months ago 6
CSS Question

When to put space between a tag name and a class or id

When should I be using this:

header.classname


and when should I use this:

header .classname


And what is the difference between the two?

Answer

header.classname mean you are targeting header having class as classname.

header .classname mean you are targeting the html element having class classname which is a child/descendent of header

1st case:

header.hclassname {
  background: turquoise;
}
<header class="hclassname">
  Lorem Ipsum
  <div class="divclassname">Dolor</div>
  Sit Amet
</header>

2nd case

header .divclassname {
  background: skyblue;
}
<header class="hclassname">
  Lorem Ipsum
  <div class="divclassname">Dolor</div>
  Sit Amet
</header>

Comments