应用全局CSS重置后,返回默认属性 [英] Get back default properties after applying a global CSS reset

查看:216
本文介绍了应用全局CSS重置后,返回默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须构建的一些旧代码,真的让我感觉全局CSS重置的缺点

Some legacy code that I have to build upon, really makes me feel the cons of global CSS reset.

我有 foo.css

* {margin:0; padding:0;}

我用来将它复制到不同的文件 bar.css ,调整我的需要(与CSS重置),并用它来替换 foo.css 只有在我写的代码。我这样做不用担心与网站的旧版本的向后兼容性。

and I used to copy it to a different file bar.css, tweak it too my needs (out with the CSS reset), and use it to replace foo.css only in the code I'm writing. I do this not to worry about backwards compatibility with the older sections of the site.

现在这是相当麻烦:对于全局更改,我必须记住修改这两个文件。所以现在我的 bar.css 扩展 foo.css ,开头为:

Now this is quite cumbersome: for global changes I have to remember to modify both files. So now my bar.css is extending foo.css, starting with:

@import url("style.css");

问题是现在我也继承了CSS重置。

The problem is that now I also inherit the CSS reset.

有任何方式(†)带来保证金 padding 将某些元素(标题,列表等)的属性恢复为默认值 - 应用重置前的值?

Is there any way(†) to bring the margin & padding properties of some elements (headers, lists etc.) back to their default values -- the ones before the reset was applied?

(†)

推荐答案

有一个建议值初始用于将属性设置为其初始值,没有找到显式值。但它仍然是非常多的草稿阶段,几乎无法在任何浏览器中实现。

There’s the proposed value initial for setting a property to its initial value, without finding the explicit value. But it’s still very much draft-stage and hardly implemented in any browser.

此外,这可能甚至不是你想要实现的。如果我正确理解您,您可以设置h2元素的上边距和下边距到浏览器默认值。我不认为在CSS中甚至有任何建议的方式。规范不定义浏览器默认值。例如margin属性的初始值为0.标题在默认情况下具有顶部和底部边距的原因是浏览器至少在概念上应用浏览器样式表。 CSS规格建议是默认的浏览器样式表,但不强制使用,浏览器可能会偏离建议。

Moreover, that’s probably even not what you’d like to achieve. If I understand you correctly, you would want to set e.g. the top and bottom margins of h2 elements to the browser default value. I don’t think there’s even any proposed way of doing that in CSS. The specifications do not define the browser defaults. The initial value of, say, the margin property is 0. The reason why headings have top and bottom margin by default is that the browser applies, at least conceptually, a browser style sheet. The CSS specs suggest a default browser style sheet but do not mandate one, and browser may (and actually do) deviate from the suggestions.

实际上,在给定情况下最好的方法是检查CSS 2.1规范的附录D ,使用给出的值。对于诸如边距这样的东西,这将主要产生使用浏览器默认值的效果。

In practice, the best shot in the given situation would be to check Appendix D of the CSS 2.1 spec and use the values given there. For things like margins, this would mostly create the effect of using browser defaults.

这篇关于应用全局CSS重置后,返回默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