0

I used to write a simple code as:

.grandparent {}
.parent {}
.child {}

with

<div class="grandparent">
    <div class="parent">
        <div class="child">
        </div>
    </div>
</div>

But with a use of selectors now, the above code could be written as:

.name {}
.name > div {}
.name > div > div {}

Now, sorry if this sounds dumb, but what is the difference between these two codes? When should I use class names and when selectors? Is there any difference in speed performance? Ty

darkchampionz
  • 932
  • 2
  • 13
  • 33
  • they don't have the same specificity to start with and this is an important fact that make them different. They also don't select the same elements. – Temani Afif Jan 02 '20 at 22:24
  • This is not only about specificity. classnames are useful when you want to have a property than you can apply to any element. For instance, you could have a `red` className that will give the color red to your element. You need this if an element has multiple children that do not have the same color. However, if you want all children to behave the same way, it's easier to use children selectors. – Axnyff Jan 02 '20 at 22:26

0 Answers0