这个 CSS Reset 可以吗?

发布于 2024-11-27 03:30:35 字数 4445 浏览 2 评论 0原文

我本来打算使用 Eric Meyer 的 CSS 重置,但我偶然发现了一些跨浏览器的差异(例如 input 边距)。基于此,我想出了一个更激进的方法:

(这已经过时了。不要忘记在这个问题的末尾检查当前的修订版本并根据需要批评它)

/* CSS Reset by Hugo Leonardo. Based on Eric Meyer's CSS Reset (just google it). */
* {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font: inherit;
    text-decoration: none;

    /* in case "font: inherit" fails (IE7) */
    font-family: "times new roman";
    font-size: 100%;
    font-style: normal;
    font-variant: normal;
    font-weight: normal;
    /* remove this block if you DON'T want to support lame browsers */
}

:before, :after {
    content: '';
}

:link, :visited, :hover, :active {
    color: inherit;
    color: #000; /* for IE7 'inherit' problem (again) */
    text-decoration: none;
}

:focus {
    outline: 0;
}

ol li, ul li {
    list-style: none;
}

table {
    /* "collapse" here is because of IE7 (maybe others?). don't remove it as it affects other browsers as well */
    border-collapse: collapse;
    border-spacing: 0;
}

/* this entire block was copied from Eric Meyer's CSS reset */
/* HTML5 "display" reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

它有效在所有测试的浏览器中都能顺利运行:

  • IE7
  • IE8
  • Chrome(最新)
  • Firefox(最新)
  • Opera(最新)

问题是:有人看到这里有什么问题吗?我认为自己在 CSS 方面不太擅长,所以我不知道这是否会给我将来带来任何麻烦。

观察:此重置仅适用于跨浏览器问题。它应该(或必须!)后跟诸如 inputacode 等元素的通用规则(例如:“文本”类型的 >input 如果没有边框,将是不可见的!)。 稍后我将添加通用 a 样式等内容。现在我正在重置一些东西,摆脱(几乎)所有主要浏览器中不一样的东西。


到目前为止发现的问题

  • *选择器可能会影响性能问题。

  • 带有某些规则的 * 选择器会以无法恢复的方式覆盖元素的某些默认样式。例如:“submit”类型的 input 的默认样式。

  • 令人惊讶的是 :before, :after { content: ''; } 破坏了 Firefox 中的选择元素。

  • 在修订版本中,我尝试将 margin: 0 设置为所有输入元素。大多数浏览器忽略输入类型 checkboxradio


修订版本

/* CSS Reset by Hugo Leonardo Leão Mota
Based on Eric Meyer's CSS Reset: http://meyerweb.com/eric/thoughts/2011/01/03/reset-revisited/
Helped by fellows in stackoverflow: http://stackoverflow.com/questions/6892982/is-this-css-reset-okay */

/* resetting style for every visible element except 'ruby' family and form controls
   browsers deal with controls (and ruby style) in their own way */
a, abbr, acronym, address, b, big, blockquote, body,
br, caption, cite, code, col, colgroup, dd, del, dfn, div,
dl, dt, em, fieldset, form, h1, h2, h3, h4, h5, h6, hr, html, i,
img, ins, kbd, label, legend, li, noscript, object, ol, p, pre, q, samp,
small, span, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, ul, var {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font: inherit;
    text-decoration: none;

    /* in case "font: inherit" fails (IE7) */
    font-family: "times new roman";
    font-size: 100%;
    font-style: normal;
    font-variant: normal;
    font-weight: normal;
    /* remove this block if you DON'T want to support lame browsers */
}

/* browsers are free to handle controls but
   we can't let them mess up with the other elements  */
button, select, textarea,
input[type=text], input[type=password], input[type=submit],
input[type=image], input[type=reset], input[type=button], input[type=file] {
    margin: 0;
}



:link, :visited, :hover, :active {
    color: inherit;
    color: #000; /* for IE7 'inherit' problem (again) */
    text-decoration: none;
}

:focus {
    outline: 0;
}

ol li, ul li {
    list-style: none;
}

