店頭に商品を陳列しております。各製品には専用の箱があります。ボックスをグリッドに配置し、1 行に 2 つ配置します。ただし、それらはカテゴリにも分類されるため、そのカテゴリに属する各ボックス セットの上に全幅の見出しを付けたいと考えています。ここに私のHTMLがあります:
<section class="product-listing">
<h2>Category 1</h2>
<section>first</section>
<section>second</section>
<section>third</section>
<h2>Category 2</h2>
<section>fourth</section>
<section>fifth</section>
<h2>Category 3</h2>
<section>sixth</section>
<section>seventh</section>
<section>eighth</section>
<section>ninth</section>
</section>
そしてCSS:
.product-listing {
width:410px; height:100%
background:#000;
}
.product-listing > section {
float:left;
width:200px;
margin:10px 10px 0 0;
background:#ff0000;
}
.product-listing > section:nth-of-type(2n+2){
margin-right:0;
}
そしてJSフィドル
ご覧のとおり、グリッドは 2 番目の h2 要素が導入されるまで正常に機能しますが、その後、最後のセットに奇数のボックスがある場合に 2n+2 が破棄されるため、崩壊します。基本的に私がする必要があるのは、各見出しの後に 2n+2 式をリセットすることです。
手っ取り早く汚い方法は、各ボックス セットを div でラップすることですが、不要なマークアップをページに導入することは避けたいと思います。
よりクリーンなソリューションはありますか?