简体   繁体   English

CSS:第一个孩子规则

[英]CSS :first-child rule

HTML is as follows HTML如下

<div class="box">
<a href="images/about_1.jpg">
    <img src="images/about_1_thumb.jpg" class="float-left">
</a>

<a href="images/about_2.jpg">
    <img src="images/about_2_thumb.jpg" class="float-left">
</a>
</div>

CSS is as follows CSS如下

div.box img.float-left{
  float:left;
  display:inline;
}

div.box a img.float-left:first-child{
  margin-right: 5px;
}

Trying to get the margin-right rule to appear only on the image inside the first anchor tag. 试图使右边距规则仅出现在第一个锚标记内的图像上。 Anybody see what I'm doing incorrectly? 有人看到我做错了吗?

Yes, Just add the selector to the a tag (Since you want the style on the first child of the parent) 是的,选择只需添加到a标签(因为你要在父母的第一个孩子的样式)

div.box img.float-left{
  float:left;
  display:inline;
}

div.box a:first-child img.float-left{
  margin-right: 5px;
}

Here is a DEMO 这是一个演示

您对第一个孩子的css调用应位于共享最高的标签(在本例中为a标签)上,而不应位于共享最高的嵌套元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM