1

Whats the difference of use CSS like this:

.mainHeader nav ul li {.....

compared with just this:

.mainHeader li {.....

It works fine with just the latter alternative. Since I don't have any other nav or ul in the mainHeader, I guess it's ok to just use the latter one?

3
  • 3
    Specificity, and precision. Commented Nov 2, 2013 at 12:59
  • Have a look at this fiddle. might help you to understand better. Commented Nov 2, 2013 at 13:13
  • 1
    The title of your question should reflect the specific problem you are facing, and the body of the question should state the problem clearly. If you think “it works fine”, what is the problem? (The two selectors are not equivalent, and whether they match the same elements depends on the HTML document.) Commented Nov 2, 2013 at 13:27

4 Answers 4

2

What if you have HTML like this?

<div class="mainHeader">
  <nav>
    <ul>
      <li>Menu item</li>
      <li>Menu item
        <ul><li>With submenu</li></ul>
      </li>
    </ul>
  </nav>
</div>

Now, if you wanted to only style a "Menu item" and submenu items separately, the only way to do so specifically is with the following selectors:

.mainHeader nav>ul>li { /* menu item */ }
.mainHeader li>ul>li { /* submenu item */ }

Using the > combinator is important here, to ensure you are styling the right element. .mainHeader li alone will not do.

Sign up to request clarification or add additional context in comments.

5 Comments

OK, then I guess it's better to stick to the first alternative in my question?
Yes, but ask yourself if you should be adding > combinators. I'm pretty sure they're more efficient than "space" too, because they only have to check up one level.
@NiettheDarkAbsol [not related to this post] Just a doubt.. which one in > and space would be faster if there is only one level?
>, because there's always more than one level (html, body, ...)
@NiettheDarkAbsol Sorry, I disagree. It is a browser implementation detail but a recursive lookup in a depth-1 structure should not be noticeable slower than an iterative lookup in the same structure.
2

As long as you will never include any other matching elements, it's okay (where okay means "it will work"). A good approach is to add a class to your ul and select it that way:

ul.my-menu li {
  /* CSS styles */
}

And - by the way - I guess mainHeader is not the tag name. If it is an identifier, you must use #mainHeader and .mainHeader if it is a class. (You changed it)

2 Comments

I think it is not a good practice to give a prefix like ul to a class name.
I agree, but I want to point out that the class applies to the ul.
0
<div id="mainHeader">
 <ul><li>facebook</li><li>twiiter</li></ul>
   <div id="nav">
     <ul><li>Home</li><li>About</li><li>Information</li><li>Contact</li></ul>
   </div>
</div>

So #mainHeader li{....} will do all li in div and #mainHeader nav ul li {....} will overwrite for the nav bar

Adding a class to each ul or adding > will make the code stronger when it is edited in future like suggested above.

Comments

0

The difference is only one thing, you can list any type of element next to .mainHeader for example, #mainHeader a p code div nav span ul li. This will give all of these elements with an ID of mainHeader the CSS you place in the { } for that element.

I'll give you an example.

HTML:

<div class="mainHeader">This text is black because "mainHeader".</div>
<a class="mainHeader" href="#">This text is black because "mainHeader".</a>
<p class="mainHeader">This text is black because "mainHeader".</p>
<nav class="mainHeader">This text is black because "mainHeader".</nav>
<span class="mainHeader">This text is black because "mainHeader".</span>

CSS:

.mainHeader div a p nav span {
    color: #000;
}

Update(1): Please understand that doing this is recommended if you are going to give multiple elements the same aspect for a specific thing. An example of this usage, say you want div a p to have the same color, you would achieve this by div a p { color: #000; /* color your wanted */ }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.