那些你需要知道的CSS小代码

1、文字渐变

.text{
  background-image: linear-gradient(to right, #c6ffdd, #fbd786, #f7797d);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}

2、column count,列属性为文本元素制作漂亮的列布局

p{
  column-count: 3;
  column-gap: 5rem;          
  column-rule: 1px solid salmon;
}

3、逗号分割的无序列表

ul li:not(:last-child)::after{
    content: ','
}

4、实现表格奇偶行不同的背景

.table > tr:nth-child(even) > td {} //(偶数行)
.table > tr:nth-child(odd) > td {} //(奇数行)

//或者

.table > tr:nth-child(2n) > td {} //(偶数行)
.table > tr:nth-child(2n+1) > td {} //(奇数行)

5、实现自定义的小三角

//下三角
.arrow-down{
    width: 0px;
    height: 0px;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid #ccc;
    line-height: 0px;
}

//上三角
.arrow-up{
    width: 0px;
    height: 0px;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid #ccc;
    line-height: 0px;
}

6、实现文本俩端对齐

p{
    display: inline-block;
    text-align: justify;
    text-justify:distribute-all-lines; // 兼容ie
    text-align-last: justify;
}

7、文本自动换行

p{
    width: 100px;
    word-wrap: break-word;
}

8、文本超长使用省略号代替

//单行文本
p{
    width: 100px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}


//多行文本
p{
    display: -webkit-box; /*重点,不能用block等其他,将对象作为弹性伸缩盒子模型显示*/
    -webkit-box-orient: vertical; /*从上到下垂直排列子元素(设置伸缩盒子的子元素排列方式)*/
    -webkit-line-clamp: 3; /*行数,超出三行隐藏且多余的用省略号表示...*/
    line-clamp: 3;
    word-break: break-all;
    overflow: hidden;
    max-width: 100%;
}

10、清除浮动

.clearfix:after { content:"\20"; display:block;height:0;clear:both;visibility:hidden;}
.clearfix{display:block;}
.clear{margin:0; padding:0; width:0; height:0; line-height:0; font-size:0; clear:both;}

11、设置输入框占位符(placeholder)的颜色

input::-webkit-input-placeholder {    /* Chrome/Opera/Safari */
    color: red;
}
input::-moz-placeholder { /* Firefox 19+ */
    color: red;
}
input:-ms-input-placeholder { /* IE 10+ */
    color: red;
}
input:-moz-placeholder { /* Firefox 18- */
    color: red;
}

12、背景透明

.liter{
    filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#4CDDDDDD', endColorstr='#4CDDDDDD');
}
:root .liter {
    filter:none;     /*处理IE9浏览器中的滤镜效果*/
    background-color:rgba(221,221,221,0.3);
}

持续更新

猜你喜欢

转载自blog.csdn.net/codingLeader/article/details/124730559