table {
    /* "border-collapse" here is because of IE7 different behaviour (maybe others?).
       don't remove it as it affects other browsers as well */
    border-collapse: collapse;
    border-spacing: 0;
}

/* the next two blocks were copied from Eric Meyer's CSS reset */

blockquote:before, blockquote:after, q:before, q:after {
    content: '';
}

/* HTML5 "display" reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

结束

好吧,我越努力改进重置,它看起来就越像 meyer 的 css 重置,所以我放弃了我的并采用了它。工作得很好:p

I was intending to use Eric Meyer's CSS reset but I stumbled in some cross-browser differences (like input margins). Based on it, i came up with a more agressive aproach:

(This is outdated. Don't forget to check the current revised version at the end of this question and criticize it as you wish)

/* CSS Reset by Hugo Leonardo. Based on Eric Meyer's CSS Reset (just google it). */
* {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font: inherit;
    text-decoration: none;

    /* in case "font: inherit" fails (IE7) */
    font-family: "times new roman";
    font-size: 100%;
    font-style: normal;
    font-variant: normal;
    font-weight: normal;
    /* remove this block if you DON'T want to support lame browsers */
}

:before, :after {
    content: '';
}

:link, :visited, :hover, :active {
    color: inherit;
    color: #000; /* for IE7 'inherit' problem (again) */
    text-decoration: none;
}

:focus {
    outline: 0;
}

ol li, ul li {
    list-style: none;
}

table {
    /* "collapse" here is because of IE7 (maybe others?). don't remove it as it affects other browsers as well */
    border-collapse: collapse;
    border-spacing: 0;
}

/* this entire block was copied from Eric Meyer's CSS reset */
/* HTML5 "display" reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

It worked smoothly in all tested browsers:

  • IE7
  • IE8
  • Chrome (newest)
  • Firefox (newest)
  • Opera (newest)

The question is: Does anyone see any trouble here? I consider myself not so good in CSS so I don't know if this will get me in any trouble in the future.

Obs.: this reset is for cross-browser issues only. It should (or must!) be followed by generic rules for elements like input, a, code, and so on (ex: input of type "text" would be invisible without borders!). I will be adding things like generic a styles and stuff later. For now I'm resetting things, getting rid of (almost) everything that isn't the same across the major browsers.


PROBLEMS SPOTTED SO FAR

  • The * selector could cause performance issues.

  • The * selector with some of the rules override some default styles of elements in a way they can't be recovered. ex: the default style of an input of the type "submit".

  • Surprisingly the :before, :after { content: ''; } was breaking select elements in Firefox.

  • In the revised version I tried to set margin: 0 to all input elements. Most browsers ignored it for inputs type checkbox and radio.


REVISED VERSION

/* CSS Reset by Hugo Leonardo Leão Mota
Based on Eric Meyer's CSS Reset: http://meyerweb.com/eric/thoughts/2011/01/03/reset-revisited/
Helped by fellows in stackoverflow: http://stackoverflow.com/questions/6892982/is-this-css-reset-okay */

/* resetting style for every visible element except 'ruby' family and form controls
   browsers deal with controls (and ruby style) in their own way */
a, abbr, acronym, address, b, big, blockquote, body,
br, caption, cite, code, col, colgroup, dd, del, dfn, div,
dl, dt, em, fieldset, form, h1, h2, h3, h4, h5, h6, hr, html, i,
img, ins, kbd, label, legend, li, noscript, object, ol, p, pre, q, samp,
small, span, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, ul, var {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font: inherit;
    text-decoration: none;

    /* in case "font: inherit" fails (IE7) */
    font-family: "times new roman";
    font-size: 100%;
    font-style: normal;
    font-variant: normal;
    font-weight: normal;
    /* remove this block if you DON'T want to support lame browsers */
}

/* browsers are free to handle controls but
   we can't let them mess up with the other elements  */
button, select, textarea,
input[type=text], input[type=password], input[type=submit],
input[type=image], input[type=reset], input[type=button], input[type=file] {
    margin: 0;
}



:link, :visited, :hover, :active {
    color: inherit;
    color: #000; /* for IE7 'inherit' problem (again) */
    text-decoration: none;
}

