CSS Reset & Modern CSS Reset

简介: CSS Reset & Modern CSS Reset

d8c57e2ca3574763a752234595c810e5.jpg

什么是CSS Reset


CSS Reset,又叫做 CSS 重写或者 CSS 重置,意为重置默认样式。HTML中绝大部分标签元素在网页显示中都有一个默认属性值,通常为了避免重复定义元素样式,需要进行重置默认样式—— CSS Reset

CSS Reset为什么存在?


不同核心的浏览器对CSS的解析效果呈现各异,导致所期望的效果跟浏览器的“理解”效果有偏差,而 CSS Reset 就是用来重置(复位)元素在不同核心浏览器下的默认值以尽量保证元素在不同浏览器下的同一“起跑线”的样式方法

CSS Reset该怎么写?


对于不同的项目的 CSS Reset 也会有变化。CSS Reset 应当是个人积累的经验的总结,重置一些经常使用的,在不断学习中修改。


那作为初学者该如何写呢?首先可以参考一些网站的 CSS Reset ,切不可复制过来直接用。应当像砌墙一样,一块砖一块砖的积累。当你觉得你每次都要写这个重置样式,那就加进你的 CSS Reset表吧。

CSSReset示例


1.淘宝(CSS Reset):


html {
overflow-x:auto;
overflow-y:scroll;
}
body, dl, dt, dd, ul, ol, li, pre, form, fieldset, input, p, blockquote, th, td {
font-weight:400;
margin:0;
padding:0;
}
h1, h2, h3, h4, h4, h5 {
margin:0;
padding:0;
}
body {
background-color:#FFFFFF;
color:#666666;
font-family:Helvetica,Arial,sans-serif;
font-size:12px;
padding:0 10px;
text-align:left;
}
select {
font-size:12px;
}
table {
border-collapse:collapse;
}
fieldset, img {
border:0 none;
}
fieldset {
margin:0;
padding:0;
}
fieldset p {
margin:0;
padding:0 0 0 8px;
}
legend {
display:none;
}
address, caption, em, strong, th, i {
font-style:normal;
font-weight:400;
}
table caption {
margin-left:-1px;
}
hr {
border-bottom:1px solid #FFFFFF;
border-top:1px solid #E4E4E4;
border-width:1px 0;
clear:both;
height:2px;
margin:5px 0;
overflow:hidden;
}
ol, ul {
list-style-image:none;
list-style-position:outside;
list-style-type:none;
}
caption, th {
text-align:left;
}
q:before, q:after, blockquote:before, blockquote:after {
content:””;
}

2.百度(CSS Reset):


body {
font-family:arial,helvetica,sans-serif;
font-size:13px;
font-size-adjust:none;
font-stretch:normal;
font-style: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;
}

而我最近比较喜欢的一个 CSS Reset 方案,源自于—— Modern-CSS-Reset 。

Modern CSS Reset


核心观点


  1. 重置合理的默认值
  2. 关注用户体验
  1. 关注可访问性

整个 Reset 的源码比较简单:


