什么是 CSS?

级联样式表 (CSS) 是一种样式表语言,用于描述以 HTML 等标记语言编写的文档的样式。CSS 是万维网的主要技术之一,与 HTML 和 JavaScript 并驾齐驱。
CSS 旨在实现样式和内容的分离,包括布局、颜色和字体。这种分离可以提高内容的可访问性,在样式特性的规范中提供更多的灵活性和控制,通过在单独的 .css 文件中指定相关的 CSS 使多个网页能够共享样式,并降低结构内容的复杂性和重复性。

重置样式1

  1. html ,body ,div ,span ,applet ,object ,iframe ,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 ,main ,menu ,nav ,output ,ruby ,section ,summary ,time ,mark ,audio ,video {margin: 0;padding: 0;vertical-align: baseline;border: none;}
  2. body ,textarea ,input ,button ,select ,keygen ,legend {font: 14px/1.5 'Microsoft YaHei UI','Microsoft YaHei',Roboto,Helvetica,Arial,Verdana,sans-serif;outline: none;}
  3. article ,aside ,details ,figcaption ,figure ,footer ,header ,hgroup ,main ,menu ,nav ,section {display: block;}
  4. blockquote:before ,blockquote:after ,q:before ,q:after {content: none;}
  5. table {width: 100%;border-spacing: 0;border-collapse: collapse;}
  6. input ,select {outline: none;appearance: none;}
  7. h1 ,h2 ,h3 ,h4 ,h5 ,h6 ,em ,strong ,b {font-weight: bold;}
  8. del ,ins ,u ,s ,a ,a:hover {text-decoration: none;}
  9. * ,:after ,:before {box-sizing: border-box;-webkit-tap-highlight-color: transparent;}
  10. textarea {overflow: auto;resize: none;}
  11. blockquote ,q {quotes: none;}
  12. a ,button {cursor: pointer;}
  13. *[hidden] {display: none;}
  14. ol ,ul ,li {list-style: none;}
  15. html ,body {width: 100%;height: 100%;}
  16. /* 滚动条 */
  17. ::-webkit-scrollbar-track {border-radius: 10px;background-color: #fff;box-shadow: inset 0 0 6px rgba(0,0,0,.3);}
  18. ::-webkit-scrollbar-thumb {border-radius: 10px;background-color: #999;box-shadow: inset 0 0 6px rgba(0,0,0,.3);}
  19. ::-webkit-scrollbar {width: 6px;height: 6px;background-color: #f5f5f5;}

重置样式2

  1. html, body, div, span, applet, object, iframe,
  2. h1, h2, h3, h4, h5, h6, p, blockquote, pre,
  3. a, abbr, acronym, address, big, cite, code,
  4. del, dfn, em, img, ins, kbd, q, s, samp,
  5. small, strike, strong, sub, sup, tt, var,
  6. b, u, i, center,
  7. dl, dt, dd, menu, ol, ul, li,
  8. fieldset, form, label, legend,
  9. table, caption, tbody, tfoot, thead, tr, th, td,
  10. article, aside, canvas, details, embed,
  11. figure, figcaption, footer, header, hgroup,
  12. main, menu, nav, output, ruby, section, summary,
  13. time, mark, audio, video {
  14. margin: 0;
  15. padding: 0;
  16. border: 0;
  17. font-size: 100%;
  18. font: inherit;
  19. vertical-align: baseline;
  20. }
  21. /* HTML5 display-role reset for older browsers */
  22. article, aside, details, figcaption, figure,
  23. footer, header, hgroup, main, menu, nav, section {
  24. display: block;
  25. }
  26. /* HTML5 hidden-attribute fix for newer browsers */
  27. *[hidden] {
  28. display: none;
  29. }
  30. body {
  31. line-height: 1;
  32. }
  33. menu, ol, ul {
  34. list-style: none;
  35. }
  36. blockquote, q {
  37. quotes: none;
  38. }
  39. blockquote:before, blockquote:after,
  40. q:before, q:after {
  41. content: '';
  42. content: none;
  43. }
  44. table {
  45. border-collapse: collapse;
  46. border-spacing: 0;
  47. }

css中的选择器

~(波浪号)
p~ul选择器 p之后出现的所有ul。
两种元素必须拥有相同的父元素,但是 ul不必直接紧随 p。

+(加号)

  • 二者有相同的父元素,
  • 选择紧接在另一个元素后的元素

>(大于号)
是css3特有的选择器,
A>B 表示选择A元素的所有子B元素。

打字效果

您知道吗?其实您可以使用零 JavaScript 来创建打字效果。
CSS - 图1

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. .wrapper {
  10. height: 100vh;
  11. /*This part is important for centering*/
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. }
  16. .typing-demo {
  17. width: 22ch;
  18. animation: typing 2s steps(22), blink 0.5s step-end infinite alternate;
  19. white-space: nowrap;
  20. overflow: hidden;
  21. border-right: 3px solid;
  22. font-family: monospace;
  23. font-size: 2em;
  24. }
  25. @keyframes typing {
  26. from {
  27. width: 0;
  28. }
  29. }
  30. @keyframes blink {
  31. 50% {
  32. border-color: transparent;
  33. }
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="wrapper">
  39. <div class="typing-demo">这是一个打字演示。</div>
  40. </div>
  41. </body>
  42. </html>

图片非透明区域添加投影

当您使用透明图像时,您可以使用 drop-shadow() 过滤器函数在图像内容上创建阴影,而不是使用 box-shadow 属性在元素的整个框后面创建矩形阴影:filter: drop-shadow(2px 4px 8px #585858);
CSS - 图2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. .wrapper {
  10. height: 100vh;
  11. display: flex;
  12. align-items: center;
  13. justify-content: center;
  14. }
  15. .mr-2 {
  16. margin-right: 2em;
  17. }
  18. .mb-1 {
  19. margin-bottom: 1em;
  20. }
  21. .text-center {
  22. text-align: center;
  23. }
  24. .box-shadow {
  25. box-shadow: 2px 4px 8px #585858;
  26. }
  27. .drop-shadow {
  28. filter: drop-shadow(2px 4px 8px #585858);
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="wrapper">
  34. <div class="mr-2">
  35. <div class="mb-1 text-center">box-shadow</div>
  36. <img
  37. class="box-shadow"
  38. src="https://markodenic.com/man_working.png"
  39. alt="Image with box-shadow"
  40. />
  41. </div>
  42. <div>
  43. <div class="mb-1 text-center">drop-shadow</div>
  44. <img
  45. class="drop-shadow"
  46. src="https://markodenic.com/man_working.png"
  47. alt="Image with drop-shadow"
  48. />
  49. </div>
  50. </div>
  51. </body>
  52. </html>

平滑滚动

不需要 JavaScript 就可以实现平滑滚动,只需一行 CSS。
CSS - 图3

  1. /* 喜欢的同学一定要点赞、转发、收藏啊! */
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" />
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  8. <title>Document</title>
  9. <style>
  10. html {
  11. scroll-behavior: smooth;
  12. }
  13. nav {
  14. position: fixed;
  15. left: calc(50vw - 115px);
  16. top: 0;
  17. width: 200px;
  18. text-align: center;
  19. padding: 15px;
  20. background: #fff;
  21. box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.2);
  22. }
  23. nav .link {
  24. padding: 5px;
  25. color: white;
  26. }
  27. .section {
  28. height: 100vh;
  29. display: flex;
  30. align-items: center;
  31. justify-content: center;
  32. color: #fff;
  33. font-size: 5em;
  34. text-shadow: 0px 2px 0px #b2a98f, 0px 4px 3px rgba(0, 0, 0, 0.15),
  35. 0px 8px 1px rgba(0, 0, 0, 0.1);
  36. }
  37. .bg-red {
  38. background: #de5448;
  39. }
  40. .bg-blue {
  41. background: #4267b2;
  42. }
  43. .bg-green {
  44. background: #4caf50;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <nav>
  50. Scroll to:
  51. <a href="#sectionA" class="link bg-red">A</a>
  52. <a href="#sectionB" class="link bg-blue">B</a>
  53. <a href="#sectionC" class="link bg-green">C</a>
  54. </nav>
  55. <div class="wrapper">
  56. <div id="sectionA" class="section bg-red">A</div>
  57. <div id="sectionB" class="section bg-blue">B</div>
  58. <div id="sectionC" class="section bg-green">C</div>
  59. </div>
  60. </body>
  61. </html>

图片光标

您知道如何使用自己的图像,甚至是表情符号作为光标吗?
CSS - 图4

  1. /* 喜欢的同学一定要点赞、转发、收藏啊! */
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" />
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  8. <title>Document</title>
  9. <style>
  10. .wrapper {
  11. display: flex;
  12. height: 100vh;
  13. align-items: center;
  14. justify-content: center;
  15. background: #4776e6;
  16. background: -webkit-linear-gradient(to right, #4776e6, #8e54e9);
  17. background: linear-gradient(to right, #4776e6, #8e54e9);
  18. padding: 0 10px;
  19. }
  20. .tile {
  21. width: 200px;
  22. height: 200px;
  23. display: flex;
  24. align-items: center;
  25. justify-content: center;
  26. background-color: #de5448;
  27. margin-right: 10px;
  28. color: #fff;
  29. font-size: 1.4em;
  30. text-align: center;
  31. }
  32. .tile--image-cursor {
  33. background-color: #1da1f2;
  34. cursor: url(https://picsum.photos/20/20), auto;
  35. }
  36. .tile--emoji-cursor {
  37. background-color: #4267b2;
  38. cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>🚀</text></svg>"),
  39. auto;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div class="wrapper">
  45. <div class="tile">Default</div>
  46. <div class="tile tile--image-cursor">Image Cursor</div>
  47. <div class="tile tile--emoji-cursor">Emoji Cursor</div>
  48. </div>
  49. </body>
  50. </html>

文本保留特定行数其余内容省略

您可以使用“-webkit-line-clamp”属性将文本截断为特定的行数。文本被夹住的点将显示一个省略号。
CSS - 图5

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. body {
  10. padding: 20px;
  11. font-family: "Open Sans", sans-serif;
  12. }
  13. .tile {
  14. background: linear-gradient(to right, #2b32b2, #1488cc);
  15. padding: 15px;
  16. margin-bottom: 15px;
  17. padding: 15px;
  18. width: 300px;
  19. color: #fff;
  20. }
  21. .line-clamp {
  22. display: -webkit-box;
  23. -webkit-box-orient: vertical;
  24. -webkit-line-clamp: 3; /* Change this line if you want. In this case it trimmed the text to 3 lines. */
  25. overflow: hidden;
  26. }
  27. .line-clamp--four {
  28. -webkit-line-clamp: 4; /* Trimmed the second tile to four lines. */
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. 文本保留 3 行
  34. <div class="tile">
  35. <p class="line-clamp">
  36. 可以使用 <code>-webkit-line-clamp</code> 属性截断文本到特定行数。屏幕 3
  37. 行显示不完全,将显示省略号。喜欢的同学一定要点赞、转发、收藏啊 !
  38. 喜欢的同学一定要点赞、转发、收藏啊 !
  39. </p>
  40. </div>
  41. 文本保留 4 行
  42. <div class="tile">
  43. <p class="line-clamp line-clamp--four">
  44. 可以使用 <code>-webkit-line-clamp</code> 属性截断文本到特定行数。屏幕 4
  45. 行显示不完全,将显示省略号。喜欢的同学一定要点赞、转发、收藏啊 !
  46. 喜欢的同学一定要点赞、转发、收藏啊 !
  47. </p>
  48. </div>
  49. 文本全部显示
  50. <div class="tile">
  51. <p>
  52. 可以使用 <code>-webkit-line-clamp</code> 属性截断文本到特定行数。屏幕 4
  53. 行显示不完全,将显示省略号。喜欢的同学一定要点赞、转发、收藏啊 !
  54. 喜欢的同学一定要点赞、转发、收藏啊 !
  55. </p>
  56. </div>
  57. </body>
  58. </html>

::selection CSS 伪元素

::selection CSS 伪元素可以自定义控制鼠标选中部分内容的样式。
CSS - 图6

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. .wrapper {
  10. height: 100vh;
  11. display: flex;
  12. align-items: center;
  13. justify-content: center;
  14. }
  15. p {
  16. font-size: 2rem;
  17. font-family: sans-serif;
  18. }
  19. .custom-highlighting::selection {
  20. background-color: #8e44ad;
  21. color: #fff;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="wrapper">
  27. <div>
  28. <p>这是默认高亮显示。鼠标选中试试看。</p>
  29. <p class="custom-highlighting">这是自定义高亮显示。鼠标选中试试看。</p>
  30. </div>
  31. </div>
  32. </body>
  33. </html>

万物皆可伸缩

你知道任何元素调整大小,都可以像 <textarea> 一样吗?
CSS - 图7

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. div {
  8. border: 2px solid;
  9. padding: 10px 40px;
  10. width: 300px;
  11. /* 核心代码 */
  12. resize: both;
  13. overflow: auto;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <p><b>注意:</b> Firefox, Safari,和 Chrome 兼容 resize 属性.</p>
  19. <div>调整属性指定一个元素是否由用户可调整大小的。</div>
  20. </body>
  21. </html>

CSS 模态框

你可以使用 :target 伪类来创建零 JavaScript 的模态。
CSS - 图8

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. .wrapper {
  8. height: 100vh;
  9. /* This part is important for centering the content */
  10. display: flex;
  11. align-items: center;
  12. justify-content: center;
  13. /* End center */
  14. background: -webkit-linear-gradient(to right, #834d9b, #d04ed6);
  15. background: linear-gradient(to right, #834d9b, #d04ed6);
  16. }
  17. .wrapper a {
  18. display: inline-block;
  19. text-decoration: none;
  20. padding: 15px;
  21. background-color: #fff;
  22. border-radius: 3px;
  23. text-transform: uppercase;
  24. color: #585858;
  25. font-family: "Roboto", sans-serif;
  26. }
  27. .modal {
  28. visibility: hidden;
  29. opacity: 0;
  30. position: absolute;
  31. top: 0;
  32. right: 0;
  33. bottom: 0;
  34. left: 0;
  35. display: flex;
  36. align-items: center;
  37. justify-content: center;
  38. background: rgba(77, 77, 77, 0.7);
  39. transition: all 0.4s;
  40. }
  41. .modal:target {
  42. visibility: visible;
  43. opacity: 1;
  44. }
  45. .modal__content {
  46. border-radius: 4px;
  47. position: relative;
  48. width: 500px;
  49. max-width: 90%;
  50. background: #fff;
  51. padding: 1em 2em;
  52. }
  53. .modal__footer {
  54. text-align: right;
  55. }
  56. .modal__close {
  57. position: absolute;
  58. top: 10px;
  59. right: 10px;
  60. color: #585858;
  61. text-decoration: none;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div class="wrapper">
  67. <a href="#demo-modal">打开模态框</a>
  68. </div>
  69. <div id="demo-modal" class="modal">
  70. <div class="modal__content">
  71. <h1>CSS 实现模态框</h1>
  72. <p>
  73. 您可以使用 :target 伪类创建零 JavaScript 的模态框。赶紧一起玩一玩啊!
  74. </p>
  75. <div class="modal__footer">
  76. Made with <i class="fa fa-heart"></i>, by
  77. <a href="https://juejin.cn/user/958429872534056/posts" target="_blank"
  78. >@gyx_这个杀手不太冷静</a
  79. >
  80. </div>
  81. <a href="#" class="modal__close">&times;</a>
  82. </div>
  83. </div>
  84. </body>
  85. </html>

您可以创建自定义样式滚动条

CSS - 图9

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. .wrapper {
  8. height: 100vh;
  9. /*This part is important for centering*/
  10. display: flex;
  11. align-items: center;
  12. justify-content: center;
  13. }
  14. .mr-1 {
  15. margin-right: 1em;
  16. }
  17. .tile {
  18. overflow: auto;
  19. display: inline-block;
  20. background-color: #ccc;
  21. height: 200px;
  22. width: 180px;
  23. }
  24. .tile--custom-scrollbar::-webkit-scrollbar {
  25. width: 12px;
  26. background-color: #eff1f5;
  27. }
  28. .tile--custom-scrollbar::-webkit-scrollbar-track {
  29. border-radius: 3px;
  30. background-color: transparent;
  31. }
  32. .tile--custom-scrollbar::-webkit-scrollbar-thumb {
  33. border-radius: 5px;
  34. background-color: #515769;
  35. border: 2px solid #eff1f5;
  36. }
  37. .tile-content {
  38. padding: 20px;
  39. height: 500px;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div class="wrapper">
  45. <div>
  46. <div class="tile mr-1">
  47. <div class="tile-content">默认滚动条</div>
  48. </div>
  49. <div class="tile tile--custom-scrollbar">
  50. <div class="tile-content">自定义滚动条</div>
  51. </div>
  52. </div>
  53. </div>
  54. </body>
  55. </html>

position: sticky 吸顶效果

您可以使用 2 行 CSS 实现导航栏吸顶效果。
CSS - 图10

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. .section {
  8. font-size: 1.4em;
  9. letter-spacing: 0.05em;
  10. line-height: 1.5em;
  11. }
  12. .section h2,
  13. .section h3 {
  14. text-align: center;
  15. margin: 0;
  16. font-weight: normal;
  17. }
  18. .section__header {
  19. position: sticky;
  20. top: 0;
  21. padding: 1em;
  22. color: #a99160;
  23. background: #171717;
  24. }
  25. .section__content {
  26. padding: 1em;
  27. display: flex;
  28. justify-content: center;
  29. }
  30. .section__content > div {
  31. max-width: 992px;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <section class="section">
  37. <div class="section__header">
  38. <h2>第 1 章导航条</h2>
  39. </div>
  40. <div class="section__content">
  41. <div>
  42. <h3>第 1 章</h3>
  43. <p>
  44. 元素根据正常文档流进行定位,然后相对它的*最近滚动祖先(nearest
  45. scrolling ancestor)和 containing block (最近块级祖先 nearest
  46. block-level ancestor),包括 table-related 元素,基于 top, right,
  47. bottom, 和 left 的值进行偏移。偏移值不会影响任何其他元素的位置。
  48. </p>
  49. <p>
  50. 该值总是创建一个新的层叠上下文(stacking context)。注意,一个
  51. sticky
  52. 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的
  53. overflow 是 hidden, scroll, auto, 或 overlay
  54. 时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见
  55. Github issue on W3C CSSWG)。
  56. </p>
  57. </div>
  58. </div>
  59. </section>
  60. <section class="section">
  61. <div class="section__header">
  62. <h2>第 2 章导航条</h2>
  63. </div>
  64. <div class="section__content">
  65. <div>
  66. <h3>第 2 章</h3>
  67. <p>
  68. 元素根据正常文档流进行定位,然后相对它的*最近滚动祖先(nearest
  69. scrolling ancestor)和 containing block (最近块级祖先 nearest
  70. block-level ancestor),包括 table-related 元素,基于 top, right,
  71. bottom, 和 left 的值进行偏移。偏移值不会影响任何其他元素的位置。
  72. </p>
  73. <p>
  74. 该值总是创建一个新的层叠上下文(stacking context)。注意,一个
  75. sticky
  76. 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的
  77. overflow 是 hidden, scroll, auto, 或 overlay
  78. 时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见
  79. Github issue on W3C CSSWG)。
  80. </p>
  81. </div>
  82. </div>
  83. </section>
  84. <section class="section">
  85. <div class="section__header">
  86. <h2>第 3 章导航条</h2>
  87. </div>
  88. <div class="section__content">
  89. <div>
  90. <h3>第 3 章</h3>
  91. <p>
  92. 元素根据正常文档流进行定位,然后相对它的*最近滚动祖先(nearest
  93. scrolling ancestor)和 containing block (最近块级祖先 nearest
  94. block-level ancestor),包括 table-related 元素,基于 top, right,
  95. bottom, 和 left 的值进行偏移。偏移值不会影响任何其他元素的位置。
  96. </p>
  97. <p>
  98. 该值总是创建一个新的层叠上下文(stacking context)。注意,一个
  99. sticky
  100. 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的
  101. overflow 是 hidden, scroll, auto, 或 overlay
  102. 时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见
  103. Github issue on W3C CSSWG)。
  104. </p>
  105. </div>
  106. </div>
  107. </section>
  108. <section class="section">
  109. <div class="section__header">
  110. <h2>第 4 章导航条</h2>
  111. </div>
  112. <div class="section__content">
  113. <div>
  114. <h3>第 4 章</h3>
  115. <p>
  116. 元素根据正常文档流进行定位,然后相对它的*最近滚动祖先(nearest
  117. scrolling ancestor)和 containing block (最近块级祖先 nearest
  118. block-level ancestor),包括 table-related 元素,基于 top, right,
  119. bottom, 和 left 的值进行偏移。偏移值不会影响任何其他元素的位置。
  120. </p>
  121. <p>
  122. 该值总是创建一个新的层叠上下文(stacking context)。注意,一个
  123. sticky
  124. 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的
  125. overflow 是 hidden, scroll, auto, 或 overlay
  126. 时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见
  127. Github issue on W3C CSSWG)。
  128. </p>
  129. </div>
  130. </div>
  131. </section>
  132. </body>
  133. </html>

CSS 滚动捕捉优化滚动体验

您可以控制 scroll-snap-type 值来获取良好的滚动体验:

这个可能要亲自体验,滚动鼠标,才可以感受到效果!

CSS - 图11

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. body {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. .wrapper {
  12. height: 100vh;
  13. overflow: auto;
  14. scroll-snap-type: y mandatory;
  15. }
  16. .section {
  17. scroll-snap-align: center;
  18. height: 100vh;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. background-color: #de5448;
  23. color: #fff;
  24. font-size: 3em;
  25. }
  26. .bg-blue {
  27. background: #4267b2;
  28. }
  29. .bg-green {
  30. background: #4caf50;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="wrapper">
  36. <div class="section">Content 1</div>
  37. <div class="section bg-blue">Content 2</div>
  38. <div class="section bg-green">Content 3</div>
  39. <div class="section">Content 4</div>
  40. <div class="section bg-blue">Content 5</div>
  41. </div>
  42. </body>
  43. </html>

CSS 动态文字提示

使用 attr() CSS 函数创建动态的仅用 CSS 实现文字提示。
CSS - 图12

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. body {
  8. padding: 20px;
  9. }
  10. .tooltip {
  11. position: relative;
  12. border-bottom: 1px dotted black;
  13. }
  14. /* Tooltip box */
  15. .tooltip:before {
  16. content: attr(data-tooltip);
  17. position: absolute;
  18. width: 100px;
  19. background-color: #062b45;
  20. color: #fff;
  21. text-align: center;
  22. padding: 10px;
  23. line-height: 1.2;
  24. border-radius: 6px;
  25. z-index: 1;
  26. opacity: 0;
  27. transition: opacity 0.6s;
  28. bottom: 125%;
  29. left: 50%;
  30. margin-left: -60px;
  31. font-size: 0.75em;
  32. visibility: hidden;
  33. }
  34. /* Tooltip arrow */
  35. .tooltip:after {
  36. content: "";
  37. position: absolute;
  38. bottom: 75%;
  39. left: 50%;
  40. margin-left: -5px;
  41. border-width: 5px;
  42. border-style: solid;
  43. opacity: 0;
  44. transition: opacity 0.6s;
  45. border-color: #062b45 transparent transparent transparent;
  46. visibility: hidden;
  47. }
  48. .tooltip:hover:before,
  49. .tooltip:hover:after {
  50. opacity: 1;
  51. visibility: visible;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <h1>HTML/CSS 文字提示</h1>
  57. <p>
  58. 鼠标移入<span class="tooltip" data-tooltip="这是一个文字提示">这里</span
  59. >显示文字提示。
  60. </p>
  61. <p>
  62. 你也可以移入<span class="tooltip" data-tooltip="这是另一个文字提示"
  63. >这里</span
  64. >显示另一个文字提示。
  65. </p>
  66. </body>
  67. </html>

输入框光标颜色

您可以更改文本输入框光标的颜色。
CSS - 图13

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. input {
  8. caret-color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <input type="text" />
  14. </body>
  15. </html>

::in-range::out-of-range 伪类

使用 ::in-range::out-of-range 伪类来设置当前值超出 minmax 属性指定的范围限制的输入的样式。
CSS - 图14

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. input:in-range {
  8. background-color: rgba(0, 255, 0, 0.25);
  9. border: 1px solid green;
  10. }
  11. input:out-of-range {
  12. background-color: rgba(255, 0, 0, 0.25);
  13. border: 1px solid red;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <form>
  19. <label for="one">输入 1 - 10 之间的一个数值</label>
  20. <input type="number" id="one" name="one" min="1" max="10" />
  21. <br />
  22. <label for="two">输入 1 - 10 之间的一个数值</label>
  23. <input type="number" id="two" name="two" min="1" max="10" />
  24. </form>
  25. </body>
  26. </html>

花里胡哨的文字

使用 background-clip 属性创建漂亮的标题。
CSS - 图15

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Document</title>
  6. <style>
  7. h1 {
  8. background: blue url("https://picsum.photos/id/1015/200/300");
  9. background-clip: text;
  10. -webkit-background-clip: text;
  11. color: transparent;
  12. margin-top: 20px;
  13. font-size: 120px;
  14. }
  15. @media (max-width: 600px) {
  16. h1 {
  17. font-size: 45px;
  18. }
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <h1>揭秘 CSS</h1>
  24. </body>
  25. </html>

保持宽高比

在响应式网页设计中,保持一致的宽高比,即所谓的长宽比
使用padding-top
解决方案需要一个父容器和一个绝对放置的子容器。然后,可以将宽高比计算为百分比以设置为 padding-top。例如:
1:1长宽比 = 1/1 = 1 = padding-top: 100%
4:3的长宽比 = 3/4 = 0.75 = padding-top: 75%
3:2的长宽比 = 2 / 3 = 0.66666 = padding-top: 66.67%
16:9的长宽比 = 9 / 16 = 0.5625 = padding-top: 56.25%

  1. <div class="container">
  2. <img class="media" src="..." alt="...">
  3. </div>
  1. .container {
  2. position: relative;
  3. width: 100%;
  4. padding-top: 56.25%; /* 16:9 宽高比 */
  5. }
  6. .media {
  7. position: absolute;
  8. top: 0;
  9. }

aspect-ratio CSS属性
aspect-ratio: 16 / 9 代替:padding-top: 56.25%,将 aspect-ratio 设置为指定的 width / height比例
使用padding-top

  1. .container {
  2. width: 100%;
  3. padding-top: 56.25%;
  4. }

使用aspect-ratio

  1. .container {
  2. width: 100%;
  3. aspect-ratio: 16 / 9;
  4. }

文本溢出

单行

  1. {
  2. overflow: hidden;
  3. white-space: nowrap;
  4. text-overflow: ellipsis;
  5. }

多行

只有-webkit内核才有作用

  1. {
  2. display: -webkit-box;
  3. overflow: hidden;
  4. text-overflow: ellipsis;
  5. -webkit-box-orient: vertical;
  6. -webkit-line-clamp: 2;
  7. }

居中对齐

  1. {
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. transform: translate(-50%, -50%);
  6. }

隐藏滚动条

  1. ::-webkit-scrollbar {
  2. display: none;
  3. }

video/audio隐藏控件 — chrome 58+

  1. // 全屏
  2. video::-webkit-media-controls-fullscreen-button {
  3. display: none;
  4. }

禁止用户选择页面中的文字或者图片

  1. div {
  2. user-select: none;
  3. }

清除输入框内阴影- iOS

  1. div {
  2. -webkit-appearance: none;
  3. }

禁止保存或拷贝图像

  1. img {
  2. -webkit-touch-callout: none;
  3. }

placeholder字体颜色

  1. input::-webkit-input-placeholder,
  2. textarea::-webkit-input-placeholder {
  3. color: #c7c7c7;
  4. }
  5. input:-moz-placeholder,
  6. textarea:-moz-placeholder {
  7. color: #c7c7c7;
  8. }
  9. input:-ms-input-placeholder,
  10. textarea:-ms-input-placeholder {
  11. color: #c7c7c7;
  12. }

字体禁止缩放

  1. body {
  2. text-size-adjust: 100% !important;
  3. }

旋转动画

  1. .demo { animation: rotateTurn 5s infinite linear; }
  2. @keyframes rotateTurn {
  3. 0% {
  4. transform: translate3d(0, 0, 0) rotate(0);
  5. }
  6. to {
  7. // 1turn从左往右
  8. // -1turn从右往左
  9. transform: translate3d(0, 0, 0) rotate(1turn);
  10. }
  11. }

过渡

  1. position: relative;
  2. touch-action: none;
  3. transform: translate3d(0px, -3810px, 0px);
  4. transition: all 700ms ease;

position: fixed依据父元素定位

  1. transform: scale(1);