2 replies [Last post]
gautamz07
gautamz07's picture
Offline
Enthusiast
Last seen: 3 years 29 weeks ago
Timezone: GMT+5.5
Joined: 2014-04-24
Posts: 265
Points: 403

Now of ed if i declare something like

p ~ ul

what this is really doing is "Selecting every P that precedes a ul" , now that was pretty simple to understand and sounded easy to implement until i came across This Fiddle and well if you do have a look at it , you will see that the final list is precedeed by a "" , now why is it applying the selection to the "ul preceeded by a h2" when it should only be applying the selection to a "ul preceeded by a p" ?

Thank you .

regards .

gautam.

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 1 week 1 day ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5342
Points: 2963

Hi gautamz07, The second UL

Hi gautamz07,
The second UL is still preceded by the P.
It's called a general selector so it targets all elements on the same level, siblings that come after the P.
To get the result you were expecting try Adjacent sibling selector +

gautamz07
gautamz07's picture
Offline
Enthusiast
Last seen: 3 years 29 weeks ago
Timezone: GMT+5.5
Joined: 2014-04-24
Posts: 265
Points: 403

reply

Tony i am aware of adjacent selectors , and after reading a bit more of ~ selector i got the reason why , the second set of "UL" tags were selected too . Now my question is whats the practical use of something like ~ selector , would't it be dangerous to select something like that ? unless ur absolutely certain you want everything thats preceded by ~ to be selected , and that would be a rare case scenario . Is it practically even used ?