CSS img 不是的直接子级

标签 css css-selectors

我想选择 #boardMostraimg 子元素,但不是标签 a 的直接子元素。

我试过这个:

#boardMostra img :not(a img:first-child){
  ...
}

但它不起作用。

最佳答案

唯一可以使用的 css 方法是在 #boardMostra img 上设置属性并在 #boardMostra a > img 上中和它。

一个例子

<div>
    <img src="#" alt="#">
    <a href="#"><img src="#" alt="#"></a>
</div>

div img { margin: 10px 20px; }
div a > img { margin: 0; }

解释

为什么您的示例代码不起作用:

The :not(X) property in CSS is a negation pseudo class and accepts a simple selector1 as an argument. Essentially, just another selector of any kind.

:not matches an element that is not represented by the argument. The passed argument may not contain additonal selectors or any pseudo-element selectors.

来源:https://css-tricks.com/almanac/selectors/n/not/

关于CSS img 不是的直接子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35207739/

相关文章:

html - 如何更改转置表以使其可水平滚动?

jQuery:具有点击不透明度变化的图像推子

javascript - 单击同一类型时关闭一种类型的下拉列表

html - Squarespace 是否不允许在自定义 HTML 中使用类和 ID 选择器,然后在 CSS 编辑器中定位?

css - 嵌套 CSS 类

css - 如何使用伪类选择除第一个和最后一个之外的所有 child ?

html - 如何将内容包裹在div中?

html - flexbox 容器和子结构

css - 是否有一个 CSS 选择器可以在整个页面中定位给定类型的第 n 个元素?

.net - 页面按钮的样式不同于选择按钮