redundant in HTML5 + CSS? In an online tutorial, I was recently asked to create a class for navigation elements called "...">

Is <nav class = "nav"> redundant in HTML5 + CSS?

In an online tutorial, I was recently asked to create a class for navigation elements called "nav". I'm new to CSS, but is it just me, or is it redundant / confusing / bad practice?

+3


source to share


2 answers


This is not redundant. DOM element nav

is different from CSS class nav

.

If you want to style this element by class, you should use this style declaration (for example):

.nav { background-color : #F00; }

      

if it was styled on the element type it would be:

nav { background-color : #F00; }

      



It may sound trivial, but this period .

does matter. This means that you are identifying an element by its class, not by the element's name. If you are using class syntax (c .

) you can also write:

<div class="nav"></div>

      

This will display with a red background if you include a class definition, but not if you style the element directly.

In simple applications, you can get away with styling types (for example <nav>

) as opposed to classes (for example class="nav"

), but as you get more complex layouts, you will want to use classes.Also, if you are using a selector library such Like jQuery or document.querySelect()

, you might have good reasons for specifying a class.

If you can really know that all elements <nav>

can be styled the same across all of your pages, then anyway, just use an element selector, but to keep the flexibility it is best to use classes.

+1


source


NO is not redundant.

YES , it is redundant if you think that in your particular case you are ok withnav{ /*blaah blaah*/ }



<nav>

is an HTML5 semantic tag that represents SEO navigation. Navigation is everything you want. So, in case our page has multiple elements nav

and you're fine with targeting directly with the tag element using nav

, I'll be happy to see that.

+3


source







All Articles