:focus {
    outline: 0;
}

ol li, ul li {
    list-style: none;
}

table {
    /* "border-collapse" here is because of IE7 different behaviour (maybe others?).
       don't remove it as it affects other browsers as well */
    border-collapse: collapse;
    border-spacing: 0;
}

/* the next two blocks were copied from Eric Meyer's CSS reset */

blockquote:before, blockquote:after, q:before, q:after {
    content: '';
}

/* HTML5 "display" reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

END

Well, the more i tried to improve my reset, the more it looked like meyer's css reset, so i gave mine up and adopted it. works just fine :p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼波传意 2024-12-04 03:30:35

我通常认为大范围的 CSS 重置并不是最好的。我同意 Russ Weakley 的观点,他“集中”了三大问题:

  1. 每个被重置的元素都必须重新定义。 CSS 文件大小 &维护量可以增加。
  2. 您可能会忘记重新设计您重置的内容。
  3. 某些重置对于依赖键盘进行导航的用户来说是有害的。

在这里查看他的整个演讲:http://www.maxdesign.com.au/articles/ css-reset/

具体来说,我认为不应重置以下内容,因为它在样式表中

:before, :after {
    content: '';
}

:link, :visited, :hover, :active {
    color: inherit;
    color: #000; /* for IE7 'inherit' problem (again) */
    text-decoration: none;
}

:focus {
    outline: 0;
}

ol li, ul li {
    list-style: none;
}

focus 是一个可访问性问题。

olul 应该有它们的默认样式。您可能需要它们。 (尽管您可能需要覆盖它们以进行导航。)

:link, :visited, :hover, :active 我只会根据需要重置这些。

正如您所提到和承认的,*{ // } 可能会导致性能问题,并可能导致不可预见的问题。

另外,我会考虑添加一些东西来重置标题上的大顶部和底部边距

h1、h2、h3、h4、h5、h6 {margin-top:0;边距底部:0;}

I generally think that wide-ranging CSS resets are not the best. I agree with Russ Weakley, who "zeroed" in on three big concerns:

  1. Every element that's reset must be redefined. CSS file size & maintenance can increase.
  2. You could forget to restyle something you reset.
  3. Some resets are harmful to users who rely on keyboards for navigation.

See his whole presentation here: http://www.maxdesign.com.au/articles/css-reset/

Specifically, I think the following should not be reset, as it is in your stylesheet

:before, :after {
    content: '';
}

:link, :visited, :hover, :active {
    color: inherit;
    color: #000; /* for IE7 'inherit' problem (again) */
    text-decoration: none;
}

:focus {
    outline: 0;
}

ol li, ul li {
    list-style: none;
}

focus is an accessibility issue.

ol and ul should have their default styles. You are likely to need them. (Although you may need to overwrite them for a nav.)

:link, :visited, :hover, :active I would reset these only as needed.

As mentioned and acknowledged by you *{ // } could cause performance issues and may cause unforeseen issues.

Also, I would consider adding something to reset the big top and bottom margins on headers

h1, h2, h3, h4, h5, h6 {margin-top:0; margin-bottom:0;}

花想c 2024-12-04 03:30:35

这是使用 * ,它将影响一切。您无法使用“稍后”样式表返回输入、选择等的边框。

此外,* 被认为对性能不利。首选使用显式标签。

如果您遇到 Eric 的问题,请尝试重置 html5boilerplate(不确定是否能解决问题,但值得一试)

This is using * which will affect everything. You can't get borders for input, select etc. back in with a "later" stylesheet.

Also, * is considered bad for performance. Using explicit tags is preferred.

Try html5boilerplate's reset if you're having issues with Eric's (not sure if it will solve them, but worth a shot)

一指流沙 2024-12-04 03:30:35

我唯一担心的是使用 * 选择器引起的性能问题

My only concern is the performance issue caused by using the * selector

傲影 2024-12-04 03:30:35

我不认为它有任何问题,如果你已经测试过它并且它有效,那么它应该没问题。

I dont see any trouble with it, if you've tested it and it works then it should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文