CSS
在开发项目中,修改输入占位符样式,多行文本溢出,隐藏滚动条,修改光标颜色,水平和垂直居中等等,这些都是非常熟悉的开发场景!前端开发者几乎每天都会和它们打交道,因此在这里给大家总结了20个超级实用的CSS技巧。

1、解决图片5px间距问题

是否经常遇到图片底部多出5px间距的问题?不着急,这里有4种方法可以解决此问题。

解决方案 1:将 font-size: 0 设置为父元素

演示地址:https://codepen.io/Fcant/pen/qBKgVYL
具体实现代码如下:
HTML

  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>image 5px spacing</title>
  8. </head>
  9. <body>
  10. <div class="img-container">
  11. <img src="https://cdn-images-1.medium.com/max/1600/0*MU3iBxNwssZWt6Tj" alt="">
  12. </div>
  13. </body>
  14. </html>

CSS

  1. html,body{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .img-container{
  6. background-color: lightblue;
  7. /* Key Style */
  8. font-size: 0;
  9. }
  10. img{
  11. width: 100%;
  12. }

解决方案 2:将 display: block 设置为 img

演示地址:https://codepen.io/Fcant/pen/bGKzYKw
具体实现代码如下:
HTML

  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>image 5px spacing</title>
  8. </head>
  9. <body>
  10. <div class="img-container">
  11. <img src="https://cdn-images-1.medium.com/max/1600/0*MU3iBxNwssZWt6Tj" alt="">
  12. </div>
  13. </body>
  14. </html>

CSS

  1. html,body{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .img-container{
  6. background-color: lightblue;
  7. }
  8. img{
  9. width: 100%;
  10. /* Key Style */
  11. display: block;
  12. }

解决方案 3:将 vertical-align: bottom 设置为 img

演示地址:https://codepen.io/Fcant/pen/ZERwaRv
具体实现代码如下:
HTML

  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>image 5px spacing</title>
  8. </head>
  9. <body>
  10. <div class="img-container">
  11. <img src="https://cdn-images-1.medium.com/max/1600/0*MU3iBxNwssZWt6Tj" alt="">
  12. </div>
  13. </body>
  14. </html>

CSS

  1. html,body{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .img-container{
  6. background-color: lightblue;
  7. }
  8. img{
  9. width: 100%;
  10. /* Key Style */
  11. vertical-align: bottom;
  12. }

解决方案 4:给父元素设置line-height: 5px

演示地址:https://codepen.io/Fcant/pen/LYrqOrq
具体实现代码如下:
HTML

  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>image 5px spacing</title>
  8. </head>
  9. <body>
  10. <div class="img-container">
  11. <img src="https://cdn-images-1.medium.com/max/1600/0*MU3iBxNwssZWt6Tj" alt="">
  12. </div>
  13. </body>
  14. </html>

CSS

  1. html,body{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .img-container{
  6. background-color: lightblue;
  7. /* Key Style */
  8. line-height: 5px;
  9. }
  10. img{
  11. width: 100%;
  12. }

2、元素高度与窗口相同

演示地址:https://codepen.io/Fcant/pen/vYrbWzB
点击查看【codepen】
如何让元素和窗口一样高?示例代码如下:

  1. <div class="app">
  2. <div class="child">
  3. </div>
  4. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .child {
  6. width: 100%;
  7. /* Key Style */
  8. height: 100vh;
  9. background-image: linear-gradient(180deg, #2af598 0%, #009efd 100%);
  10. }

3、修改输入占位符样式

演示地址:https://codepen.io/Fcant/pen/WNyPXgp
点击查看【codepen】
第一个修改了,第二个没有修改。

  1. <input type="text" class="placehoder-custom" placeholder="Please enter user name to search">
  2. <input type="text" placeholder="Please enter user name to search">
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. input {
  6. width: 300px;
  7. height: 30px;
  8. border: none;
  9. outline: none;
  10. display: block;
  11. margin: 15px;
  12. border: solid 1px #dee0e9;
  13. padding: 0 15px;
  14. border-radius: 15px;
  15. }
  16. /* Key Style */
  17. .placehoder-custom::-webkit-input-placeholder {
  18. color: #babbc1;
  19. font-size: 12px;
  20. }

4、使用:not选择器

演示地址:https://codepen.io/Fcant/pen/MWXLOqr
除了最后一个元素之外的所有元素都需要一些样式,使用 not 选择器会非常容易。
如下图:最后一个元素没有底边框。
点击查看【codepen】

  1. <ul>
  2. <li>
  3. <span>cell</span>
  4. <span>content</span>
  5. </li>
  6. <li>
  7. <span>cell</span>
  8. <span>content</span>
  9. </li>
  10. <li>
  11. <span>cell</span>
  12. <span>content</span>
  13. </li>
  14. <li>
  15. <span>cell</span>
  16. <span>content</span>
  17. </li>
  18. </ul>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. html,
  6. body {
  7. background-color: #f7f8fa;
  8. height: 100%;
  9. }
  10. ul,
  11. li {
  12. list-style: none;
  13. padding: 0 15px;
  14. font-size: 14px;
  15. }
  16. ul {
  17. margin-top: 10px;
  18. background-color: #fff;
  19. }
  20. li {
  21. height: 32px;
  22. display: flex;
  23. align-items: center;
  24. justify-content: space-between;
  25. }
  26. /* Key Style */
  27. li:not(:last-child) {
  28. border-bottom: 1px solid #ebedf0;
  29. }
  30. li span:first-child {
  31. color: #323233;
  32. }
  33. li span:last-child {
  34. color: #969799;
  35. }

5、使用flex布局智能固定一个元素在底部

演示地址:https://codepen.io/Fcant/pen/JjZxOaw
点击查看【codepen】
当内容不够时,按钮应该在页面底部。当有足够的内容时,按钮应该跟随内容。当遇到类似问题时,使用flex实现智能布局!
20 个超级实用的 CSS 技巧 - 图1
代码如下:

  1. <div class="container">
  2. <div class="main">I'm fatfish, 6 years of programming experience, like front-end, writing
  3. and making friends,looking forward to becoming good friends with you.</div>
  4. <div class="footer">rule</div>
  5. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .container {
  6. height: 100vh;
  7. /* Key Style */
  8. display: flex;
  9. flex-direction: column;
  10. justify-content: space-between;
  11. }
  12. .main {
  13. /* Key Style */
  14. flex: 1;
  15. background-image: linear-gradient(
  16. 45deg,
  17. #ff9a9e 0%,
  18. #fad0c4 99%,
  19. #fad0c4 100%
  20. );
  21. display: flex;
  22. align-items: center;
  23. justify-content: center;
  24. color: #fff;
  25. }
  26. .footer {
  27. padding: 15px 0;
  28. text-align: center;
  29. color: #ff9a9e;
  30. font-size: 14px;
  31. }

6、使用"caret-color"修改光标颜色

演示地址:https://codepen.io/Fcant/pen/RwJvjew
有时需要修改光标的颜色。现在是插入符号颜色显示时间。
点击查看【codepen】

  1. <input type="text" class="caret-color" />
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. display: flex;
  7. justify-content: center;
  8. }
  9. .caret-color {
  10. width: 300px;
  11. padding: 10px;
  12. margin-top: 20px;
  13. border-radius: 10px;
  14. border: solid 1px #ffd476;
  15. box-sizing: border-box;
  16. background-color: transparent;
  17. outline: none;
  18. color: #ffd476;
  19. font-size: 14px;
  20. /* Key Style */
  21. caret-color: #ffd476;
  22. }
  23. .caret-color::-webkit-input-placeholder {
  24. color: #4f4c5f;
  25. font-size: 14px;
  26. }

7、去掉type="number"末尾的箭头

演示地址:https://codepen.io/Fcant/pen/yLEZPRP
点击查看【codepen】
默认情况下,input type = "number"的末尾会出现一个小箭头,但有时需要将其去掉。应该做什么?
如下图:第二个去掉了,第一个没有。
20 个超级实用的 CSS 技巧 - 图2

  1. <input type="number" />
  2. <input type="number" class="no-arrow" />
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. display: flex;
  7. justify-content: center;
  8. align-items: center;
  9. flex-direction: column;
  10. }
  11. input {
  12. width: 300px;
  13. padding: 10px;
  14. margin-top: 20px;
  15. border-radius: 10px;
  16. border: solid 1px #ffd476;
  17. box-sizing: border-box;
  18. background-color: transparent;
  19. outline: none;
  20. color: #ffd476;
  21. font-size: 14px;
  22. caret-color: #ffd476;
  23. display: block;
  24. }
  25. input::-webkit-input-placeholder {
  26. color: #4f4c5f;
  27. font-size: 14px;
  28. }
  29. /* Key Style */
  30. .no-arrow::-webkit-outer-spin-button,
  31. .no-arrow::-webkit-inner-spin-button {
  32. -webkit-appearance: none;
  33. }

8、outline:none去掉输入状态行

演示地址:https://codepen.io/Fcant/pen/ExRrbde
点击查看【codepen】
当输入框被选中时,默认会有一个蓝色的状态行,可以使用 outline: none 去掉。
如下图:第二个输入框去掉了,第一个没有。
20 个超级实用的 CSS 技巧 - 图3

  1. <input type="number" />
  2. <input type="number" class="no-outline" />
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. display: flex;
  7. justify-content: center;
  8. align-items: center;
  9. flex-direction: column;
  10. }
  11. input {
  12. width: 300px;
  13. padding: 10px;
  14. margin-top: 20px;
  15. border-radius: 10px;
  16. border: solid 1px #ffd476;
  17. box-sizing: border-box;
  18. background-color: transparent;
  19. color: #ffd476;
  20. font-size: 14px;
  21. caret-color: #ffd476;
  22. display: block;
  23. }
  24. /* Key Style */
  25. .no-outline {
  26. outline: none;
  27. }

9、解决iOS滚动条卡住的问题

在苹果手机上,经常会出现滚动时元素卡住的情况。这个时候只有一行CSS会支持弹性滚动。

  1. body,html{
  2. -webkit-overflow-scrolling: touch;
  3. }

10、画三角形

演示地址:https://codepen.io/Fcant/pen/VwdgrEJ
点击查看【codepen】

  1. <div class="box">
  2. <div class="box-inner">
  3. <div class="triangle bottom"></div>
  4. <div class="triangle right"></div>
  5. <div class="triangle top"></div>
  6. <div class="triangle left"></div>
  7. </div>
  8. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. }
  8. .box {
  9. padding: 15px;
  10. background-color: #f5f6f9;
  11. border-radius: 6px;
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. }
  16. .triangle {
  17. display: inline-block;
  18. margin-right: 10px;
  19. /* Base Style */
  20. border: solid 10px transparent;
  21. }
  22. /*bottom*/
  23. .triangle.bottom {
  24. border-top-color: #0097a7;
  25. }
  26. /*top*/
  27. .triangle.top {
  28. border-bottom-color: #b2ebf2;
  29. }
  30. /*left*/
  31. .triangle.left {
  32. border-right-color: #00bcd4;
  33. }
  34. /*right*/
  35. .triangle.right {
  36. border-left-color: #009688;
  37. }

11、画小箭头

演示地址:https://codepen.io/Fcant/pen/OJEdOaN
点击查看【codepen】

  1. <div class="box">
  2. <div class="box-inner">
  3. <div class="arrow bottom"></div>
  4. <div class="arrow top"></div>
  5. <div class="arrow right"></div>
  6. <div class="arrow left"></div>
  7. </div>
  8. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. }
  8. .box {
  9. padding: 15px;
  10. background-color: #ffffff;
  11. border-radius: 6px;
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. }
  16. .arrow {
  17. display: inline-block;
  18. margin-right: 10px;
  19. width: 0;
  20. height: 0;
  21. /* Base Style */
  22. border: 16px solid;
  23. border-color: transparent #cddc39 transparent transparent;
  24. position: relative;
  25. }
  26. .arrow::after {
  27. content: "";
  28. position: absolute;
  29. right: -20px;
  30. top: -16px;
  31. border: 16px solid;
  32. border-color: transparent #fff transparent transparent;
  33. }
  34. /*bottom*/
  35. .arrow.bottom {
  36. transform: rotate(270deg);
  37. }
  38. /*top*/
  39. .arrow.top {
  40. transform: rotate(90deg);
  41. }
  42. /*left*/
  43. .arrow.left {
  44. transform: rotate(180deg);
  45. }
  46. /*right*/
  47. .arrow.right {
  48. transform: rotate(0deg);
  49. }

