Home > database >  Why does :nth-child(1) don't change the height and width?
Why does :nth-child(1) don't change the height and width?

Time:10-02

I can change and apply the left and top position values of the img tag using the nth-child selector but the height and width values are not getting applied. I know that the nth-child is selecting correctly, but why is the height and width not getting applied.

I was working on a scss project and then this same issue happened. So I tried to recreate it with just plain HTML and CSS. And it happens again.

Someone please explain what I did wrong here. Thank you.

Note: I really need to make the nth-child() selector work here!

.home {
    position: absolute;
}

.home:nth-child(1) {
    height: 41.8rem;
    left: 9.4rem;
    top: 16.4rem;
    width: 27.8rem;
}

  
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="home">
        <img src="https://imgsv.imaging.nikon.com/lineup/dslr/df/img/sample/img_02_l.jpg" alt="">
    </div>
</body>
</html>

CodePudding user response:

You need add space for your selector .home :nth-child(1)

Example, normally if you write as .home:hover it mean div .home when hover will have property. With space it mean the child in .home

.home {
    position: absolute;
}

.home :nth-child(1) {
    height: 40px;
    left: 9.4rem;
    top: 16.4rem;
    width: 27.8rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="home">
        <img src="https://imgsv.imaging.nikon.com/lineup/dslr/df/img/sample/img_02_l.jpg" alt="">
    </div>
</body>
</html>

  • Related