当前位置:   article > 正文

CSS常用技巧

CSS常用技巧

【1】制作三角形

  1. <style>
  2. .box {
  3. width: 0;
  4. height: 0;
  5. margin: 0 auto;
  6. /* 等腰直角三角: 各边框宽度一致,将上边框保留,其他边框设置为透明色 */
  7. border: 100px solid transparent;
  8. border-top: 100px solid red;
  9. }
  10. </style>
  11. <div class="box"></div>

【2】文字超过范围显示省略号

  1. <style>
  2. .box {
  3. width: 200px;
  4. height: 200px;
  5. /* 一下三句就是做省略的关键 */
  6. white-space: nowrap;
  7. overflow: hidden;
  8. text-overflow: ellipsis;
  9. }
  10. </style>
  11. <div class="box"></div>

【3】超出宽度在div内左右滑动

  1. <style>
  2. .box {
  3. height: auto;
  4. /* 设置不换行 */
  5. white-space: nowrap;
  6. /* x轴超出可滚动 */
  7. overflow-x: auto;
  8. overflow-y: hidden;
  9. border: 1px solid #ccc;
  10. }
  11. </style>
  12. <div class="box">
  13. <img src="../image/logo.png" alt="">
  14. ...
  15. </div>

【4】清除浮动

  1. <style>
  2. .box:before,
  3. .box:after {
  4. content: "";
  5. display: block;
  6. clear: both;
  7. }
  8. </style>
  9. <div class="box">
  10. <div class="left"></div>
  11. <div class="right"></div>
  12. </div>

【5】背景颜色多变

  1. <style>
  2. .box {
  3. position: relative;
  4. width: 200px;
  5. height: 200px;
  6. margin: 100px auto;
  7. background-color: #ccc;
  8. }
  9. .box::before {
  10. content: '';
  11. position: absolute;
  12. right: 0;
  13. bottom: 0;
  14. left: 0;
  15. height: 5px;
  16. background: -webkit-repeating-linear-gradient(45deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
  17. background: repeating-linear-gradient(-45deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
  18. background-size: 80px;
  19. }
  20. </style>
  21. <div class="box"></div>

【6】上下居中

  1. <style>
  2. .box {
  3. height: 200px;
  4. }
  5. .middle {
  6. margin-top: 100px;
  7. transform: translateY(-50%);
  8. }
  9. </style>
  10. <div class="box">
  11. <div class="middle"></div>
  12. </div>

【7】不显示滚动条但可滚动

  1. <style>
  2. .box {
  3. overflow: scroll;
  4. }
  5. .box::-webkit-scrollbar {
  6. display: none;
  7. width: 0 !important;
  8. }
  9. </style>
  10. <div class="box"></div>

【8】更改滚动条样式

  1. <style>
  2. /*滚动条整体样式*/
  3. .box::-webkit-scrollbar {
  4. width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
  5. height: 5px;
  6. }
  7. /*滚动条里面小方块*/
  8. .box::-webkit-scrollbar-thumb {
  9. border-radius: 5px;
  10. box-shadow: inset 0 0 5px #fafafa;
  11. -webkit-box-shadow: inset 0 0 5px #fafafa;
  12. background: rgba(0,0,0,.075);
  13. }
  14. /*滚动条里面轨道*/
  15. .box::-webkit-scrollbar-track {
  16. box-shadow: inset 0 0 5px #fafafa;
  17. -webkit-box-shadow: inset 0 0 5px #fafafa;
  18. border-radius: 0;
  19. background: #ececec;
  20. }
  21. </style>
  22. <div class="box"></div
  23. https://www.cnblogs.com/ranyonsue/p/9487599.html

【9】去掉input、select自带样式

  1. <style>
  2. input {
  3. width: 100%;
  4. outline-color: invert;
  5. outline-style: none;
  6. outline-width: 0px;
  7. border: none;
  8. border-style: none;
  9. text-shadow: none;
  10. -webkit-appearance: none;
  11. -webkit-user-select: text;
  12. outline-color: transparent;
  13. box-shadow: none;
  14. }
  15. select {
  16. border: 0;
  17. display: block;
  18. width: 100%;
  19. white-space: nowrap;
  20. appearance: none;
  21. -moz-appearance: none;
  22. -webkit-appearance: none;
  23. }
  24. </style>

【10】动画animation

  1. <style>
  2. .box {
  3. animation: move 2.5s linear 2.5s;
  4. }
  5. @keyframes move {
  6. 0% {
  7. ...
  8. }
  9. 100% {
  10. ...
  11. }
  12. }
  13. </style>
  14. <div class="box"></div>

【11】过渡效果

  1. <style>
  2. .box {
  3. width: 200px;
  4. height: 20px;
  5. margin: 0 auto;
  6. border: 1px solid red;
  7. border-radius: 10px;
  8. box-sizing: border-box;
  9. }
  10. .son {
  11. width: 50%;
  12. height: 100%;
  13. border-radius: 10px;
  14. background-color: red;
  15. /* 过渡效果 */
  16. transition: all 1s ease-in-out 0.3s;
  17. }
  18. .box:hover .son {
  19. width: 100%;
  20. background-color: green;
  21. }
  22. </style>
  23. <div class="box">
  24. <div class="son"></div>
  25. </div>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/137988
推荐阅读
相关标签
  

闽ICP备14008679号