/* Box sizing rules */
*,
*::before,
*::after {
  box-sizing: border-box;
}
/* Remove default margin */
body,
h1,
h2,
h3,
h4,
p,
figure,
blockquote,
dl,
dd {
  margin: 0;
}
/* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */
ul[role='list'],
ol[role='list'] {
  list-style: none;
}
/* Set core root defaults */
html:focus-within {
  scroll-behavior: smooth;
}
/* Set core body defaults */
body {
  min-height: 100vh;
  text-rendering: optimizeSpeed;
  line-height: 1.5;
}
/* A elements that don't have a class get default styles */
a:not([class]) {
  text-decoration-skip-ink: auto;
}
/* Make images easier to work with */
img,
picture {
  max-width: 100%;
  display: block;
}
/* Inherit fonts for inputs and buttons */
input,
button,
textarea,
select {
  font: inherit;
}
/* Remove all animations, transitions and smooth scroll for people that prefer not to see them */
@media (prefers-reduced-motion: reduce) {
  html:focus-within {
   scroll-behavior: auto;
  }
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

其中一些比较有意思的点,单看盒子模型:


*,
*::before,
*::after {
  box-sizing: border-box;
}

Normalize.css 是不推荐这么做的,大部分元素的 box-sizing 其实都是 content-box,但是,对于实际开发,全部元素都设置为 border-box 其实是更便于操作的一种方式。

再看看在用户体验及可访问性方面的一些做法:


html:focus-within {
  scroll-behavior: smooth;
}

scroll-behavior: smooth 意为平滑滚动,当然这里是设置给了 html:focus-within 伪类,而不是直接给 html 赋予平滑滚动,这样做的目的是只对使用键盘 tab 键切换焦点页面时,让页面进行平滑滚动切换,带来更好的使用体验。


如果我们设置了如下 CSS:

`html {
scroll-behavior: smooth;
}`

可能会起到一起副作用,譬如,当我们在页面查找元素时候(使用 Ctrl + F、或者 Mac 的 Commond + F),这段 CSS 代码可能会严重延缓我们的查找速度:

再看看这段代码:

@media (prefers-reduced-motion: reduce) {
  html:focus-within {
   scroll-behavior: auto;
  }
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

prefers-reduced-motion 规则查询用于减弱动画效果,除了默认规则,只有一种语法取值 prefers-reduced-motion: reduce,开启了该规则后,相当于告诉用户代理,希望他看到的页面,可以删除或替换掉一些会让部分视觉运动障碍者不适的动画类型。

vestibular motion disorders 是一种视觉运动障碍患者,翻译出来是前庭运动障碍,是一种会导致眩晕的一类病症,譬如一个动画一秒闪烁多次,就会导致患者的不适。

使用方法,还是上面那段代码:

.ele {
    animation: aniName 5s infinite linear;
}
@media (prefers-reduced-motion: reduce) {
    .ele {
        animation: none;
    }
}

结合实际环境


当然,结合实际环境,目前国内整体不太注重可访问性相关的内容。

而且,许多业务根本无法抛弃一些老旧浏览器,仍然需要兼容 IE 系列。

因此,对于现阶段的 Reset 方案,可以灵活搭配:

  1. 如果你的业务场景仍然需要考虑一些老旧浏览器,依旧需要兼容 IE 系列,Normalize.css 的大部分功能都还是非常好的选择
  2. 如果你的业务场景只专注于 Chrome 或者是 Chromium 内核,Normalize.css 内的许多内容其实可能是一些实际中根本不会遇到或者用上的兼容适配,可以进行必要的精简
  3. 如果你的业务是全球化,面向的用户不仅仅在国内,你应该开始考虑更多可访问性相关的内容,上述的 Modern CSS Reset 可以借鉴一下
    因此,更应该的情况是,根据实际的业务需要,吸收多个业界比较常见 / 知名的 Reset 方案形成自己业务适用的。

小结


前端早期,各浏览器对 CSS 的规范支持和理解是不同的,为了解决网站风格的统一,出现了 CSS Reset 。


早期的 CSS Reset 无脑兼容各浏览器被称为硬重置,因为硬重置会导致许多不必要的覆盖和放弃了浏览器原生支持的特性,就被弃用了。


这时候出现了软重置,它只简单的规范了各种元素的样式和纠正一些错误,做到对元素最小的侵入,而软重置中的翘楚就是 Normalize.css。


最后的集大成者是个性化重置,就是我们平时用到第三方 UI 框架。


最后说一句,CSS Reset 是完全需要的,不然会导致网址布局的混乱,但同时记住不要使用硬重置做过多的事情,来保护 CSS 的原生特性。

目录
相关文章
|
前端开发
现代浏览器样式重置CSS Reset 方案汇总整理
现代浏览器样式重置CSS Reset 方案汇总整理
现代浏览器样式重置CSS Reset 方案汇总整理
|
前端开发
Normalize.css:CSS reset的替代方案
Normalize.css:CSS reset的替代方案
119 0
|
前端开发
重置浏览器样式CSS Tools: Reset CSS
重置浏览器样式CSS Tools: Reset CSS
|
Web App开发 前端开发 UED
现代 CSS 解决方案:Modern CSS Reset
现代 CSS 解决方案:Modern CSS Reset
176 0
现代 CSS 解决方案:Modern CSS Reset
|
前端开发 BI 移动开发
移动端reset.css
说明 移动端reset.css,来自张鑫旭网站的推荐,下载地址:https://huruqing.gitee.io/demos/source/reset.
1873 0
|
前端开发
css - 移动端reset汇总与注释
1.解决移动端触摸a元素,会有蓝色阴影 正常状态:         点击时状态:   a{ outline:none; -webkit-tap-highlight-color: rgba(0,0,0,0); } -webkit-tap-highlight-color: rgba(0,0,0,0);可以将该阴影透明化   2.
977 0
|
前端开发 移动开发 iOS开发
|
前端开发 iOS开发 Android开发
移动端APP CSS Reset及注意事项CSS重置
@charset "utf-8"; * {    -webkit-box-sizing: border-box;     box-sizing: border-box; } //禁止文本缩放 字体设置  取消touch高亮效果 html {     width: 100%;     height: ...
1062 0
|
前端开发 开发者
CSS Reset
为什么要重置 浏览器默认 CSS? 浏览器会对『标签』进行『默认设置』,不同浏览器的默认设置存在差异。 很多时候,浏览器的默认设置对我们开发者来说是没有用的。 那么,就需要开发者写覆盖的css将 浏览器的默认设置覆盖掉。
1224 0
|
2天前
|
移动开发 HTML5
HTML5/CSS3粒子效果进度条代码
HTML5/CSS3进度条应用。这款进度条插件在播放进度过程中出现粒子效果,就像一些小颗粒从进度条上散落下来
15 0
HTML5/CSS3粒子效果进度条代码