[譯]這幾個CSS小技巧,你知道嗎?

前言

在網頁設計和前端開發中,CSS屬性是非常重要的一部分。掌握常用的CSS屬性不僅可以使你的網頁看起來更美觀,還能提升用戶體驗,今天小編爲大家介紹8個常見的CSS小技巧:

1.修改滾動條樣式

下圖是我們常見的滾動條,現在需要改變滾動條的寬度和顏色了,並把它畫的圓一點。

(常見的滾動條)

可以用::-webkit-scrollbar來實現:

/*設置滾動條的寬度*/
::-webkit-scrollbar{  
 width: 10px;  
}  
/*將軌道改爲藍色,並設置圓形邊框*/
::-webkit-scrollbar-track{  
 background-color: blue;  
 border-radius: 10px;  
}  
/* 將滾動條設置爲灰色並將其設置爲圓形*/
::-webkit-scrollbar-thumb{  
 background: gray;  
 border-radius: 10px  
}  
/*懸停時呈深灰色*/
::-webkit-scrollbar-thumb:hover{  
 background: darkgray;  
}

​ (改變之後的滾動條)

2.修改光標停留在頁面上的樣式

一般情況下鼠標的樣式是一個箭頭,改變鼠標光標的樣式爲其他類型:

/*類爲first的元素,設置鼠標爲不可用狀態 。 */  
.first{  
 cursor: not-allowed;  
}  
/* 類爲second的元素,將鼠標指針設置爲放大鏡效果 */  
.second{  
 cursor: zoom-in;  
}  
/* 類爲third的元素,將鼠標指針設置爲十字準星形狀*/ 
.third{  
 cursor: crosshair;  
}

​ (改變之後的光標)

3.保持組件的縱橫比大小

在構建響應式組件的時候,組件的高度與寬度的不協調經常會導致視頻和圖像會出現拉伸的情況,影響讀者的觀感,因此我們需要設置組件的縱橫比屬性:

.example{  
 /* 設置縱橫比 */  
 aspect-ratio: 1 / .25;  
 /* 設置寬度後,高度自動設置 */  
 width: 200px;  
 /*設置邊框.*/  
 border: solid black 1px;  
}

設置了寬度之後,我們將自動得到等於125像素的高度,以保持長寬比。

​ (顯示效果)

4.頁面平滑的滾動

通過代碼實現平滑地從一個頁面跳轉到另一個頁面:

<!DOCTYPE html\>

<html\>

<head\>

<style\>

/*設置頁面平滑地滾動*/

html {

scroll-behavior: smooth;

}

#section1 {

height: 600px;

background-color: pink;

}

#section2 {

height: 600px;

background-color: yellow;

}

<style\>

<head\>

<body>

<h1\>Smooth Scroll</h1\>

<div class="main" id="section1"\>

<h2>Section 1</h2>

<p>Click on the link to see the "smooth" scrolling effect.</p>

<a href="\#section2">Click Me to Smooth Scroll to Section 2 Below</a>

<p>Note: Remove the scroll-behavior property to remove smooth scrolling.</p>

</div>

<div class="main" id="section2">

<h2>Section 2</h2>

<a href="#section1">Click Me to Smooth Scroll to Section 1 Above</a>

</div>

<p><strong>Note:</strong> The scroll-behavior property is not supported in Internet Explorer.</p>

</body>

</html>

點擊這裏查看效果:

5.篩選

使用 CSS 向圖像添加濾鏡:

img{  
 filter: /*YOUR VALUE */;  
}

有許多可用的過濾器。您可以模糊、增亮和飽和濾鏡。您可以將圖像設爲灰度、更改其不透明度、反轉顏色等等。

​ 正常圖像(左)、模糊圖像(中)和高對比度圖像(右)

​ 增亮圖像(左)、灰度圖像(中)和色調旋轉圖像(右)

點擊此頁面瞭解更多關於篩選的詳細信息。

6.背景效果

使用backdrop-filter在圖片中添加背景。

<div class="image"\>  
 <div class="effect">  
 backdrop-filter: blur(5px);  
 </div>  
</div>  

<style>  
.image{  
 background-image: url(YOUR URL);  
 background-size: cover;  
 width: 400px;  
 height: 400px;  
 display: flex;  
 align-items: center;  
 justify-content: center;  
}  
.effect{  
 font-size: x-large;  
 color: white;  
 font-weight: 800;  
 background-color: rgba(255, 255, 255, .3);  
 backdrop-filter: blur(5px);  
 padding: 20px;  
}  
</style>

​ (實現的效果)

7.組件反射

在 SVG 下方創建反射:

.example{  
 /* 反射將出現在下面。其他可能的值如下:| left | right */  
 -webkit-box-reflect: below;  
}

​ (方框反射)

抵消反射:

.example{  
 /* 反射將出現在下面。其他可能的值如下:| left | right */
 -webkit-box-reflect: below 20px;  
}

​ (帶有偏移的反射)

漸變反射:

.example{  
 /* 反射將出現在下面。其他可能的值如下:| left | right */ 
 -webkit-box-reflect: below 0px linear-gradient(to bottom, rgba(0,0,0,0), rgba(0,0,0,.5));  
}

​ (漸變反射)

8. 檢查瀏覽器是否支持某個屬性

使用@Supports檢查 CSS 是否支持特定屬性。

/* 檢查瀏覽器是否支持顯示 */  
@supports (display: flex){  
 /* 如果支持,則顯示爲flex。*/  
 div{  
 display: flex  
 }  
}

以上就是關於CSS的8個小技巧,希望可以幫助到大家。

本文爲翻譯,原文地址:

https://medium.com/@anirudh.munipalli/10-powerful-css-properties-that-every-web-developer-must-know-e5d7f8f04e10


擴展鏈接:

高級SQL分析函數-如何用窗口函數進行排名計算

3D模型+BI分析,打造全新的交互式3D可視化大屏開發方案

React + Springboot + Quartz,從0實現Excel報表自動化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章