4

我打算使用 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
  • 铬(最新)
  • 火狐(最新)
  • 歌剧(最新)

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

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


到目前为止发现的问题

  • 选择*器可能会导致性能问题。

  • 具有某些规则的*选择器会以无法恢复的方式覆盖某些元素的默认样式。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

4

4 回答 4

6

我通常认为广泛的 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是一个可访问性问题。

ol并且ul应该有它们的默认样式。您可能需要它们。(尽管您可能需要为导航覆盖它们。)

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

正如您所提到和承认的那样,*{ // }可能会导致性能问题,并可能导致无法预料的问题。

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

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

于 2011-08-01T02:14:27.747 回答
4

这是使用*将影响一切。您无法input, select使用“稍后”样式表重新获得等的边框。

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

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

于 2011-07-31T23:22:28.983 回答
1

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

于 2011-08-01T01:46:01.947 回答
0

我看不出它有什么问题,如果您已经对其进行了测试并且可以正常工作,那么它应该没问题。

于 2011-07-31T23:21:48.643 回答