html - Div inside Div stripe not working -
i trying create striped divs similar stripe spreadsheet. working on creating format relies on table created divs.
when trying implement either nth-child
or nth-of-type
, keep getting broken output. can me? have been trying find solution no avail.
this code.
html
<div> <div class="tab1"> 1 <div class="tab2"> 2 </div> </div> </div> <div> <div class="tab1"> 3 <div class="tab2"> 4 </div> </div> </div>
on repeat...
css
.tab1 { background-color: red; } .tab2 { background-color: orange; } div:nth-of-type(odd) .tab1 { background-color: green; } .tab1:nth-child(odd) .tab2{ background-color: yellow; }
edit: should returning green, yellow, red orange color stripes in order.
if can me scale well, nice.
if wanted expand , add div, such as
<div> <div class="tab1"> 1 <div> 2 </div> </div> </div>
how change syntax? div:nth-child(odd) > .tab1 tab2
as said in comment .tab1:nth-child(odd)
odd-numbered, because .tab1 element in level.
so have use css:
div:nth-child(odd) > .tab1 .tab2{ background-color: yellow; }
have look:
.tab1 { background-color: red; } .tab2 { background-color: orange; } div:nth-of-type(odd) .tab1 { background-color: green; } div:nth-child(odd) > .tab1 .tab2{ background-color: yellow; }
<div> <div class="tab1"> 1 <div class="tab2"> 2 </div> </div> </div> <div> <div class="tab1"> 3 <div class="tab2"> 4 </div> </div> </div><div> <div class="tab1"> 1 <div class="tab2"> 2 </div> </div> </div> <div> <div class="tab1"> 3 <div class="tab2"> 4 </div> </div> </div><div> <div class="tab1"> 1 <div class="tab2"> 2 </div> </div> </div> <div> <div class="tab1"> 3 <div class="tab2"> 4 </div> </div> </div>
edit
after edited question, code following:
.tab1 { background-color: red; } div:nth-child(even) > .tab1 > div { background-color: orange; } div:nth-of-type(odd) .tab1 { background-color: green; } .tab1:nth-child(odd) > div { background-color: yellow; }
<div> <div class="tab1"> 1 <div> 2 </div> </div> </div> <div> <div class="tab1"> 3 <div> 4 </div> </div> </div>
Comments
Post a Comment