-1

Hey guys I am beginner at HTML and CSS and just faced the confusion when I try to choose the class being inside another class. That is, as you cann see in the code below I am trying to target .num1 class which is inside container class. The problem is why when I target .num1 directly nothing happens but when I target .container .num1{} all works as expected. Can't I just target num1 directly?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    .container{
        background: lightcoral;
       display: flex;
       justify-content: center;
       align-items: stretch;
    }
    .container>div{
        background: red;
        border:3px solid black;
        width: 150px;
        height: 150px;

    }

    .num1{
        height: 300px;
    }

    </style>
</head>
<body>
    <div class="container">
        <div class="item num1">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
    </div>
</body>
</html>
Andrew Kovalchuk
  • 705
  • 1
  • 6
  • 25
  • Go read up on _CSS Specificity_. `.container>div` has a higher specificity than `.num1`, so it “wins”. – 04FS Feb 26 '19 at 12:27

2 Answers2

2

You should use like this .container .num1

   .container{
        background: lightcoral;
       display: flex;
       justify-content: center;
       align-items: stretch;
    }
    .container div{
        background: red;
        border:3px solid black;
        width: 150px;
        height: 150px;

    }

    .container .num1 {
        height: 300px;
    }
 <div class="container">
    <div class="item num1">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
</div>

I hope this table helps.

enter image description here

doğukan
  • 14,001
  • 6
  • 27
  • 47
0

Read about css style priority https://www.thebookdesigner.com/2017/02/styling-priorities-css-for-ebooks-3/

.container{
   background: lightcoral;
   display: flex;
   justify-content: center;
   align-items: stretch;
}
.container>div{
    background: red;
    border:3px solid black;
    width: 150px;
    height: 150px;

}

.container .num1{
    height: 300px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <div class="item num1">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
    </div>
</body>
</html>
Andrew Kovalchuk
  • 705
  • 1
  • 6
  • 25