前端样式重置(Normalize/Reset)
前言:所有浏览器都有附带的默认样式,这些样式应用在每一个页面,叫做“用户代理样式表”。(如下需要梯子)
Chromium UA stylesheet -Google Chrome & Opera
Mozilla UA stylesheet - firefox
WebKit UA stylesheet - safari
虽然大部分相同,但也有很多样式是不一致的,如搜索输入框
所以我们需要统一不同浏览器差异确认团队开发的起始标准,弥补浏览器的‘缺点’。
CSS Reset
1. CSS Reset为什么存在?
只要您的客户存在使用不同浏览器(ie,firefox,chrome等)的可能,那你就不得不从完美的理想状态回到现实,因为不同核心的浏览器对
CSS
的解析效果呈现各异,导致您所期望的效果跟浏览器的“理解”效果有偏差,今天提到的css reset
就是用来重置(复位)元素在不同核心浏览器下的默认值,尽量保证元素在不同浏览器下的同一“起跑线”。—— 引用自关于CSS Reset
2. CSS Reset该怎么写?
CSS reset
(css重置)基本上是不需要的,至少可以说80%的的CSS reset
都是没有必要的,反而增加了页面。
CSS
的重写,尤其像*{margin:0;}
的做法更是要不得(反而破坏了很多UI
的兼容性,比如说单复选框等)。我不是一概鄙弃CSS
reset
,有些常用标签我也是会简单重置一下的,而且会避免overwrite
(样式重写),以保证样式最精简,渲染最高效—— 摘录我是如何对网站CSS进行架构的
3. CSS Reset 示例
淘宝天猫
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,
form,fieldset,legend,button,input,textarea,th,td {margin:0;padding:0;}
body,button,input,select,textarea{font:12px/1.5 tahoma,arial,\5b8b\4f53;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
small{font-size:12px;
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:underline;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
et,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
腾讯
body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0;padding:0}
body{font:12px"宋体","Arial Narrow",HELVETICA;background:#fff;-webkit-text-size-adjust:100%;}
a{color:#2d374b;text-decoration:none}
a:hover{color:#cd0200;text-decoration:underline}
em{font-style:normal}
li{list-style:none}
img{border:0;vertical-align:middle}
table{border-collapse:collapse;border-spacing:0}
p{word-wrap:break-word}
百度
body {font-family:arial,helvetica,sans-serif;font-size:13px;font-size-adjust:none;font-stretch:normal;fontstyle:normal;font-variant:normal;font-weight:normal;line-height:1.4;text-align:center;}
body, ul, ol, dl, dd, h1, h2, h3, h4, h5, h6, p, form, fieldset, legend, input, textarea, select, button, th, td {margin:0;padding:0;}
h1, h2, h3, h4, h5, h6 {font-size:100%;font-weight:normal;}
table {font-size:inherit;}
input, select {font-family:arial,helvetica,clean,sans-serif;font-size:100%;font-size-adjust:none;font-stretch:normal;font-style:normal;font-variant:normal;font-weight:normal;line-height:normal;}
button {overflow:visible;}
th, em, strong, b, address, cite {font-style:normal;font-weight:normal;}
li {list-style-image:none;list-style-position:outside;list-style-type:none;}
img, fieldset {border:0 none;}
ins {text-decoration:none;}
网易
html {overflow-y:scroll;}
body {margin:0; padding:29px 0 0; font:12px"\5B8B\4F53",sans-serif;background:#ffffff;}
div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,blockquote,p{padding:0; margin:0;}
table,td,tr,th{font-size:12px;}
li{list-style-type:none;}
img{vertical-align:top;border:0;}
ol,ul {list-style:none;}
h1,h2,h3,h4,h5,h6{font-size:12px; font-weight:normal;}
address,cite,code,em,th {font-weight:normal; font-style:normal;}
新浪
body,ul,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div{margin:0;padding:0;border:0;}
body{background:#fff;color:#333;font-size:12px; margin-top:5px;font-family:"SimSun","宋体","Arial Narrow";}
ul,ol{list-style-type:none;}
select,input,img,select{vertical-align:middle;}
a{text-decoration:none;}
a:link{color:#009;}
a:visited{color:#800080;}
a:hover,a:active,a:focus{color:#c00;text-decoration:underline;}
搜狐
/* 全局CSS定义 */
body{font-family:’宋体';text-align:center;margin:0 auto;padding:0;background:#FFF;font-size:12px;color:#333;}
body > div{text-align:center;margin-right:auto;margin-left:auto;
div,form,ul,ol,li,span,p{margin:0;padding:0;border:0;}
img,a img{border:0;margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{margin:0;padding:0;font-size:12px;font-weight:normal;
ul,ol,li{list-style:none}
table,td,input{font-size:12px;padding:0}
/* 默认链接颜色 */
a{outline-style:none;color:#333;text-decoration:none}
a:hover{color:#c00;text-decoration:underline;}
土豆
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,
fieldset,input,textarea,p,blockquote,th,td{margin:0;padding:0}
body{color:#000;background:#fff;font-size:12px;line- height:1.25;font-family:tahoma,arial,\5b8b\4f53,sans-serif}
th,td{text-align:left;vertical-align:top}
h1,h2,h3,h4,h5,h6{font-size:1em;font-weight:normal}
p{margin:0 0 1em 0}
blockquote{margin:0 0 1em 0
a{color:#14647d;text-decoration:none;font-family:tahoma,arial,\5b8b\4f53,sans-serif;_font-family:arial,sans-serif}
a:hover{text-decoration:underline}
img{display:block;border:0}
li{list-style:none}
ol{padding:0 0 0 2em}
input,label,select,option,textarea,button,fieldset,legend{font-size:1em;font-family:tahoma,arial,\5b8b\4f53,sans-serif;_font-family:Arial,sans-serif}
input[type=text],textarea{-webkit-appearance:none;-webkit-border-radius:0;outline:none}
input.text{color:#444;border-color:#9f9f9f #dcdcdc #dcdcdc #9f9f9f;border-style:solid;border-width:1px;padding:3px 2px;height:14px}
input.radio,input.checkbox{margin-right:2px;vertical-align:-2px}
input.radio,input.checkbox{margin-left:-2px}
label.radio,label.checkbox{margin-right:5px}
textarea{border-color:#9f9f9f #dcdcdc #dcdcdc #9f9f9f;border-style:solid;border-width:1px;padding:1px;resize:none}
雅虎
/*
YUI 3.18.1 (build f7e7bcb)
Copyright 2014 Yahoo! Inc. All rights reserved.
Licensed under the BSD License.
http://yuilibrary.com/license/
*/
html{color:#000;background:#FFF}
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0}
table{border-collapse:collapse;border-spacing:0}
fieldset,img{border:0}
address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal}
ol,ul{list-style:none}
caption,th{text-align:left}
h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}
q:before,q:after{content:''}
abbr,acronym{border:0;font-variant:normal}
sup{vertical-align:text-top}
sub{vertical-align:text-bottom}
input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;*font-size:100%}
legend{color:#000}
#yui3-css-stamp.cssreset{display:none}
Normalize.css
Normalize.css
只是一个很小的CSS
文件,但它在默认的HTML
元素样式上提供了跨浏览器的高度一致性。相比于传统的CSS reset
,Normalize.css
是一种现代的、为HTML5
准备的优质替代方案。Normalize.css
现在已经被用于Bootstrap
、HTML5 Boilerplate
、GOV.UK
、Rdio
、CSS Tricks
以及许许多多其他框架、工具和网站上。
地址:http://necolas.github.io/normalize.css/
GitHub:https://github.com/necolas/normalize.css
创造目的:
- 保护有用的浏览器默认样式而不是完全去掉它们
- 一般化的样式:为大部分HTML元素提供
- 修复浏览器自身的bug并保证各浏览器的一致性
- 优化CSS可用性:用一些小技巧
- 解释代码:用注释和详细的文档来
Normalize.css支持包括手机浏览器在内的超多浏览器,同时对HTML5元素、排版、列表、嵌入的内容、表单和表格都进行了一般化。尽管这个项目基于一般化的原则,但我们还是在合适的地方使用了更实用的默认值。
Normalize vs Reset
- Normalize.css 保护了有价值的默认值
Reset
通过为几乎所有的元素施加默认样式,强行使得元素有相同的视觉效果。相比之下,Normalize.css
保持了许多默认的浏览器样式。这就意味着你不用再为所有公共的排版元素重新设置样式。当一个元素在不同的浏览器中有不同的默认值时,Normalize.css
会力求让这些样式保持一致并尽可能与现代标准相符合。
- Normalize.css 修复了浏览器的bug
它修复了常见的桌面端和移动端浏览器的
bug
。这往往超出了Reset
所能做到的范畴。关于这一点,Normalize.css
修复的问题包含了HTML5
元素的显示设置、预格式化文字的font-size
问题、在IE9
中SVG
的溢出、许多出现在各浏览器和操作系统中的与表单相关的bug
。
- Normalize.css 不会让你的调试工具变的杂乱
使用
Reset
最让人困扰的地方莫过于在浏览器调试工具中大段大段的继承链,如下图所示。在Normalize.css
中就不会有这样的问题,因为在我们的准则中对多选择器的使用时非常谨慎的,我们仅会有目的地对目标元素设置样式。
- Normalize.css 是模块化的
这个项目已经被拆分为多个相关却又独立的部分,这使得你能够很容易也很清楚地知道哪些元素被设置了特定的值。因此这能让你自己选择性地移除掉某些永远不会用到部分(比如表单的一般化)。
- Normalize.css 拥有详细的文档
Normalize.css
的代码基于详细而全面的跨浏览器研究与测试。这个文件中拥有详细的代码说明并在Github Wiki中有进一步的说明。这意味着你可以找到每一行代码具体完成了什么工作、为什么要写这句代码、浏览器之间的差异,并且你可以更容易地进行自己的测试。