最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a (polyfill) code that resets all the inherited CSS properties on a given element? - Stack Overflow

programmeradmin2浏览0评论

The subject says it all. Looking for a (polyfill) code that will reset all the inherited CSS properties on a given element (such as <img>, <a> or <p>).

The subject says it all. Looking for a (polyfill) code that will reset all the inherited CSS properties on a given element (such as <img>, <a> or <p>).

Share Improve this question asked Nov 14, 2011 at 9:03 Alexander AbramovichAlexander Abramovich 11.5k26 gold badges74 silver badges114 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

If you want to pletely reset everything to the browser's default styling, then the modern CSS approach is to use the revert keyword with the all property:

all: revert;

From MDN,

The revert CSS keyword rolls back the cascade so that the property takes on the value it would have had if there were no styles in the current style origin (author, user, or user-agent). In author stylesheets (the normal case), for the purposes of the given declaration, it's as if there were no author-level styles, thus resetting the property to the default value established by the user-agent stylesheet (or by user styles, if any exist).

Caveat: It is defined in the CSS Cascading and Inheritance Level 4 working draft, so browser support is currently limited to Safari only.

What you could do is create one of those elements but not attach it to the DOM. It wouldn't then receive any of the styles from your stylesheets. Then, using window.getComputedStyle, you will get a list of the default styles.

var a = document.createElement('a');
var s = window.getComputedStyle(a);

myTargetEl.style.color = s.color;  // or, use a loop to do all of them

May be you can use HTML5 boilerplate or css Reset to reset all the inherited CSS properties.

check these articles http://html5boilerplate./ ,

http://meyerweb./eric/thoughts/2007/05/01/reset-reloaded/

This sounds like the all property, which sadly is not yet implemented by any browser other than Firefox.

Example:

h2 {
  all: initial; // Will reset any style set for h2 elements
}

See https://developer.mozilla/en-US/docs/Web/CSS/all

发布评论

评论列表(0)

  1. 暂无评论