快速的回答是使用“all:revert”
.element {
all:revert;
}
all:revert会将元素上的所有样式属性重置回原始的浏览器默认UA样式表属性值,或者父元素体中的任何样式。但它不会像initial那样ERASE样式属性,将它们返回到完全无样式的状态。
在文本或继承属性的情况下,“revert”将元素的CSS属性重置为来自“body”元素或浏览器默认UA样式值的继承值,而不是属性的基本样式。对于非继承的属性,它会将其重置为浏览器的UA默认样式表,而不是属性的基本样式。all允许影响所有属性。这可能是你想看到的。
使用“all:revert”的问题
“all:revert”是一个更新的CSS声明,它只适用于更现代的HTML5浏览器(2015年后),即使在那时,某些现代浏览器(如Internet Explorer 1-11、Edge Trident和一些移动浏览器)的支持也很差。没有一个老式的、非html5的浏览器(2010年以前)能够理解这个声明,所以它将被各种各样的浏览器忽略,无论新旧。(请参阅下面我的混合CSS解决方案,它修复了Internet Explorer)。
使用“initial”和“unset”的问题
You can use "initial" or "unset" but you have to manually apply them for each property, and what is even worse, they will not return properties to the element's default display values as set by each browser's default UA style sheet, but "initial" will essentially erase the element's property values and create a completely unstyled element. For example, "display:block" on block level elements will be erased. Because the style property still needs a default value of some kind all block and non-block level elements with "display" will be changed to "display:inline" when you use "display:initial". You do not want to ever do this as it erases your styles AND the browser's default UA element styles from the selected element completely.
“unset”工作接近于相同,但在继承基于文本的CSS属性的情况下,它的属性继承了元素上面的父元素中的任何东西(可能是浏览器默认的UA样式或上面的HTML父元素中的任何东西),但对于非继承的属性则像“initial”一样工作。
My recommendation is AVOID using all:initial or any form of initial in CSS unless you are trying to erase an individual CSS property you cannot erase in any other way. Why? initial erases not just the styles you applied but all styles the browsers default UA style sheet applied. all:revert will not do this. In terms of using initial, it does have better support in Internet Explorer, as does its cousin, inherit. But only IE8+ understands initial. So, there are a wide range of problems with this property value. It is not reliable.
The reason CSS works this way is all HTML elements come without any styling until the browser applies a default user-agent style sheet that gives all the HTML elements a base style. All HTML elements really have NO STYLES, and other than "replaced" elements like textarea and buttons, look alike until each browser's default UA sheet is applied. "initial" and "unset" would wipe away most of that from the browser. "revert" at least preserves their basic style set applied by the user's browser, and is therefore superior to "initial" and "unset". You can review all the various default style sheets that come with browsers in the link below.
默认浏览器样式表列表:
https://meiert.com/en/blog/user-agent-style-sheets/
现在有一个更好的解决方案
这里有两个概念被混淆了:
The first idea is about "returning" styles back to a browser's UA style sheet value set (the style sheet that comes with the browser on install that defines what each element looks like). Each browser defines its own styles as to how elements should look by default. This idea is about returning all page styles back to each browsers native element styles.
The second idea is about "resetting" all default browser styles to a common look and feel shared by all browsers. People build special "reset" sheets to try and align all the browser elements to a common agreed on style, universally. This has nothing to do with a browsers default UA styles and more about "cleaning up" and aligning all browsers to a common base style. This is an additive process only.
这是两个非常不同的概念,这里的人都搞混了。
因为每个浏览器通常都有默认的、开箱即用的元素和布局样式,看起来略有不同,人们想出了“reset”或“reboot”样式表的想法,在应用自定义CSS之前对齐所有浏览器。例如,Bootstrap现在就是这样做的。但这与人们想要回到浏览器的默认外观无关。
问题不在于构建这些自定义的“重置”样式表,而是在应用任何样式之前,确定每个浏览器和每个元素的默认CSS是什么。大多数发现你不能重建现有的干净级联,直到你“清除”所有已经应用的样式。但是如何回到默认的浏览器样式呢?
对于一些人来说,这意味着将元素返回到浏览器自带的UA样式表。许多人想重置回“初始”属性值,这与浏览器的默认样式无关,但实际上是属性的默认值。这是危险的,因为在“display”的情况下,将块级元素推回“内联”,破坏表格布局和其他东西。
所以我不同意用户在这里使用“initial”来重置任何东西,或者自定义重置类将每个属性更改为任意的基值集。
A better solution to me has always been to attempt to try and return all core element styling back to the browser's UA style sheet values, which is what all our end-users are using anyway. If you are creating a new website, you don't have to do this. You start with the browser's default styles and style over them. Its only after you've added third-party CSS products, or found yourself with complicated CSS cascades, that you want to figure out how to return to the browser default style sheet values.
出于这个原因,我支持创建自己的“重置”表,首先将所有元素重置为所有新老浏览器共享的一种通用样式,作为第一步。这样就有了一个坚固的框架,可以更容易地恢复,而不必回到浏览器的默认设置。您只是在元素样式值的重置公共核心集上进行构建。一旦建立了自己的“重置”表,一个添加而不是改变浏览器UA样式的表,你就有了一个非常容易修改的网站。
剩下的唯一问题是,当你有一个网站没有这样的重置表,或有复杂的第三方CSS,需要尝试和返回浏览器的UA样式。你是怎么做到的?
I realize Internet Explorer has forced us too manually reset every property to get back to any sort of reset. But pushing those property values all back to "initial" destroys the browser UA style sheet values completely! BAD IDEA! A better way is to simply use "all:revert" for non-IE browsers on every element using a wildcard, and "inherit" only for a handful of inherited root-level properties found in the "html" and "body" elements that affect all inheriting children in the page. (see below). I'm NOT for these huge resets of properties using "initial" or going back to some imaginary standard we assume all browsers or IE will use. For starters "initial" has poor support in IE and doesn't reset values to element defaults, only property defaults. But its also pointless if you are going to create a reset sheet to align all elements to a common style. In that case its pointless to clear out styles and start over.
So here is my simple solution that in most cases does enough to reset what text-based values sift down into IE from the root and use "all:revert" for all non-IE browsers to force non-inherited values back to the browser's UA style sheet completely, giving you a true restart. This does not interfere with higher level classes and styles layered over your element styles, which should always be the goal anyway. Its why I'm NOT for these custom reset classes which is tedious and unnecessary and doesn't return the element to its browser UA style anyway. Notice the slightly more selective selectors below which would write over, for example, Bootstrap's "reboot" style values, returning them to the browser default styles. These would not reset element styles on elements for IE, of course, but for non-IE browsers and most inheritable text styling it would return elements in most agents to the UA style sheets that come with browsers:
:root, html {
display: block;
font-family: inherit;
font-size: inherit;
font-weight: inherit;
line-height: inherit;
-webkit-text-size-adjust: inherit;
-webkit-tap-highlight-color: inherit;
all: revert;
}
html body {
display: block;
font-family: inherit;
font-size: inherit;
font-weight: inherit;
line-height: inherit;
margin: inherit;
padding: inherit;
color: inherit;
text-align: inherit;
background-color: inherit;
background: inherit;
all: revert;
}
/* This rule attempts to manually reset all elements back to the
UA browser style sheet using "revert" and the "wildcard" (*)
which resets all properties on all HTML elements.
This would overwrite most of Bootstraps "reboot" styles
on elements, for example.
Note: This selector should be ignored by IE. */
html body * {
all: revert;
}
你也可以使用我的免费通用CSS框架,它在它的重置样式表中实现了恢复。