It appears that there may be a misunderstanding regarding the functionality of the :nth-of-type
selector. The .box selector does not restrict the :nth-of-type selector; instead, it acts as an additional subfilter. In this scenario, all odd divs are initially selected, followed by styling only those odd divs with the box class.
In the provided example, the odd divs identified are:
<div>
<div class="box">1</div> <!-- Odd -->
<div class="box">2</div>
<div class="box">3</div> <!-- Odd -->
<div class="box">4</div>
<div class="splitter">splitter</div> <!-- Odd -->
<div class="box">1</div>
<div class="box">2</div> <!-- Odd -->
<div class="box">3</div>
<div class="splitter">splitter</div> <!-- Odd -->
<div class="box">1</div>
<div class="box">2</div> <!-- Odd -->
<div class="box">3</div>
<div class="box">4</div> <!-- Odd -->
<div class="box">5</div>
<div class="box">6</div> <!-- Odd -->
<div class="box">7</div>
<div class="box">8</div> <!-- Odd -->
</div>
Within these odd divs listed above, only the following meet the additional filter criteria by also containing the class box:
<div>
<div class="box">1</div> <!-- Odd w/ class box -->
<div class="box">2</div>
<div class="box">3</div> <!-- Odd w/ class box -->
<div class="box">4</div>
<div class="splitter">splitter</div>
<div class="box">1</div>
<div class="box">2</div> <!-- Odd w/ class box -->
<div class="box">3</div>
<div class="splitter">splitter</div>
<div class="box">1</div>
<div class="box">2</div> <!-- Odd w/ class box-->
<div class="box">3</div>
<div class="box">4</div> <!-- Odd w/ class box -->
<div class="box">5</div>
<div class="box">6</div> <!-- Odd w/ class box -->
<div class="box">7</div>
<div class="box">8</div> <!-- Odd w/ class box-->
</div>
These elements fulfill the specified requirements by also having the box class.
For instance:
Here is an approach to achieve this using various :nth-child
selectors for creating CSS ranges. This method can be beneficial if modifying the HTML structure is not feasible, as suggested in other responses:
.box {
width: 48%;
float: left;
height: 30px;
background: #ccc;
}
.box:nth-child(n+1):nth-child(odd):nth-child(-n+4),
.box:nth-child(n+6):nth-child(even):nth-child(-n+8),
.box:nth-child(n+10):nth-child(even):nth-child(-n+17),
.box:nth-child(n+19):nth-child(odd):nth-child(-n+21){
margin-right: 4%;
background-color: red;
}
.splitter {
width: 100%;
float: left;
}
<div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="splitter">splitter</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="splitter">splitter</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="splitter">splitter</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
Please note: The provided markup is slightly more comprehensive than what was originally outlined in your question. It is based on the full jsfiddle markup rather than the abbreviated version provided.
Here is your updated fiddle incorporating the aforementioned changes.