常用 CSS 代码片段集合,建议收藏

1,439 阅读4分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路

分享日常开发过程中遇到的常见CSS代码,来看看有没有你熟悉的代码,相信这么多的场景能为你节省一些些时间,建议收藏起来,说不定哪天就用上啦~

overscroll-behavior-x

内部盒子滚动到边界的时候,将触发整个页面滚动,可通过设置overscroll-behavior-x阻止此行为

div {
  height: 300px;
  width: 500px;
  overflow: auto;
  overscroll-behavior-x: contain;
}

使用 sroll-snap-type 优化滚动

在实际应用中,应用在全屏滚动或banner上有很多用武之地,不需要原始的各种尺寸位置计算

ul {
    scroll-snap-type: x mandatory;
}
 
li {
    scroll-snap-align: center;
}

横竖虚线

css自带的虚线在某些设计场景下不够用,通过linear-gradient绘制虚线。

// 横虚线
.dashed {
  height: 1px;
  width: 100px;
	background: linear-gradient(to right, #000, #000 5px, transparent 5px, transparent);
	background-size: 10px 100%;
}

// 竖虚线
.dashed {
  background: linear-gradient(to bottom, #000, #000 3.2px,transparent 3.2px, transparent);
  background-size: 100% 10px;
  width: 1px;
  height: 100px;
}

画格子

基于linear-gradient 渐变画格子,格子的角度及条纹之间的间隙可自行调整。

background-image:
  linear-gradient(
    90deg, 
    rgba(52,83,139,.5) 50%, 
    transparent 50%), 
  linear-gradient(
    rgba(52,83,139,.5) 50%, 
    transparent 50%)

改变输入框光标的颜色

caret-color: #0ff;

解决IOS滚动条卡顿

body,html{   
  -webkit-overflow-scrolling: touch;
}

隐藏滚动条

.box::-webkit-scrollbar {
  display: none;
}

webkit下修改滚动条样式

  • ::-webkit-scrollbar 滚动条整体部分
  • ::-webkit-scrollbar-button 滚动条两端的按钮
  • :-webkit-scrollbar-track 外层轨道
  • ::-webkit-scrollbar-track-piece 内层滚动区
  • ::-webkit-scrollbar-thumb 滚动的滑块
  • ::-webkit-scrollbar-corner 边角
  • ::-webkit-resizer 定义右下角拖动块的样式
::-webkit-scrollbar {
  width: 12px;
}
::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.4);
  border-radius: 10px;
}
::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: rgba(0,0,0,0.1);
  -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.6);
}
::-webkit-scrollbar-thumb:window-inactive {
  background: rgba(255,0,0,0.4);
}

开启硬件加速

开启了3D模式后的 transition 过渡在IOS中闪屏,卡顿,可设置开启硬件加速解决

.dom {
   backface-visibility: hidden;
   perspective: 1000;
}

在webkit内核的浏览器中,另一个行之有效的方法是

.dom {
   transform: translate3d(0, 0, 0);
}

清除浮动

@mixin clearfix() {
  &::after {
    content: '';
    display: table;
    clear: both;
  }
}

文本溢出显示省略号

单行文本溢出,定义ellipsis函数方便调用

@mixin ellipsis() {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.ellipsis {
  @include ellipsis();
}

多行文本溢出,定义multi-ellipsis函数方便调用,line是对应的行数

@mixin multi-ellipsis($line: 1) {
  @if $line <= 0 {
      $line: 1;
  }

  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: $line;
  -webkit-box-orient: vertical;
}

.ellipsis-1 {
  @include multi-ellipsis(1);
}

.ellipsis-2 {
  @include multi-ellipsis(2);
}

使用:not() 解决最后一个元素特殊样式

// before
.nav li {    
    border-right: 1px solid #666;  
}
.nav li:last-child {    
    border-right: none;  
}

// after
.nav li:not(:last-child) {    
    border-right: 1px solid #666;  
}

让列表项看上去像一个真正的用逗号分隔的列表

ul > li:not(:last-child)::after {
  content: ",";
}

安卓CSS不识别边框0.5px解决办法

// 按钮边框
.border {
  position: relative;
  
  &::after {
      content: '';
      position: absolute;
      width: 200%;
      height: 200%;
      left: -50%;
      top: -50%;
      border: 1px solid #000;
      border-radius: 4px;
      transform: scale(0.5);
      box-sizing: border-box;
    }
}

CSS3 nth-child()选择器

  • 选择列表中的偶数标签 :nth-child(2n)
  • 选择列表中的奇数标签 :nth-child(2n-1)
  • 选择从第6个开始的,直到最后:nth-child(n+6)
  • 选择第1个到第6个 :nth-child(-n+6)
  • 两者结合使用,可以限制选择某一个范围,选择第6个到第9个 :nth-child(n+6):nth-child(-n+9)

禁止文字选中

*:not(input,textarea) {
  -webkit-user-select:none;
  user-select:none;
  -webkit-tap-highlight-color:rgba(255,0,0,0);
}

显示链接名称的同时并显示当前URL

<a href="http://www.baidu.com">baidu</a>
<style>
a::after {
  content: " (" attr(href) ")";
}
</style>

当a标签没有文本值,但 href 属性有链接的时候显示链接

a[href^="http"]:empty::before {
  content: attr(href);
}

利用border实现三角形

要实现不同方向的三角形改变其border的代码,三角形底部对应两侧的颜色为透明。

.arrow-up {
  width: 0px;
  height: 0px;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #2f2f2f;
  font-size: 0px;
  line-height: 0px;
}

图片灰度滤镜

img {
    filter: gray; 
    -webkit-filter: grayscale(1);
}

自定义选中文本颜色

::selection {
     background: #00ff00;
}
 ::-moz-selection {
     background: #00ff00;
}
 ::-webkit-selection {
     background: #00ff00;
}

禁用鼠标事件

.disabled { 
  pointer-events: none; 
}

参考

专注前端开发,分享前端相关技术干货,公众号:南城大前端(ID: nanchengfe)