Is it possible to “remove” styling from HTML elements?

荒凉一梦 提交于 2019-12-24 19:24:15

问题


I'm wondering if it's possible to restore an element's style to it's "default" state, with Javascript or otherwise.

I need to do this because I'm inserting HTML into 3rd party web pages and cannot control what styles they attribute to different elements. For instance, they may have:

div {
  margin: 10px;
  padding: 5px;
  line-height: 10px;
  font-size: 150%;
  border: 10px solid yellow;
  foo: bar;
}

Is there an easy way to clear out all of the styles and set it back to the "default" settings? Is there even a list of default styles?

Thanks


回答1:


It appears that someone has already asked this question on SO.

CSS Reset, default styles for common elements




回答2:


The default styles are defined by each browser, and as you can guess, all the browsers have it styled a bit differently. You may be able to define each style attribute and assign it to inherit !important:

div {
  margin: inherit !important;
  padding: inherit !important;
  line-height: inherit !important;
  font-size: inherit !important;
  border: inherit !important;
  foo: inherit !important;
}

I am not entirely positive this will work, so I will be looking around and trying it out.




回答3:


If you can upload your own CSS (or internal stylesheet) you can set the id of an element and those styles will take precedence over the generic div{} styles.




回答4:


You could always define a class of reset or similar, with all of the default styling rules, and then use JS to apply that style to the elements you want to reset. eg

.reset {
  margin: 0 !important;
  color: #000 !important;
  ... etc ...
}

and then some jQuery like:

$('div').addClass('reset');

Would maybe work ...



来源:https://stackoverflow.com/questions/460197/is-it-possible-to-remove-styling-from-html-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!