最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far