12、图像适合窗口大小

演示地址:https://codepen.io/Fcant/pen/MWXLOzr
20 个超级实用的 CSS 技巧 - 图4
vw vs padding

  1. <div class="box">
  2. <div class="img-container">
  3. <img src="https://cdn-images-1.medium.com/max/1600/0*tuDPftoIhupd-qx-.jpg" alt="">
  4. </div>
  5. </div>
  6. <div class="box">
  7. <div class="img-container">
  8. <img src="https://cdn-images-1.medium.com/max/1600/0*tuDPftoIhupd-qx-.jpg" alt="">
  9. </div>
  10. </div>
  11. <div class="box-vw">
  12. <div class="img-container">
  13. <img src="https://cdn-images-1.medium.com/max/1600/0*tuDPftoIhupd-qx-.jpg" alt="">
  14. </div>
  15. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. }
  8. .box,
  9. .box-vw {
  10. background-color: #010102;
  11. border-radius: 10px;
  12. overflow: hidden;
  13. margin-bottom: 15px;
  14. }
  15. .box:nth-of-type(2) {
  16. width: 260px;
  17. }
  18. /* vw */
  19. .box-vw .img-container {
  20. width: 100vw;
  21. height: 66.620879vw;
  22. padding-bottom: inherit;
  23. }
  24. /* padding */
  25. .img-container {
  26. width: 100%;
  27. height: 0;
  28. /* Aspect ratio of picture*/
  29. padding-bottom: 66.620879%;
  30. }
  31. img {
  32. width: 100%;
  33. }

