我有一组包含要显示的信息的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;
答案 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>