【发布时间】:2021-12-28 22:25:36
【问题描述】:
我只想选择前两个嵌套的<img/> 元素并设置width: 200px; 或其他东西。我将如何使用组合器而不是给它们自己的类来选择它们?
我尝试过使用nth-child(-n+2),但由于每个<img/> 都嵌套在不同的<div> 中,我认为它试图在每个<div> 中选择<img/> 的前两个实例而不是<section> 中的前两个 <img/> 实例。我仍在学习组合器,因此非常感谢您对答案的解释。
我的代码如下所示:
.section-container div {
background-color: skyblue;
}
.section-container div img {
width: 100px;
height: 100px;
background-color: red;
}
<body>
<header>
<!--Navbar stuff-->
<h1>I'm the navbar</h1>
</header>
<section class="section-container">
<h2>subheading</h2>
<div>
<img src="image 1"/>
<p>1st div</p>
</div>
<div>
<img src="image 2"/>
<p>2nd div</p>
</div>
<div>
<img src="image 3"/>
<p>3rd div</p>
</div>
<div>
<img src="image 4"/>
<p>4th div</p>
</div>
</section>
<form>
<h2>Form</h2>
<label for"name-input">Name*</label>
<input id="name-input" type="text"></input>
<button>Submit</button>
</form>
</body>
【问题讨论】:
标签: html css asp.net asp.net-mvc css-selectors