Is it preferable to select a class element or <nav>?

They told me several times not to select elements such as <nav> and <body> , and to use the class selector instead. For example, "the <nav> not intended to be displayed / stylized, but it must explicitly separate the navigation section." But does this not strike a part of the goal of semantic elements?

Assuming that I have only one bullet navigation list, and I am making them horizontal by adding a background, etc., can you still select the nav element above the class?

  <nav class="navigation"> <ul class="links"> <li><a href="">Shop</a></li> <li><a href="">Discounts</a></li> <li><a href="">Profile</a></li> <li><a href="">Blog</a></li> </ul> </nav> 

And, if you are going to vote for me, please give an explanation, and not just assume that I can understand why you did it.

+6
source share
2 answers

Adding a class simplifies page expansion later. A class must carry semantic meaning on its own.

If you only select elements and then add more elements of the same type later, you still have to add classes.

+6
source

Classes have more specificity than an element selector. So this is for you when to use it.

Assume this html:

 <nav class="nav"></nav> <ul class="nav"></ul> <nav class="footer-nav"></nav> 

Now how do you use css style for <nav class="nav"></nav> ?

.nav{...} ? No, there are two classes with navigation.

Next: nav{} ? No, there are two navigational elements.

So how do you support?

Example: nav.nav{} Now it selects the element that we need.

So, all about the elements we want to select.

So, it completely depends on the elements that you have on your page.

+1
source

Source: https://habr.com/ru/post/978874/


All Articles