经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
CSS小技巧
来源:cnblogs  作者:人生之外的路途  时间:2020/12/8 8:47:18  对本文有异议

lex布局一边固定,一遍自适应

flex布局,一边定宽的宽度会变化的问题
解决:flex:1 这边设置width为0

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>flex超出省略问题</title>
  6. <style>
  7. .flex{
  8. display: flex;
  9. }
  10. .left{
  11. flex:1 1 auto;
  12. background: red;
  13. }
  14. .left p{
  15. width: 100%;
  16. text-overflow:ellipsis;
  17. overflow:hidden;
  18. white-space:nowrap;
  19. }
  20. .right{
  21. background:green;
  22. width: 200px;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="flex">
  28. <div class="left">
  29. <p>我在左边,自适应布局我在左边,自适应布局我在左边,自适应布局我在左边,自适应布局</p></div>
  30. <div class="right">我在右边,定宽</div>
  31. </div>
  32. <div class="flex">
  33. <div class="left" style="width:0">
  34. <p>我在左边,自适应布局我在左边,自适应布局我在左边,自适应布局我在左边,自适应布局</p>
  35. </div>
  36. <div class="right">我在右边,定宽</div>
  37. </div>
  38. </body>
  39. </html>

左边固定,右边自适应,带响应式

  1. .dialog-box {
  2. display: flex;
  3. flex-wrap: wrap;
  4. .people-info-show {
  5. max-width: 360px;
  6. margin: 0 auto 20px;
  7. }
  8. .right-timeline {
  9. flex: 1;
  10. padding-left: 40px;
  11. }
  12. @media screen and (max-width: 375px) {
  13. .right-timeline {
  14. padding-left: 0px;
  15. }
  16. }
  17. }

边框

  1. //虚线
  2. border:1px dashed #FF6366;
  3. //实线
  4. border:1px solid #FF6366;

表格内容等分

  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>设置表格单元格等宽</title>
  6. <style type="text/css">
  7. /*
  8. 使字体在所有设备上都达到最佳的显示
  9. */
  10. html {
  11. -moz-osx-font-smoothing: grayscale;
  12. -webkit-font-smoothing: antialiased;
  13. text-rendering: optimizeLegibility;
  14. }
  15. /*
  16. 表格单元格等宽
  17. */
  18. .calendar { table-layout: fixed; }
  19. </style>
  20. </head>
  21. <body>
  22. <table width="100%" border="1" class="calendar">
  23. <tr>
  24. <td>我在测试我在测试我在测试我在测试我在测试我在测试我在测试我在测试我在测试</td>
  25. <td>122222222222222</td>
  26. <td>3</td>
  27. <td>4</td>
  28. <td>5</td>
  29. </tr>
  30. <tr>
  31. <td>1</td>
  32. <td>2</td>
  33. <td>3</td>
  34. <td>4</td>
  35. <td>5</td>
  36. </tr>
  37. </table>
  38. </body>
  39. </html>

CSS取消浏览器默认输入框样式

  1. input,button,select,textarea{
  2. outline:none
  3. }

修改placeholder样式

  1. &::-webkit-input-placeholder {
  2. color: #C2CFDD;
  3. }

css计算

  1. width: calc(100% - 138px);

css选择器not

  1. :not(p){
  2. background-color: #ff0000;
  3. }

CSS 三角形 正方形(不依赖伪元素)

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Document</title>
  9. </head>
  10. <style>
  11. .test {
  12. width: 0;
  13. height: 0;
  14. border-width: 100px;
  15. border-style: solid;
  16. border-color: #FFCCCC #0099CC #996699 #339933;
  17. }
  18. </style>
  19. <body>
  20. <div class="test">
  21. </div>·
  22. </body>
  23. </html>

CSS生成加号(+)

  1. //html
  2. <div></div>
  3. //css
  4. html,
  5. body {
  6. width: 100%;
  7. height: 100%;
  8. display: flex;
  9. }
  10. div {
  11. width: 200px;
  12. height: 200px;
  13. background-color: #fc3;
  14. box-sizing: border-box;
  15. margin: auto;
  16. }
  17. div {
  18. outline: 20px solid #000;
  19. outline-offset: 10px;
  20. animation: move 8s infinite linear;
  21. }
  22. @keyframes move {
  23. 50% {
  24. outline-offset: -118px;
  25. }
  26. 100% {
  27. outline-offset: -118px;
  28. }
  29. }

特殊的样式名

  1. :focus{
  2. //聚集焦点的时候
  3. }
  4. :blur{
  5. //失去焦点
  6. }
  7. :hover{
  8. //经过的时候
  9. }

超出部分不换行,显示省略号?

  1. overflow: hidden;
  2. text-overflow:ellipsis;
  3. white-space: nowrap;
  4. <div v-if="item.is_display">
  5. <p class="topic-content" v-html="item.content1"></p>
  6. </div>
  7. <div class="topic-container" v-else>
  8. <div class="topic-img" v-show="Boolean(item.image)"
  9. :style="{backgroundImage:'url('+item.image+')'}"
  10. alt=""></div>
  11. <p class="topic-content" :class="{'img-active':Boolean(item.image)}" v-html="item.content"></p>
  12. </div>
  13. .topic-container {
  14. .topic-img {
  15. display: inline-block;
  16. overflow: hidden;
  17. width: 140px;
  18. height: 80px;
  19. background-size: cover;
  20. background-repeat: no-repeat;
  21. background-position: center;
  22. margin-right: 20px;
  23. }
  24. .topic-content {
  25. display: inline-block;
  26. vertical-align: top;
  27. &.img-active {
  28. width: 80%;
  29. }
  30. }
  31. }

英文数字换行

  1. p{
  2. white-space: normal;
  3. word-wrap:break-word;
  4. word-break:break-all;
  5. }

清除浮动

  1. 使用 overflow: hidden; 清除浮动时,不能定高
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title></title>
  6. <style type="text/css">
  7. #div1{
  8. width: 200px;
  9. /*height: 50px;*/
  10. overflow: hidden;
  11. padding: 10px 20px 20px 10px;
  12. background: red;
  13. margin: 0 auto;
  14. }
  15. #div2{
  16. width: 100px;
  17. height: 100px;
  18. background: green;
  19. float: left;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="div1" >
  25. <div id="div2" ></div>
  26. </div>
  27. </body>
  28. </html>