13、隐藏滚动条

演示地址:https://codepen.io/Fcant/pen/abKXVQQ
点击查看【codepen】
20 个超级实用的 CSS 技巧 - 图5
第一个滚动条可见,第二个隐藏。
这意味着容器可以滚动,但是滚动条是隐藏的,就好像它是透明的一样。

  1. <div class="box">
  2. <div>
  3. I'm fatfish, 6 years of programming experience, like front-end, writing
  4. and making friends, looking forward to becoming good friends with you.
  5. </div>
  6. </div>
  7. <div class="box box-hide-scrollbar">
  8. <div>
  9. I'm fatfish, 6 years of programming experience, like front-end, writing
  10. and making friends, looking forward to becoming good friends with you.
  11. </div>
  12. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. color: #324b64;
  8. }
  9. .box {
  10. width: 375px;
  11. overflow: scroll;
  12. }
  13. /* Key Style */
  14. .box-hide-scrollbar::-webkit-scrollbar {
  15. display: none; /* Chrome Safari */
  16. }
  17. .box > div {
  18. margin-bottom: 15px;
  19. padding: 10px;
  20. background-color: #f5f6f9;
  21. border-radius: 6px;
  22. font-size: 12px;
  23. width: 750px;
  24. }

14、自定义选中的文字样式

