CSS 常见样式整理

449 阅读2分钟

css改变滚动条样式


/*滚动条的样式*/
[selector]::-webkit-scrollbar {
    /*高宽分别对应横竖滚动条的尺寸*/
    width: 4px;
    height: 8px;
}

[selector]::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 4px;
    -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    background: rgba(0, 0, 0, 0.2);
}
 
[selector]::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    border-radius: 0;
    background: #f00;
}

css: 溢出文字省略号

以下方法适用于WebKit浏览器及移动端

  1. -webkit-line-clamp用来限制在一个块元素显示的文本的行数。 为了实现该效果,它需要组合其他的WebKit属性。
  2. 常见结合属性:display: -webkit-box; 必须结合的属性 ,将对象作为弹性伸缩盒子模型显示 。
  3. -webkit-box-orient 必须结合的属性 ,设置或检索伸缩盒对象的子元素的排列方式。
/*单行省略号*/
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;

/*多行省略号*/
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;

以下方法适用范围广,但文字未超出行的情况下也会出现省略号

  1. height设置为line-height的整数倍,防止超出的文字露出。
  2. p::after添加渐变背景可避免文字只显示一半。
  3. 由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:);兼容ie8需要将::after替换成:after。
p{
    position: relative; 
    line-height20pxmax-height40px;
    overflow: hidden;
}
p::after{
    content"..."position: absolute; 
    bottom0right0padding-left40px;
    background-webkit-linear-gradient(left, transparent, #fff 55%);
    background-o-linear-gradient(right, transparent, #fff 55%);
    background-moz-linear-gradient(right, transparent, #fff 55%);
    backgroundlinear-gradient(to right, transparent, #fff 55%);
}

:nth-child() 选择器

单双行不同样式

/*奇数*/
p:nth-child(odd) {
    background:#ff0000;
}
/*偶数*/
p:nth-child(even) {
    background:#0000ff;
}