总结:当父元素的高度是靠子元素撑开的时候,子元素浮动时,
则在父元素使用overflow: hidden可以清除浮动,使得父元素的高度依旧是靠子元素撑开。
当父元素自身设置了height属性值,
则在父元素使用overflow: hidden可以使子元素超出父元素的那部分隐藏。

使用伪类插入li里面的横线

  1. //给父元素相对定位
  2. li:after{
  3.       content: '';                 /*CSS伪类用法*/
  4.       position: absolute;         /*定位背景横线的位置*/
  5.       top: 97%;
  6. left: 20%;
  7.       background: #fff;       /*宽和高做出来的背景横线*/
  8.       width: 60%;
  9.       height: .1rem;
  10. }

让图片填满整个盒子

  1. background-size: 100% 100%;   

不使用Flex两端分散对齐

  1. text-align:justify;
  2. text-align-last:justify;

select框内容左右对齐

  1. text-align:left;
  2. text-align-last:left;

顶部固定,超出部分不换行,显示横向

  1. .firstCate{
  2.       width: 100%;
  3.       white-space: nowrap;
  4.       overflow-x: scroll;
  5.       overflow-y:hidden;
  6.       position: fixed;
  7.       top: 0;
  8.       left: 0;
  9.       li{
  10.         display: inline-block;
  11. //这里不能加浮动
  12.       }
  13.   }

经典两列布局

  1. <div class="all-product">
  2. <ul>
  3. <li v-for="(item, key) in goodsList" :key="key" @click.native="goinfo(item.id)">
  4. <div class="img">
  5. <img slot="img" :src="item.goodsimage">
  6. </div>
  7. <p slot="title">{{item.goods_title}}</p>
  8. <p slot="title">产品编号 : {{item.serial}}</p>
  9. </li>
  10. </ul>
  11. </div>
  12. .all-product {
  13. ul {
  14. padding: 0.2rem;
  15. li {
  16. height: 2.7rem;
  17. width: 49%;
  18. float: left;
  19. margin-bottom: 0.2rem;
  20. .img {
  21. height: 2rem;
  22. width: 100%;
  23. overflow: hidden;
  24. display: flex;
  25. align-items: center;
  26. img {
  27. width: 100%;
  28. }
  29. }
  30. }
  31. li:nth-child(2n + 1) {
  32. margin-right: 2%;
  33. }
  34. }
  35. }