演示地址:https://codepen.io/Fcant/pen/VwdgrVo
点击查看【codepen】

  1. <div class="box">
  2. <p class="box-default">
  3. I'm fatfish, 6 years of programming experience, like front-end, writing
  4. and making friends, looking forward to becoming good friends with you.
  5. </p>
  6. <p class="box-custom">
  7. I'm fatfish, 6 years of programming experience, like front-end, writing
  8. and making friends, looking forward to becoming good friends with you.
  9. </p>
  10. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. color: #324b64;
  8. }
  9. .box > p {
  10. padding: 10px;
  11. background-color: #f5f6f9;
  12. border-radius: 6px;
  13. font-size: 12px;
  14. margin-bottom: 15px;
  15. }
  16. /* Key Style */
  17. .box-custom::selection {
  18. color: #ffffff;
  19. background-color: #ff4c9f;
  20. }

15、不允许选择文本

演示地址:https://codepen.io/Fcant/pen/QWxYOzg
点击查看【codepen】
第一个内容可以选,第二个不可以选中了。

  1. <div class="box">
  2. <p> I'm fatfish, 6 years of programming experience, like front-end, writing
  3. and making friends, looking forward to becoming good friends with you.</p>
  4. <p> I'm fatfish, 6 years of programming experience, like front-end, writing
  5. and making friends, looking forward to becoming good friends with you.</p>
  6. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. color: #324b64;
  8. }
  9. .box p {
  10. margin-bottom: 15px;
  11. padding: 10px;
  12. background-color: #f5f6f9;
  13. border-radius: 6px;
  14. font-size: 12px;
  15. }
  16. /* Key Style */
  17. .box p:last-child {
  18. user-select: none;
  19. }

