本以为CSS样式只能传递到子级或是以兄弟元素的方式去传递到相邻元素,没想到其实只要用到最基本的方式就可以让它无递传递属性给子级元素。 比如这里传递鼠标悬停效果给子级: 例一: /* 当鼠标悬停在 .parent 上时,所有子元素都会应用 .child 类的样式 */ .parent:hover .child { /* 这里是你想要应用的样式 */ color: red; } 例二: /* 当鼠标悬停在 .parent 上时,所有子元素以及子元素的子元素都会应用 .child 类的样式 */ .parent:hover .child, .parent:hover .child * { /* 这里是你想要应用的样式 */ color: red; } 例三: /* 当鼠标悬停在 .parent 上时,所有子元素以及子元素的子元素都会应用 .child 类的样式 */ .parent:hover .child, .parent:hover .child, .parent:hover .child *, .parent:hover .child * * { /* 这里是你想要应用的样式 */ color: red; } 以上三种方法,方法2 和 方法3 的区别在于,方法2 只会影响 .child 类及其子孙元素,而方法3 会影响更多的元素。你可以根据实际需求选择合适的方法。 比如,我用了类似的方式: .elementor-column.elementor-col-25.elementor-top-column.elementor-element:hover > div,.elementor-column.elementor-col-25.elementor-top-column.elementor-element:hover > div *,.elementor-column.elementor-col-25.elementor-top-column.elementor-element:hover > div * * { background-color: background-color: #ECEBEB; } 实测这样是可行的。 未经允许不得转载:泥人传说 » css传递样式到所有子级的方法 css传递样式css传递样式给子级css传递样式给子级的方法css用递归方式传递样式css用递归方式传递样式给子级 分享到: 赞(0)