Position:fixed 固定定位

  1. //水平和垂直方向都居中   //定位的时候用
  2. transform: translate(-50%,-50%)

横向滚动

  1. <div class="pic">
  2. <ul id="category-head">
  3. <li><img src="../../assets/partBuild1.png" /></li>
  4. <li><img src="../../assets/partBuild2.png" /></li>
  5. <li><img src="../../assets/partBuild3.png"/></li>
  6. <li><img src="../../assets/partBuild1.png"/></li>
  7. <li><img src="../../assets/partBuild2.png"/></li>
  8. </ul>
  9. </div>
  10. #category-head {
  11. width: 100%;
  12. display: inline;
  13. white-space: nowrap; /*规定段落中的文本不进行换行*/
  14. overflow-x: scroll; /*水平方向,超出部分添加滚动机制*/
  15. float: left; /*一定要设置左侧浮动*/
  16. overflow-y: hidden; /*竖直方向,超出部分隐藏*/
  17. }
  18. #category-head li {
  19. margin-top: 10px;
  20. display: inline-block; /*既可以水平排列,又可以设置宽高和边距*/
  21. }

如果不想出现滚动条,外面可以再套一层,,设置高度,然后overflow:hidden

超出两行就显示省略号

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

打包-webkit-box-orient: vertical;丢失问题

  1. /*! autoprefixer: off */
  2.  -webkit-box-orient: vertical;
  3.  /* autoprefixer: on */

参考地址

高度和宽度相等(flex布局对其有影响)

  1. width:100%
  2. height:0
  3. padding-top:100% / padding-bottom:100%
  4. //实例:
  5. .image-header {
  6. position: relative;
  7. width: 100%;
  8. height: 0;
  9. padding-top: 100%;
  10. img {
  11. position: absolute;
  12. top: 0;
  13. left: 0;
  14. width: 100%;
  15. height: 100%;
  16. }
  17. }

不使用flex布局的两端对齐

  1. //html
  2. <div class="box">
  3. <span>1</span>
  4. <span>2</span>
  5. <span>3</span>
  6. <span>4</span>
  7. <span>5</span>
  8. </div>
  9. //css
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. html,body {
  15. width: 100%;
  16. height: 100%;
  17. }
  18. .box {
  19. width: 100%;
  20. height: 100%;
  21. /* 设置元素两端对齐 */
  22. text-align: justify;
  23. }
  24. /* 这里的伪元素一定要加上,不然span元素不能两端对齐 */
  25. .box:after {
  26. content: "";
  27. display: inline-block;
  28. overflow: hidden;
  29. width: 100%;
  30. }
  31. .box span {
  32. width: 50px;
  33. height: 50px;
  34. /* 设置盒子为行内块 */
  35. display: inline-block;
  36. background-color: skyblue;
  37. /* 设置盒子内元素水平居中 */
  38. text-align: center;
  39. /* 设置盒子内容垂直居中 */
  40. line-height: 50px;
  41. }

去除滚动条

  1. //谷歌适用
  2. ::-webkit-scrollbar{
  3. display:none;
  4. }
  5. //添加属性
  6. /*隐藏滚动条,当IE下溢出,仍然可以滚动*/
  7. -ms-overflow-style:none;
  8. /*火狐下隐藏滚动条*/
  9. overflow:-moz-scrollbars-none;

1px border线

  1. border-bottom-1px($color)
  2.     position: relative
  3.     &::after
  4.         display: block
  5.         position: absolute
  6.         left: 0
  7.         bottom: 0
  8.         width: 100%
  9.         border-top: 1px solid $color    
  10.         content: ' '
  11. border-top-1px($color)
  12.     position: relative
  13.     &::before
  14.         display: block
  15.         position: absolute
  16.         left: 0
  17.         top: 0
  18.         width: 100%
  19.         border-top: 1px solid $color    
  20.         content: ' '
  21. bg-image($url)
  22.     background-image: url($url + "@2x.png")
  23.     @media ( -webkit-min-device-pixel-ratio: 3),(min-device-pixel-ratio: 3)
  24.         background-image: url($url + "@3x.png")
  25. border-none()
  26.     &:after
  27.         display: none

原文链接:http://www.cnblogs.com/wangzhaoyv/p/14050754.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号