简体   繁体   English

如何使用nth-child属性更改背景图像?

[英]How to change background-image with nth-child property?

I have the following html code. 我有以下html代码。 From row onwards it is repeated 2 more time inside the class contenedor, only thing that changes is the href url and de class in i(an icon). 从行开始,将在类竞争器中重复2次以上,唯一更改的是i(图标)中的href url和de class。

<div class="contenedor">
<div class="row">
        <div class="col-lg-12">
            <center>
                <div class="zoom espacio">
                    <div class="contorno">
                        <a href="carta.html">
                            <i class="fas fa-shopping-bag fa-5x"></i>
                        </a>
                    </div>
                </div>
                <p>Prueba</p>
            </center>
        </div>
    </div>

<!–– Second time––>
<!–– Third time ––>
</div>

And this CSS. 还有这个CSS。 What it does is that when I hover over the element I change the background image of contorno. 它的作用是,当我将鼠标悬停在元素上时,将更改contorno的背景图像。 I would like to change the image of the 3 elements individually with the nth-child property: 我想使用nth-child属性分别更改3个元素的图像:

.contorno:hover{
background-color: none;
background-size: cover;
    background-image: url("someURL");
}

And this is what I've tried but it's not working: 这是我尝试过的,但是没有用:

.contenedor .row .col-lg-12 .zoom .espacio .contorno:hover:nth-child(1){
background-image: url("someURL");
}

Thank 4 the help. 谢谢4的帮助。

You would need to select the nth-child of .contenedor which is your .row . 您需要选择.contenedornth-child .contenedor ,即.row

.contenedor .row:nth-child(1) .col-lg-12 .zoom.espacio .contorno:hover {
  background-image: url("someURL");
}

 .contenedor .row:nth-child(1) .col-lg-12 .zoom.espacio .contorno:hover { color: red; } .contenedor .row:nth-child(2) .col-lg-12 .zoom.espacio .contorno:hover { color: blue; } .contenedor .row:nth-child(3) .col-lg-12 .zoom.espacio .contorno:hover { color: green; } 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="contenedor"> <div class="row"> <div class="col-lg-12"> <center> <div class="zoom espacio"> <div class="contorno"> <p> First </p> </div> </div> </center> </div> </div> <div class="row"> <div class="col-lg-12"> <center> <div class="zoom espacio"> <div class="contorno"> <p> Second </p> </div> </div> </center> </div> </div> <div class="row"> <div class="col-lg-12"> <center> <div class="zoom espacio"> <div class="contorno"> <p> Third </p> </div> </div> </center> </div> </div> </div> 

Also your selector was incorrect. 另外,您的选择器不正确。 The .zoom .espacio should be .zoom.espacio because .espacio is not nested inside .zoom ... they are the same element. .zoom .espacio应该是.zoom.espacio因为.espacio不嵌套在.zoom ...它们是同一元素。

I would also recommend removing the <center> tag because it is deprecrated. 我还建议删除<center>标记,因为它已被废弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM