定制软件CSS Reset & Modern CSS Reset

目录


什么是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):

  1. html {
  2. overflow-x:auto;
  3. overflow-y:scroll;
  4. }
  5. body, dl, dt, dd, ul, ol, li, pre, form, fieldset, input, p, blockquote, th, td {
  6. font-weight:400;
  7. margin:0;
  8. padding:0;
  9. }
  10. h1, h2, h3, h4, h4, h5 {
  11. margin:0;
  12. padding:0;
  13. }
  14. body {
  15. background-color:#FFFFFF;
  16. color:#666666;
  17. font-family:Helvetica,Arial,sans-serif;
  18. font-size:12px;
  19. padding:0 10px;
  20. text-align:left;
  21. }
  22. select {
  23. font-size:12px;
  24. }
  25. table {
  26. border-collapse:collapse;
  27. }
  28. fieldset, img {
  29. border:0 none;
  30. }
  31. fieldset {
  32. margin:0;
  33. padding:0;
  34. }
  35. fieldset p {
  36. margin:0;
  37. padding:0 0 0 8px;
  38. }
  39. legend {
  40. display:none;
  41. }
  42. address, caption, em, strong, th, i {
  43. font-style:normal;
  44. font-weight:400;
  45. }
  46. table caption {
  47. margin-left:-1px;
  48. }
  49. hr {
  50. border-bottom:1px solid #FFFFFF;
  51. border-top:1px solid #E4E4E4;
  52. border-width:1px 0;
  53. clear:both;
  54. height:2px;
  55. margin:5px 0;
  56. overflow:hidden;
  57. }
  58. ol, ul {
  59. list-style-image:none;
  60. list-style-position:outside;
  61. list-style-type:none;
  62. }
  63. caption, th {
  64. text-align:left;
  65. }
  66. q:before, q:after, blockquote:before, blockquote:after {
  67. content:””;
  68. }

2.百度(CSS Reset):

  1. body {
  2. font-family:arial,helvetica,sans-serif;
  3. font-size:13px;
  4. font-size-adjust:none;
  5. font-stretch:normal;
  6. font-style:normal;
  7. font-variant:normal;
  8. font-weight:normal;
  9. line-height:1.4;
  10. text-align:center;
  11. }
  12. body, ul, ol, dl, dd, h1, h2, h3, h4, h5, h6, p, form, fieldset, legend, input, textarea, select, button, th, td {
  13. margin:0;
  14. padding:0;
  15. }
  16. h1, h2, h3, h4, h5, h6 {
  17. font-size:100%;
  18. font-weight:normal;
  19. }
  20. table {
  21. font-size:inherit;
  22. }
  23. input, select {
  24. font-family:arial,helvetica,clean,sans-serif;
  25. font-size:100%;
  26. font-size-adjust:none;
  27. font-stretch:normal;
  28. font-style:normal;
  29. font-variant:normal;
  30. font-weight:normal;
  31. line-height:normal;
  32. }
  33. button {
  34. overflow:visible;
  35. }
  36. th, em, strong, b, address, cite {
  37. font-style:normal;
  38. font-weight:normal;
  39. }
  40. li {
  41. list-style-image:none;
  42. list-style-position:outside;
  43. list-style-type:none;
  44. }
  45. img, fieldset {
  46. border:0 none;
  47. }
  48. ins {
  49. text-decoration:none;
  50. }

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

Modern CSS Reset

核心观点

  1. 重置合理的默认值

  2. 关注用户体验

  3. 关注可访问性

整个 Reset 的源码比较简单:

  1. /* Box sizing rules */
  2. *,
  3. *::before,
  4. *::after {
  5. box-sizing: border-box;
  6. }
  7. /* Remove default margin */
  8. body,
  9. h1,
  10. h2,
  11. h3,
  12. h4,
  13. p,
  14. figure,
  15. blockquote,
  16. dl,
  17. dd {
  18. margin: 0;
  19. }
  20. /* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */
  21. ul[role='list'],
  22. ol[role='list'] {
  23. list-style: none;
  24. }
  25. /* Set core root defaults */
  26. html:focus-within {
  27. scroll-behavior: smooth;
  28. }
  29. /* Set core body defaults */
  30. body {
  31. min-height: 100vh;
  32. text-rendering: optimizeSpeed;
  33. line-height: 1.5;
  34. }
  35. /* A elements that don't have a class get default styles */
  36. a:not([class]) {
  37. text-decoration-skip-ink: auto;
  38. }
  39. /* Make images easier to work with */
  40. img,
  41. picture {
  42. max-width: 100%;
  43. display: block;
  44. }
  45. /* Inherit fonts for inputs and buttons */
  46. input,
  47. button,
  48. textarea,
  49. select {
  50. font: inherit;
  51. }
  52. /* Remove all animations, transitions and smooth scroll for people that prefer not to see them */
  53. @media (prefers-reduced-motion: reduce) {
  54. html:focus-within {
  55. scroll-behavior: auto;
  56. }
  57. *,
  58. *::before,
  59. *::after {
  60. animation-duration: 0.01ms !important;
  61. animation-iteration-count: 1 !important;
  62. transition-duration: 0.01ms !important;
  63. scroll-behavior: auto !important;
  64. }
  65. }

其中一些比较有意思的点,单看:

  1. *,
  2. *::before,
  3. *::after {
  4. box-sizing: border-box;
  5. }

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

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

  1. html:focus-within {
  2. scroll-behavior: smooth;
  3. }

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

如果我们设置了如下 CSS:

  1. `html {
  2. scroll-behavior: smooth;
  3. }`


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

再看看这段代码:

  1. @media (prefers-reduced-motion: reduce) {
  2. html:focus-within {
  3. scroll-behavior: auto;
  4. }
  5. *,
  6. *::before,
  7. *::after {
  8. animation-duration: 0.01ms !important;
  9. animation-iteration-count: 1 !important;
  10. transition-duration: 0.01ms !important;
  11. scroll-behavior: auto !important;
  12. }
  13. }

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

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

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

  1. .ele {
  2. animation: aniName 5s infinite linear;
  3. }
  4. @media (prefers-reduced-motion: reduce) {
  5. .ele {
  6. animation: none;
  7. }
  8. }

结合实际环境


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

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

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

  1. 如果你的业务场景仍然需要考虑一些老旧浏览器,依旧需要兼容 IE 系列,Normalize.css 的大部分功能都还是非常好的选择

  2. 如果你的业务场景只专注于 Chrome 或者是 Chromium 内核,Normalize.css 内的许多内容其实可能是一些实际中根本不会遇到或者用上的兼容适配,可以进行必要的精简

  3. 如果你的业务是全球化,面向的用户不仅仅在国内,你应该开始考虑更多可访问性相关的内容,上述的 Modern CSS Reset 可以借鉴一下
    因此,更应该的情况是,根据实际的业务需要,吸收多个业界比较常见 / 知名的 Reset 方案形成自己业务适用的。

小结

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

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

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

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

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

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发