常用 CSS 程式碼片段集合,建議收藏

南城大前端發表於2022-05-23

分享日常開發過程中遇到的常見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; 
}

參考

專注前端開發,分享前端相關技術乾貨,公眾號:南城大前端(ID: nanchengfe)

相關文章