我知道如何使用JavaScript和PHP来做到这一点,但我正在尝试学习如何/是否可以仅使用CSS来实现。
我有一个容器,其中包含许多项。 每个项目都有一张图片,在下面是一个包含描述的
。 我希望每隔一个项目的背景与其他项目不同。
是否可能仅使用CSS实现这一点? 如果可以,怎么做? 我一直在尝试使用选择器。
.item:nth-child(odd){background-color:red}
.item .description:nth-of-type(odd){background-color:orange;}
我似乎无法理解它。欢迎任何建议、评论或意见。
下面是一些简化的示例代码,演示了我的情况。
<style>
#container{width:100% height:100%;}
.item {float:left; width:250px; height:700px;}
.item img {width:250px; height:250px; float:left;}
.description {width:250px; height:450px; float:left; background-color:blue;}
.description:nth-of-type(even){background-color:red;} // <- Here's the line!!
</style>
<html>
<body>
<div id="container">
<div class="item"> //item 1
<img src="image.jpg"/>
<div class="description"> //This (and every odd number) I want to be blue
<h1>Title</h1>
<h2>Sub Title</h2>
<p>Lorem Ipsum dolor sit for Adun!</p>
<a href="#">::LEARN MORE::</a>
</div>
</div>
<div class="item"> //item 2 and so on...
<img src="image.jpg"/>
<div class="description"> //and this (and every even number, red)
<h1>Title</h1>
<h2>Sub Title</h2>
<p>Lorem Ipsum dolor sit for Adun!</p>
<a href="#">::LEARN MORE::</a>
</div>
</div>
</div>
<body>
</html>