Ich versuche, den Stil ungerader Divs innerhalb eines Divs zu ändern. Wenn sichnth-of-type(odd)
in einem anderen Div befindet, wirkt sich das aus irgendeinem Grund auf alle meine Divs aus. Hier ist mein Code für reguläre Divs und ungerade Divs:
.video-entry-summary { width: 214px; height: 210px; margin-left: 10px; float: left; position: relative; overflow: hidden; border: 1px solid black; } .video-entry-summary:nth-of-type(odd) { width: 214px; height: 210px; margin-left: 0px; float: left; position: relative; overflow: hidden; border: 1px solid black; background: #ccc; }
video 1video 2video 3video 4
Aus irgendeinem Grund funktioniertnth-of-type
nicht, wenn es in meine Divs eingepackt ist, aber es funktioniert, wenn sie nicht in irgendwelche Divs eingewickelt sind.
Arbeitsversion, wenn nicht in ein Div eingeschlossen:
.video-entry-summary { width: 214px; height: 210px; margin-left: 10px; float: left; position: relative; overflow: hidden; border: 1px solid black; } .video-entry-summary:nth-of-type(odd) { width: 214px; height: 210px; margin-left: 0px; float: left; position: relative; overflow: hidden; border: 1px solid black; background: #ccc; }
video 1video 2video 3video 4
Wie kann ich dafür sorgen, dass der ursprüngliche Code wie oben funktioniert?
:nth-of-type()
与:nth-child()
类似,因为它们必须都来自同一个父级。如果您需要这些包装器div
,请在这些包装器上使用:nth-of-type()
:如果所有同级都是
.post
,请使用:nth-child()
以避免与:nth-of-type()
的真正含义: