Does any one know how I can target a div, and style it, when it is in another div and it has no id or class.
<div class="wrapper">
<div>
</div>
<div>
</div>
<div>
</div>
</div>
How do I target div two for example.
Does any one know how I can target a div, and style it, when it is in another div and it has no id or class.
<div class="wrapper">
<div>
</div>
<div>
</div>
<div>
</div>
</div>
How do I target div two for example.
Note the > is necessary, otherwise it will target any descendent divs (grandchildren, etc) instead of just direct children.
nth-child
allows you to select specific items. There is alsofirst-child
andlast-child
for selecting the first/last item.The number 2 can also be a function like
3n+0
. This selects every third item starting from 0. Check out this codepen for an example.Last but not least, you also have
nth-of-type(2)
. This will change every second item every time the same pattern appears. I have also included an example in the same codepen.Descendant Selectors are the Worst, not only is this not performant but it is fragile, as changes to the HTML can easily break your CSS. It is preferable to simply create a new CSS class selector.
But if you can't modified the html:
if there is space in selector, it searches for children elements with tag
div
INSIDE element with given class.wrapper
Check this out fore more information. http://modernweb.com/2013/08/12/writing-better-css/?utm_source=html5weekly&utm_medium=email
Whatever number you pass into the nth-child targets #wrappers specific children. So the second div in your case.
Reference: http://www.w3schools.com/cssref/sel_nth-child.asp