CSS Reset 是前端开发中一个很重要的概念,它可以消除不同浏览器之间的样式差异,让我们的页面在各种浏览器中都能够呈现一致的效果。在本文中,我们将介绍 CSS Reset 的原理以及常用的几种实现方式,并给出相应的示例代码。
CSS Reset 的原理
CSS Reset 的原理很简单,就是通过清除所有元素的默认样式,然后再重新定义这些元素的样式。这样做的好处是,我们可以在不同的浏览器中获得一致的样式效果,避免了因为浏览器差异而导致的页面样式问题。
常用的几种实现方式
1. Eric Meyer's Reset CSS
Eric Meyer's Reset CSS 是最著名的 CSS Reset 方案之一,它通过清除所有元素的默认样式,然后重新定义这些元素的样式。具体代码如下:
// www.javascriptcn.com code example
/* Eric Meyer's Reset CSS */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
/* remember to define focus styles! */
:focus {
outline: 0;
}
/* remember to highlight inserts somehow! */
ins {
text-decoration: none;
}
del {
text-decoration: line-through;
}
/* tables still need 'cellspacing="0"' in the markup */
table {
border-collapse: collapse;
border-spacing: 0;
}2. Normalize.css
Normalize.css 是一个比较新的 CSS Reset 方案,它不仅清除了所有元素的默认样式,还对一些元素做了一些修正。具体代码如下:
// www.javascriptcn.com code example
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
/* Document
========================================================================== */
/**
* 1. Correct the line height in all browsers.
* 2. Prevent adjustments of font size after orientation changes in iOS.
*/
html {
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
}
/* Sections
========================================================================== */
/**
* Remove the margin in all browsers.
*/
body {
margin: 0;
}
/**
* Render the `main` element consistently in IE.
*/
main {
display: block;
}
/**
* Correct the font size and margin on `h1` elements within `section` and
* `article` contexts in Chrome, Firefox, and Safari.
*/
h1 {
font-size: 2em;
margin: 0.67em 0;
}
/* Grouping content
========================================================================== */
/**
* 1. Add the correct box sizing in Firefox.
* 2. Show the overflow in Edge and IE.
*/
hr {
box-sizing: content-box; /* 1 */
height: 0; /* 1 */
overflow: visible; /* 2 */
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
pre {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/* Text-level semantics
========================================================================== */
/**
* Remove the gray background on active links in IE 10.
*/
a {
background-color: transparent;
}
/**
* 1. Remove the bottom border in Chrome 57-
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
/**
* Add the correct font weight in Chrome, Edge, and Safari.
*/
b,
strong {
font-weight: bolder;
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
code,
kbd,
samp {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
* Add the correct font size in all browsers.
*/
small {
font-size: 80%;
}
/**
* Prevent `sub` and `sup` elements from affecting the line height in
* all browsers.
*/
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
/* Embedded content
========================================================================== */
/**
* Remove the border on images inside links in IE 10.
*/
img {
border-style: none;
}
/* Forms
========================================================================== */
/**
* 1. Change the font styles in all browsers.
* 2. Remove the margin in Firefox and Safari.
*/
button,
input,
optgroup,
select,
textarea {
font-family: inherit; /* 1 */
font-size: 100%; /* 1 */
line-height: 1.15; /* 1 */
margin: 0; /* 2 */
}
/**
* Show the overflow in IE.
* 1. Show the overflow in Edge.
*/
button,
input { /* 1 */
overflow: visible;
}
/**
* Remove the inheritance of text transform in Edge, Firefox, and IE.
* 1. Remove the inheritance of text transform in Firefox.
*/
button,
select { /* 1 */
text-transform: none;
}
/**
* 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
* controls in Android 4.
* 2. Correct the inability to style clickable types in iOS and Safari.
*/
button,
html [type="button"], /* 1 */
[type="reset"],
[type="submit"] {
-webkit-appearance: button; /* 2 */
}
/**
* Remove the inner border and padding in Firefox.
*/
::-moz-focus-inner {
border-style: none;
padding: 0;
}
/**
* Restore the focus styles unset by the previous rule.
*/
:-moz-focusring {
outline: 1px dotted ButtonText;
}
/**
* Remove the additional `:invalid` styles in Firefox.
*/
:-moz-ui-invalid {
box-shadow: none;
}
/* Interactive
========================================================================== */
/*
* Add the correct display in Edge, IE 10+, and Firefox.
*/
details {
display: block;
}
/*
* Add the correct display in all browsers.
*/
summary {
display: list-item;
}
/* Misc
========================================================================== */
/**
* Add the correct display in IE 10+.
*/
template {
display: none;
}
/**
* Add the correct display in IE 10.
*/
[hidden] {
display: none;
}3. 自定义 CSS Reset
除了使用现成的 CSS Reset 方案,我们也可以根据自己的需求来自定义 CSS Reset。具体代码如下:
// www.javascriptcn.com code example
/* 自定义 CSS Reset */
html, body, div, span, h1, h2, h3, h4, h5, h6, p, img, a, ul, ol, li, table, tr, th, td, form, input, textarea, button {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
}
body {
line-height: 1;
}
img {
vertical-align: middle;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
input, textarea, select, button {
font-family: inherit;
font-size: inherit;
line-height: inherit;
}
button, input[type="submit"], input[type="button"], input[type="reset"], input[type="checkbox"], input[type="radio"], select, textarea {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
button, input[type="submit"], input[type="button"], input[type="reset"] {
cursor: pointer;
}学习和指导意义
通过本文的介绍,我们了解了 CSS Reset 的原理和常用的几种实现方式,并给出了相应的示例代码。CSS Reset 可以帮助我们消除不同浏览器之间的样式差异,让我们的页面在各种浏览器中都能够呈现一致的效果。在实际开发中,我们可以选择现成的 CSS Reset 方案,也可以根据自己的需求来自定义 CSS Reset。这样做可以提高我们的开发效率和代码质量,让我们的页面更加美观和易于维护。
Source: FunTeaLearn,Please indicate the source for reprints https://funteas.com/post/679702a5504e4ea9bde017cb