更改每个偶数div中链接的颜色

时间:2017-04-22 13:17:10

标签: html css css-selectors pseudo-class

我有一组包含要显示的信息的div。 如何更改每个偶数容器内链接文本的颜色?



.container {
  height: 200px;
  margin-top: 5px;
}
.container:nth-child(even){
  background-color: green;
}

.container:nth-child(odd) {
  background-color: skyblue;
}

<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您喜欢这样,使用.container:nth-child(even)获取所有偶数容器的位置,然后添加a以定位链接.container:nth-child(even) a { color: red; }

.container {
  height: 200px;
  margin-top: 5px;
}
.container:nth-child(even){
  background-color: green;
}
.container:nth-child(odd) {
  background-color: skyblue;
}
.container:nth-child(even) a {        /*  added property  */
  color: red;
}
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>

答案 1 :(得分:1)

添加仅针对这些div的子项的CSS。

.container {
  height: 200px;
  margin-top: 5px;
}
.container:nth-child(even){
  background-color: green;
}

.container:nth-child(odd) {
  background-color: skyblue;
}

.container:nth-child(odd) a {
  color: green;
}

.container:nth-child(even) a{
  color: skyblue;
}
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>

答案 2 :(得分:0)

这是你想要的吗?

.container {
  height: 200px;
  margin-top: 5px;
}

.container:nth-child(even){
  background-color: green;
}

.container:nth-child(even) a{
  background-color: green;
  color: red; <!-- Change the Colour here -->
}

.container:nth-child(odd) {
  background-color: skyblue;
}
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>
<div class="container">
  Company website: <a href="abc.html">ABC</a>
  My Profile: <a href="user.html">User1</a>
  <button>Edit</button>
</div>