css - 是否有 CSS 父级选择器?

标签 css css-selectors

如何选择 <li> anchor 元素的直接父元素?

例如,我的 CSS 是这样的:

li < a.active {
    property: value;
}

显然有一些方法可以用 JavaScript 做到这一点,但我希望有某种 CSS Level 2 原生的解决方法。

我尝试设置样式的菜单被 CMS 弹出,所以我无法将事件元素移动到 <li>元素...(除非我将我不想做的菜单创建模块作为主题)。

最佳答案

目前无法以适用于所有浏览器的方式在 CSS 中选择元素的父元素。

Selectors Level 4 Working Draft包括将提供此功能的 :has() 伪类。它将类似于 jQuery implementation , 但目前不受 Firefox 支持。

li:has(> a.active) { /* styles to apply to the li tag */ }

Firefox is the only major browser not currently supporting it by default .

与此同时,如果您需要选择具有完全跨浏览器支持的父元素,您将不得不求助于 Firefox 中的 JavaScript。

关于css - 是否有 CSS 父级选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225250/

相关文章:

CSS 布局 : align elements in the right order

html - <h1> 图片前后重复?

jquery - 根据数量设置具有相同类的多个元素的宽度

javascript - jQuery:水平滚动到div

html - Bootstrap Responsive - 调整窗口大小时如何强制行到底部

javascript - 如何展开div/显示隐藏内容

css - 显示 :after when hovering over the parent element via CSS

android - View 从其父级获取其可绘制状态(按下等...)

html - fa 图标不能与#tag_selector * {_CSS_} 一起使用

html - Bootstrap 4 自定义复选框与文本输入格式相同?