html - 图片 :first-child not working for my first image

标签 html css image

我已经尝试过 #logos img:first-child{} 尝试将我的顶级 img 格式化为与其他的略有不同,但它不起作用。谁能帮我弄清楚为什么?

<div id="logos" name="logos">
    <div class="container">
        <div class="row">
            <br>
            <h1 class="centered">SHOWS</h1>
            <img src="img/kenshows400x300.jpg" class="img-responsive" alt="Ken Cooper" width="400px" height="300px">
            <hr>
        </div><!-- end row -->
        <div class="row">
            <div class="col-lg-6">
                <img src="img/shows/jkllogo450x300.jpg" class="img-responsive" alt="Jimmy Kimmel Live" width="450px" height="300px">
            </div>
            <div class="col-lg-6">
                <img src="img/shows/latelateshowlogo450x300.jpg" class="img-responsive" alt="Late Late Show" width="450px" height="300px">
            </div>
        </div><!-- end row -->
    </div>
</div>

最佳答案

first-child 伪选择器选择作为其父元素的第一个 子元素的任何元素。在您的示例中,在第一个 .row div 中,h1“SHOWS”是第一个元素,您的 img 是第二个元素。所以它不会被选中。相反,在另一个 div 中,两个 img 标签都是其父标签的第一个子标签,因此它们确实被选中。

换句话说,img:first-child 不会选择第一个 img 标签,它会选择 img 标签,它是其 parent 的第一个 child 。如果它有前一个 sibling ,则 first-child 不适用。

我们可以通过将 first-child 放在 .row 上来修复您的代码,以便选择第一行 div 中的 img,这也恰好是第一张图片。

#logos .row:first-child img {
  border: 1px solid blue;
  }
<div id="logos" name="logos">
			<div class="container">
				<div class="row">
				<br>
					<h1 class="centered">SHOWS</h1>

					<img src="img/kenshows400x300.jpg" class="img-responsive" alt="Ken Cooper" width="400px" height="300px">

					<hr>
				</div> <!-- end row -->

				<div class="row">
					<div class="col-lg-6">
						<img src="img/shows/jkllogo450x300.jpg" class="img-responsive" alt="Jimmy Kimmel Live" width="450px"       height="300px">
					</div>
					<div class="col-lg-6">
						<img src="img/shows/latelateshowlogo450x300.jpg" class="img-responsive" alt="Late Late Show" width="450px" height="300px">
					</div>
				</div> <!-- end row -->

			</div>
		</div>

关于html - 图片 :first-child not working for my first image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26924395/

相关文章:

html - 如何在 css background-image : url(. ....) 中放入 div 2 图像

php - jQuery数据表在PHP中不起作用

image - 使用 VueJs 复制或下载生成的 QR (vue-qrcode) 代码

html - 在 HTML 页面中实现 CSS 变量

html - 如何使用一些工具检测图像的 CSS 属性

javascript - jQuery:如何获取所选单元格的列标题和行标题

javascript - 在javascript中将图像覆盖在图像上

jquery - 在无法滚动的网站上调整图像大小

css - IE7/IE8 z-index 不合作。建议?

css - 用 <li> 显示包含 <img> 的文本