伙计们,我是HTML和CSS的初学者,当我尝试选择另一个类中的类时,只是遇到了困惑。也就是说,正如您在下面的代码中看不到的那样,我正在尝试定位位于容器类内部的.num1类。问题是为什么当我直接定位.num1时,什么都没发生,而是定位.container .num1 {}时,一切正常。我不能直接定位num1吗?
<!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>
答案 0 :(得分:2)
您应该这样使用.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>
希望此表对您有所帮助。
答案 1 :(得分:0)
了解CSS样式优先级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>