最新のブラウザーでは、純粋な CSSnth-child
セレクターで十分です。
.post:nth-child(3n) { /* not zero-based */
margin-right: 0px;
}
http://jsfiddle.net/mblase75/EQacc/
最初の の前に他の兄弟要素がある場合は、代わりdiv.post
に使用する必要があります。nth-of-type
.post:nth-of-type(3n) {
margin-right:0px;
}
http://jsfiddle.net/mblase75/4vnjt/
古いブラウザー (IE8 以前を含む) の場合、JavaScript が必要になります。jQuery を使用して 3 つおきの項目にクラスを追加し、そのクラスを CSS に追加します。
.post.thirdchild, .post:nth-child(3n) {
margin-right: 0px;
}
jQuery:
$('.post:nth-of-type(3n)').addClass('thirdchild');
http://jsfiddle.net/mblase75/Tf9ky/
なぜ両方を含めるのですか?一部のユーザーは JavaScript をオフにしている可能性があります。
実際、IE < 8 について心配している場合、理想的な解決策は 0px をデフォルトの余白にすることです。これにより、投稿がコンテナーからオーバーフローする可能性がなくなります。
.post {
position: relative;
float: left;
margin-right: 0px;
}
.post.notthirdchild,
.post:nth-child(3n+1),
.post:nth-child(3n+2) {
margin-right: 10px;
}
JS:
$('.post:nth-child(3n+1),.post:nth-child(3n+2').addClass('notthirdchild');
http://jsfiddle.net/mblase75/nBLxc/