CSS样式初始化
约 890 字大约 3 分钟
2025-10-16
什么是样式初始化
样式初始化(CSS Reset / Normalize)是指在项目开始前,通过一套预定义的 CSS 规则来统一不同浏览器之间的默认样式差异,消除浏览器自带的默认样式带来的不一致性。
如果某个元素需要自定义样式时,CSS优先级会优先使用此元素自定义样式。
样式初始化的目的
消除浏览器默认样式差异
提供一致的样式基准
减少浏览器兼容性问题
提高开发效率
中大型项目的样式初始化
在中大型项目中,推荐使用 Normalize.css 做样式初始化
Vue官网就使用的 Normalize.css 初始化方案
大型项目自定义初始化方案
/* ===== 现代大型项目样式初始化方案 ===== */
/* 1. 使用更符合直觉的盒模型 */
*, *::before, *::after {
box-sizing: border-box;
}
/* 2. 重置边距和填充 */
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,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* 3. HTML5 显示定义 */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
/* 4. 根元素和滚动行为 */
html {
line-height: 1.15;
-webkit-text-size-adjust: 100%;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
scroll-behavior: smooth;
}
/* 5. 主体样式 */
body {
line-height: 1.6;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, sans-serif;
min-height: 100vh;
}
/* 6. 标题重置 */
h1, h2, h3, h4, h5, h6 {
font-size: inherit;
font-weight: inherit;
line-height: inherit;
}
/* 7. 列表重置 */
ol, ul {
list-style: none;
}
/* 8. 引用元素 */
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
/* 9. 表格重置 */
table {
border-collapse: collapse;
border-spacing: 0;
}
/* 10. 表单元素重置 */
button, input, optgroup, select, textarea {
font-family: inherit;
font-size: inherit;
line-height: inherit;
color: inherit;
margin: 0;
padding: 0;
border: none;
background: none;
outline: none;
}
/* 11. 按钮和输入框特定样式 */
button {
cursor: pointer;
background: transparent;
}
input, textarea {
background: transparent;
}
/* 12. 移除输入框类型为number时的 spinner */
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"] {
-moz-appearance: textfield;
}
/* 13. 链接样式 */
a {
color: inherit;
text-decoration: none;
background-color: transparent;
}
/* 14. 媒体元素 */
img, picture, video, canvas, svg {
display: block;
max-width: 100%;
height: auto;
}
/* 15. 代码元素 */
code, kbd, pre, samp {
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 1em;
}
/* 16. 隐藏属性选择器 */
[hidden] {
display: none !important;
}
/* 17. 焦点样式 */
:focus {
outline: 2px solid #007bff;
outline-offset: 2px;
}
/* 18. 减少动画对于偏好减少运动的用户 */
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
/* 19. 选择文本样式 */
::selection {
background-color: #b3d4fc;
color: #000000;
text-shadow: none;
}
/* 20. 禁用状态 */
[disabled] {
cursor: not-allowed;
opacity: 0.6;
}
/* 21. 响应式图片 */
img {
max-width: 100%;
height: auto;
display: block;
}
/* 22. 移除按钮在移动端的默认样式 */
button, input[type="button"], input[type="reset"], input[type="submit"] {
-webkit-appearance: button;
appearance: button;
}
/* 23. 文本域重置 */
textarea {
resize: vertical;
overflow: auto;
vertical-align: top;
}
/* 24. 字段集和图例 */
fieldset {
border: 1px solid #c0c0c0;
margin: 0 2px;
padding: 0.35em 0.625em 0.75em;
}
legend {
padding: 0;
border: 0;
}
/* 可选 补充考虑:变量定义 */
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
--success-color: #28a745;
--font-family-base: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto;
--border-radius: 4px;
--transition: all 0.3s ease;
}