关于前端:常用-CSS-代码片段集合建议收藏

分享日常开发过程中遇到的常见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()选择器

    1、抉择列表中的偶数标签 :nth-child(2n)
    2、抉择列表中的奇数标签 :nth-child(2n-1)
    3、抉择从第6个开始的,直到最初:nth-child(n+6)
    4、抉择第1个到第6个 :nth-child(-n+6)
    5、两者联合应用,能够限度抉择某一个范畴,抉择第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; 
}

参考

  • 应用 sroll-snap-type 优化滚动
  • overscroll-behavior-x

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理