是否有可用的CSS规则可以删除之前在样式表中为特定元素设置的任何样式?
一个很好的例子可能是在一个移动优先的RWD站点中,在这个站点中,用于小屏幕视图中的特定元素的许多样式都需要“重置”或删除桌面视图中的相同元素。
一个CSS规则可以实现如下功能:
.element {
all: none;
}
使用示例:
/* mobile first */
.element {
margin: 0 10;
transform: translate3d(0, 0, 0);
z-index: 50;
display: block;
etc..
etc..
}
@media only screen and (min-width: 980px) {
.element {
all: none;
}
}
因此,我们可以快速删除或重新设置样式,而不必声明每个属性。
使用all: revert或all: unset。
中数:
在许多情况下,revert关键字的工作原理与unset完全相同。的
唯一的区别是属性的值是由浏览器设置的
或者通过用户创建的自定义样式表(在浏览器端设置)。
您需要“可用的CSS规则,可以删除之前在样式表中为特定元素设置的任何样式。”
因此,如果元素的类名是remove-all-styles:
HTML:
<div class="remove-all-styles other-classe another-class">
<!-- content -->
<p class="text-red other-some-styles"> My text </p>
</div>
用CSS:
.remove-all-styles {
all: revert;
}
这将重置由其他类、另一个类和所有其他继承和应用到该div的样式应用的所有样式。
或者在你的情况下:
/* mobile first */
.element {
margin: 0 10;
transform: translate3d(0, 0, 0);
z-index: 50;
display: block;
etc..
etc..
}
@media only screen and (min-width: 980px) {
.element {
all: revert;
}
}
会做的事情。
如果我们想要将嵌入的小部件/组件从包含它们的页面的样式中分离出来,我们可以这样写:
.isolated-component {
all: revert;
}
它将把所有作者样式(即开发人员CSS)还原为用户样式(我们网站的用户设置的样式-不太可能的场景),或者如果没有设置用户样式,则还原为用户代理本身的样式。
As other answers have mentioned, the CSS property all set to the value unset seems to override all CSS properties without knowing which properties are in play. However, this did not work for me while adding custom CSS in a Wordpress site, as the all property was not recognized by the Wordpress custom CSS editor, and did not reflect changes in Microsoft Edge. Instead, what worked for me was a brute-force override, that is, to identify every property used in the webpage element and manually reset each one of them. The way to identify the CSS properties being used by an element is to inspect the element with your web browser, and go through the 'Styles' tab.
在我的特定场景中,我想跳过对页面的特定部分应用通用样式,更好地说明如下:
<body class='common-styles'>
<div id='header'>Wants common styles</div>
<div id='container'>Does NOT want common styles</div>
<div id='footer'>Wants common styles</div>
</body>
在打乱CSS重置并没有带来多大成功(主要是因为规则优先级和复杂的样式表层次结构)之后,出现了无处不在的jQuery来拯救它,它很快就完成了这项工作,而且相当脏:
$(function() {
$('body').removeClass('common-styles');
$('#header,#footer').addClass('common-styles');
});
(现在告诉你用JS来处理CSS是多么的邪恶:-))