16、水平和垂直居中元素

演示地址:https://codepen.io/Fcant/pen/zYaePyJ
点击查看【codepen】

  1. <div class="parent">
  2. <p class="child">I'm fatfish, 6 years of programming experience, like front-end, writing
  3. and making friends, looking forward to becoming good friends with you.</p>
  4. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. color: #324b64;
  8. }
  9. .parent {
  10. padding: 0 10px;
  11. background-color: #f5f6f9;
  12. height: 100px;
  13. border-radius: 6px;
  14. font-size: 12px;
  15. /* Key Style */
  16. display: flex;
  17. align-items: center;
  18. justify-content: center;
  19. }

17、单行文字溢出时显示省略号

演示地址:https://codepen.io/Fcant/pen/oNymoJR
点击查看【codepen】

  1. <div class="box">
  2. <p class="one-line-ellipsis">Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish,Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish</p>
  3. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. color: #324b64;
  8. }
  9. .box {
  10. padding: 10px;
  11. background-color: #f5f6f9;
  12. border-radius: 6px;
  13. font-size: 12px;
  14. }
  15. .one-line-ellipsis {
  16. /* Key Style */
  17. overflow: hidden;
  18. white-space: nowrap;
  19. text-overflow: ellipsis;
  20. max-width: 375px;
  21. }

18、多行文字溢出时显示省略号

演示地址:https://codepen.io/Fcant/pen/PoaVOVN
点击查看【codepen】

  1. <div class="box">
  2. <p class="more-line-ellipsis">Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish,Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish</p>
  3. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. color: #324b64;
  8. }
  9. .box {
  10. max-width: 375px;
  11. padding: 10px;
  12. background-color: #f5f6f9;
  13. border-radius: 6px;
  14. font-size: 13px;
  15. }
  16. .more-line-ellipsis {
  17. overflow: hidden;
  18. text-overflow: ellipsis;
  19. display: -webkit-box;
  20. /* set n lines, including 1 */
  21. -webkit-line-clamp: 2;
  22. -webkit-box-orient: vertical;
  23. }

19、清除浮动

这是一种老式的布局方式,现在大部分移动端都不用了。
如下图,外层的高度没有塌陷,这就是为什么要使用clearfix类。

  1. <div class="box">
  2. <p class="more-line-ellipsis">Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish,Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish, Hello Fatfish</p>
  3. </div>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 15px;
  7. color: #324b64;
  8. }
  9. .box {
  10. max-width: 375px;
  11. padding: 10px;
  12. background-color: #f5f6f9;
  13. border-radius: 6px;
  14. font-size: 13px;
  15. }
  16. .more-line-ellipsis {
  17. overflow: hidden;
  18. text-overflow: ellipsis;
  19. display: -webkit-box;
  20. /* set n lines, including 1 */
  21. -webkit-line-clamp: 2;
  22. -webkit-box-orient: vertical;
  23. }

20、使用filter:grayscale(1)使页面处于灰色模式

  1. body{
  2. filter: grayscale(1);
  3. }

一行代码将使页面处于灰色模式,这功能也很实用的,后期如果想要恢复到原来的正常模式,只需要在CSS里将filter: grayscale(1);这行代码注释掉即可。