html - 有没有办法根据其父项在组中设置 css 样式?

标签 html css

有没有一种方法可以根据父级分组编写css,这样我就不需要一遍又一遍地重写父级的名字,例如:

.firstparent {
    .firstchild { background: red }
    .secondchild { background: orange }
    .thirdchild .grandchild { background: green }
    /* This will only affect the elements with the class name that is the child of firstparent *??
}

.secondparent {
    div { background: blue }
    .firstchild { background: black }
}

/* Right now I am writing as below */

.firstparent .firstchild { background: red }
.firstparent .secondchild { background: orange }
.firstparent .thirdchild .grandchild { background: green }

.secondparent div { background: blue }
.secondparent .firstchild { background: black }

有没有办法像我上面显示的那样编写 css,而不是一遍又一遍地重新写 parent 的名字?

最佳答案

SASS/LESS 等 CSS 预处理器可帮助您将子元素嵌套在父元素中。它们还允许您相对于父元素嵌套事件元素状态、伪元素等。

欲了解更多信息并快速启动,请查看 sass basics guide

有关详细信息,请参阅 sass reference guide .

关于html - 有没有办法根据其父项在组中设置 css 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41848478/

相关文章:

jquery - 如何通过JQuery修改div元素的CSS属性(高度宽度)?

javascript - 使用动画使元素可见

html - 为什么float元素不会计算为div高度的一部分?BFC可以!CSS规范哪里说的?

html - 如何在CSS中移动单选按钮

HTML响应式网站

html - div 内的 CSS 文本悬停下划线动画

php - 当客户添加票证时,我想向该部门的所有员工发送邮件

javascript - 隐藏较低 z-index 的元素 "beneath"div

java - 如何从 phonegap 和 android 访问相同的 sqlite 数据库?

javascript - 在 javascript 中手动创建图像并根据 Alpha channel 进行绘制?