在我的代码中,我尝试访问a
链接以使每个链接都具有不同的颜色。我使用了nth-child
或:eq
但不起作用。
我有一个div包装器,在3个div里面每个都有一个链接。我想在offer div包装器中选择一个带有数字的链接。
请有人告诉我我选择了错误的选择器还是CSS错误。
.offers-box a:nth-of-type(1) {
color: #3F265F;
}
.offers-box a:nth-of-type(2) {
color: #F04B5B;
}
<div class="offers m-t-50 m-b-15">
<div class="row">
<div class="col-md-4 ">
<div class="offers-box">
<div class="row">
<div class="col-md-12">
<h3>OFFRE STANDARD</h3>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12">
<h5>Votre activite & commerce est optimises</h5>
<ul>
<li>Inclus:</li>
<li>- Vitrine commerciale</li>
<li>- Systeme de commerce</li>
<li>- Systeme de reservation</li>
<li>- Annonces commerciale "SEO"</li>
<li>- Passerelle multidifusion auto: xml, etc</li>
<li>- Passerelle multidifusion immo: idx, xml, etc</li>
</ul>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12 text-center offers-button">
<a href="#" title="Click here">DECOUVRIR L'OFFRE</a>
</div>
</div>
<!--row-->
</div>
<!--offers-box-->
</div>
<!--col-md-4-->
<div class="col-md-4 ">
<div class="offers-box">
<div class="row">
<div class="col-md-12">
<h3>OFFRE CORPORATE</h3>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12">
<h5>Votre activite & commerce est optimises</h5>
<ul>
<li>Inclus:</li>
<li>- Vitrine commerciale</li>
<li>- Systeme de commerce</li>
<li>- Systeme de reservation</li>
<li>- Annonces commerciale "SEO"</li>
<li>- Passerelle multidifusion auto: xml, etc</li>
<li>- Passerelle multidifusion immo: idx, xml, etc</li>
</ul>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12 text-center offers-button">
<a href="#" title="Click here">DECOUVRIR L'OFFRE</a>
</div>
</div>
<!--row-->
</div>
<!--offers-box-->
</div>
<!--col-md-4-->
<div class="col-md-4 ">
<div class="offers-box offers-last-box">
<div class="row">
<div class="col-md-12">
<h3>COMMUNICATIONS STANDARD TELEPHONIQUE</h3>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12">
<p><span class="blue-color">Le portail unifie</span> de vos communications d'entreprises</p>
<img src="/images/image.png" class="img-responsive" alt="Responsive image">
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12 text-center offers-button">
<a href="#" title="Click here">DECOUVRIR L'OFFRE</a>
</div>
</div>
<!--row-->
</div>
<!--offers-box-->
</div>
<!--col-md-4-->
</div>
<!--row-->
</div>
<!--offers-->
最佳答案
具有.offers-box
类的div不是兄弟姐妹,因此您不能使用nth-child
或first-child
等。.offers-box:nth-of-type()
不起作用,因为使用:nth-of-type(1)
将选择所有.offers-box
,因为具有该类的每个元素都是其父元素的唯一子元素。 .offers-box
是col-md-4
的唯一子级
与.offers-box a:nth-of-type()
相同,每个a
是a
div中唯一的.offers-box
三个.col-md-4
是siblings
,并且是同一父级children
的唯一.offers > .row
,因此您可以使用它们。
每个.col-md-4
中都有一个.offers-box
,因此请使用以下代码来实现所需的功能。
让我知道是否有效
.col-md-4:first-child .offers-box a {
color: red;
}
.col-md-4:nth-child(2) .offers-box a {
color: green;
}
<div class="offers m-t-50 m-b-15">
<div class="row">
<div class="col-md-4 ">
<div class="offers-box">
<div class="row">
<div class="col-md-12">
<h3>OFFRE STANDARD</h3>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12">
<h5>Votre activite & commerce est optimises</h5>
<ul>
<li>Inclus:</li>
<li>- Vitrine commerciale</li>
<li>- Systeme de commerce</li>
<li>- Systeme de reservation</li>
<li>- Annonces commerciale "SEO"</li>
<li>- Passerelle multidifusion auto: xml, etc</li>
<li>- Passerelle multidifusion immo: idx, xml, etc</li>
</ul>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12 text-center offers-button">
<a href="#" title="Click here">DECOUVRIR L'OFFRE</a>
</div>
</div>
<!--row-->
</div>
<!--offers-box-->
</div>
<!--col-md-4-->
<div class="col-md-4 ">
<div class="offers-box">
<div class="row">
<div class="col-md-12">
<h3>OFFRE CORPORATE</h3>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12">
<h5>Votre activite & commerce est optimises</h5>
<ul>
<li>Inclus:</li>
<li>- Vitrine commerciale</li>
<li>- Systeme de commerce</li>
<li>- Systeme de reservation</li>
<li>- Annonces commerciale "SEO"</li>
<li>- Passerelle multidifusion auto: xml, etc</li>
<li>- Passerelle multidifusion immo: idx, xml, etc</li>
</ul>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12 text-center offers-button">
<a href="#" title="Click here">DECOUVRIR L'OFFRE</a>
</div>
</div>
<!--row-->
</div>
<!--offers-box-->
</div>
<!--col-md-4-->
<div class="col-md-4 ">
<div class="offers-box offers-last-box">
<div class="row">
<div class="col-md-12">
<h3>COMMUNICATIONS STANDARD TELEPHONIQUE</h3>
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12">
<p><span class="blue-color">Le portail unifie</span> de vos communications d'entreprises</p>
<img src="/images/image.png" class="img-responsive" alt="Responsive image">
</div>
</div>
<!--row-->
<div class="row">
<div class="col-md-12 text-center offers-button">
<a href="#" title="Click here">DECOUVRIR L'OFFRE</a>
</div>
</div>
<!--row-->
</div>
<!--offers-box-->
</div>
<!--col-md-4-->
</div>
<!--row-->
</div>
<!--offers-->
关于css - CSS nth-child()和:eq()在我的代码中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38875747/