html - :last-child effects all img tags

标签 html css css-selectors

这是第一次发布问题,所以我希望我做对了。我想要做的是让我的最后一个 child 在我的包装 div 中不向右留白。

.wrapper img,
video {
  width: 18%;
  margin-right: 2.5%;
}

.wrapper img,
video:last-child {
  margin-right: 0;
}
<div class="wrapper">
  <img src="cat.jpg">
  <video>
    <source src="cats.mp4">
    </video>
  <img src="morecats.jpg">
  <video>
    <source src="meow.mp4">
    </video>
  <img src="garfield.jpg">
</div>

这里的问题是,div 中的每个 img 标签现在都有 margin-right:0; .我希望有人理解我的问题并能帮助我。

注意:我不允许使用 HTML 和 CSS 以外的语言。

最佳答案

您还需要将 :last-child 添加到 img。

.wrapper img:last-child, .wrapper video:last-child{
    margin-right:0;
}

关于html - :last-child effects all img tags,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43975107/

相关文章:

css - 如何在嵌套元素之后选择紧接的下一个元素?

javascript - 使用 CSS ":before"将内联 block DIV 居中会破坏文本的垂直和水平居中

html - 如何区分两个不同的图像组件?

css - bootstrap 4 flex 行并排出现

html - 在 IE 8/9 中 - 无法单击图像,使用显示 :block 包裹在 span 内的 anchor

css - 更改 WordPress 中的产品格式

css - CSS 伪类会先于其他选择器被读取吗?

javascript - 对象 HTMLInputElement attr 不是函数

html - Lightbox 中的图像卡在容器外

html - CSS :first and :last-of-sequence pseudo-selectors like?