CSS Reset 是一种常见的前端技术,它的目的是消除不同浏览器之间的默认样式差异,以确保网站在各种浏览器中呈现一致的外观和行为。在实践中,有两种方法可以实现 CSS Reset:手写 CSS Reset 和使用成熟的 CSS Reset 库。那么,你会选择哪一种呢?
手写 CSS Reset
手写 CSS Reset 是一种自己编写 CSS 样式来消除默认样式差异的方法。这种方法的好处是可以完全掌控样式,使得网站的样式更加符合自己的需求。此外,手写 CSS Reset 还可以提高自己的 CSS 水平,了解各种浏览器之间的差异,以及如何处理它们。
下面是一个简单的手写 CSS Reset 的示例代码:
// www.javascriptcn.com code example
/* Reset all margins, padding, and borders */
* {
margin: 0;
padding: 0;
border: none;
box-sizing: border-box;
}
/* Set default font and color */
body {
font-family: Arial, sans-serif;
font-size: 16px;
color: #333;
}
/* Remove list-style */
ul {
list-style: none;
}
/* Remove underline from links */
a {
text-decoration: none;
}
/* Set default link color */
a:link, a:visited {
color: #0077cc;
}
/* Set default hover color */
a:hover, a:active {
color: #0055aa;
}使用成熟的 CSS Reset 库
使用成熟的 CSS Reset 库是一种使用别人编写好的 CSS 样式来消除默认样式差异的方法。这种方法的好处是可以省去自己编写 CSS 样式的时间和精力,同时可以使用其他人编写的经过测试和优化的样式,使得网站的样式更加适合各种浏览器。
下面是一个常见的 CSS Reset 库 - Normalize.css 的示例代码:
// 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;
}
/**
* Correct the inability to style clickable types in iOS and Safari.
*/
button,
[type="button"],
[type="reset"],
[type="submit"] {
-webkit-appearance: button;
}
/**
* 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;
}
/**
* Correct the text wrapping in Edge and IE.
*/
::-ms-input-placeholder {
color: inherit;
opacity: 0.5;
}
::-ms-input-placeholder {
color: inherit;
opacity: 0.5;
}
/**
* Correct the cursor style of increment and decrement buttons in Chrome.
*/
::-webkit-inner-spin-button,
::-webkit-outer-spin-button {
height: auto;
}
/**
* Correct the text style of placeholders in Chrome, Edge, and Safari.
*/
::-webkit-input-placeholder {
color: inherit;
opacity: 0.5;
}
::-webkit-input-placeholder {
color: inherit;
opacity: 0.5;
}
/**
* Remove the inner padding in Chrome and Safari on macOS.
*/
::-webkit-search-decoration {
-webkit-appearance: none;
}
/**
* 1. Correct the inability to style clickable types in iOS and Safari.
* 2. Change font properties to `inherit` in Safari.
*/
::-webkit-file-upload-button {
-webkit-appearance: button; /* 1 */
font: inherit; /* 2 */
}
/* 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;
}选择哪一种?
选择手写 CSS Reset 还是使用成熟的 CSS Reset 库,取决于你的需求和实际情况。如果你需要完全掌控样式,或者想要提高自己的 CSS 水平,那么手写 CSS Reset 是一个不错的选择。如果你想要快速消除默认样式差异,并且使用经过测试和优化的样式,那么使用成熟的 CSS Reset 库是一个更好的选择。
无论你选择哪一种方法,都应该注意一些细节。首先,应该了解不同浏览器之间的默认样式差异,以便正确地编写 CSS Reset。其次,应该在 CSS Reset 之后编写自己的样式,以确保网站的样式符合自己的需求。最后,应该在不同浏览器和设备上测试网站,以确保网站在各种情况下都能呈现一致的外观和行为。
总之,手写 CSS Reset 和使用成熟的 CSS Reset 库都有各自的优缺点。选择哪一种方法,取决于你的需求和实际情况。无论你选择哪一种方法,都应该注意一些细节,以确保网站的样式符合自己的需求,并且在各种浏览器和设备上呈现一致的外观和行为。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/67d54b37a941bf71349ca035