Difference between concatenating id and html tag ie ul # nav vs #nav ul

I just played with css ul for the menu. My starting css

#nav ul {...}

doesn't work but

ul#nav {...}

worked. What is the reason for this?

Another similar question is what is the difference between:

div.grey

OR

div .grey

Note the gap between the two ...

+3
source share
3 answers

#nav ulSearches for an element ulinside the element with the identifier "nav".

<div id="nav">
  <ul>
    <li>Like me</li>
  </ul>
</div>

ul#navsearches ulthat has the id value of "nav".

<ul id="nav">
  <li>Like me</li>
</ul>

The same applies to classes:

div.greySearches for an element divthat has the class name gray.

<div class="grey">Foo</div>

While div .greylooking for any element that has the class name "gey" within div.

<div>
  <p class="grey">
    I'm special!
  </p>
</div>
+6
#nav ul {...}

ul id nav

ul#nav {...}

ul id nav

id no .

div.grey

div

div .grey

grey div

. Selectors

+2

...

#nav ul {...} means ul inside an element having id nav

ul#nav means ul element having id nav

The same applies to the class examples that you showed.

See CSS Selectors in W3C

0
source

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


All Articles