html - css 2.1中的第一个 child 伪类

标签 html css css-selectors

我不知道在这个定义中把伪类放在哪里:

#recently_played_grid li img {
    margin-left: 10px;
}

我只想在列表的第一个子项中将 margin-left 设置为 0px。我应该怎么做:

#recently_played_grid li:first-child img

?

好像不行

最佳答案

如果您正在寻找的是第一个 li 项中的ALL 图像,那么您的代码应该可以正常工作。如果您希望在每个 li 的第一个 img 子级上留有余量,那么您必须将其更改为

#recently_played_grid li img:first-child

<ul>
    <li>
        <img />                    << Selects this, and
        <img />
        <img />
    </li>
    <li>
        <img />                     << Selects this, and
        <img />
        <img />
    </li>
    <li>
        <img />                     << Selects this
        <img />
        <img />
    </li>
</ul>

如果你只想要 FIRST li 中的 FIRST img 那么你应该将它更改为

#recently_played_grid li:first-child img:first-child


<ul>
    <li>
        <img />                          << Selects only this
        <img />
        <img />
    </li>
    <li>
        <img />
        <img />
        <img />
    </li>
    <li>
        <img />
        <img />
        <img />
    </li>
</ul>

在这里进一步阅读 http://www.w3.org/TR/CSS2/selector.html#pseudo-class-selectors

关于html - css 2.1中的第一个 child 伪类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18490597/

相关文章:

html - 创建一个向上打开的下拉菜单

html - 如何在html中并排放置2张图片?

css - 正确的 CSS 语法

javascript - 使用 jQuery 或 Javascript 更改 svg 速率

javascript - 像 Gumroad.com 上的交替文本

html - 基本 CSS 帮助 - 删除形状

css - 如何阻止我的 Feedback Div 停止改变屏幕分辨率的位置?

css - Bootstrap CSS 类通配符

css - 选择具有某种类型子元素的父元素的最后一个元素的最佳方法是什么?

javascript - jQuery 根据过滤器显示/隐藏行