1. 程式人生 > 其它 >前端開發需要知道的 10 個CSS技巧

前端開發需要知道的 10 個CSS技巧

1.使用相對單位

通常我們在專案開發中,使用px作為尺寸的單位,而不是使用相對單位,如:rem、em等。在萬物互聯的時代,最好的方式是相對單位rem、vh、vw等現代 CSS 佈局(如 flexbox 和 grid)方式,最大限度的支援各種終端裝置。

絕對單位

px:是一個絕對單位,主要是因為它是固定的,不會根據任何其他元素的測量而改變。

相對單位

vw(viewpoint width):相對於視口的寬度

vh(viewpoint height):相對於視口的高度

rem(font size of the root element):相對於根 ( ) 元素 (預設字型大小通常為 16px )

em(font size of the element):相對於父元素

%:相對於父元素

/* 不提倡 */
.wrap {
    font-size: 14px;
    margin: 10px;
    line-height: 24px;
}
/* 建議 */
.wrap {
    font-size: 1.2rem;
    margin: 0.5rem;
    line-height: 1.6em;
}

2. 程式碼複用

很多開發人員在談到CSS時都覺得程式碼重複性很高,在專案開發中這不是一個好的做法。好在現在有CSS前處理器(sass/scss、less、stylus、Turbine),能夠讓我們可以更好的規劃CSS程式碼,提高其複用性。

當然需要提高程式碼複用,還是需要一定的CSS的基礎,來設計好程式碼結構,如下:

/* 不提倡 */
.container {
    background-color: #efefef;
    border-radius: 0.5rem;
}

.sidebar {
    background-color: #efefef;
    border-radius: 0.5rem;
}

/* 建議 */
.container,
.sidebar {
    background-color: #efefef;
    border-radius: 0.5rem;
}

3.CSS重置

每個瀏覽器都有自己的預設樣式,因此,當網頁不包含CSS時,瀏覽器會為文字新增一些基本的預設樣式、填充、邊距等。

可以通過使用通用選擇器*重置padding、margin、box-sizing和font-family來實現這一點。

像這樣:

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}
ul,
li {
    list-style: none;
}
不過這些問題現在基本都被框架解決了,對於初學者建議可以模仿但不建議一開始就上框架。

4.不使用顏色名稱

不要使用red、blue等顏色名稱,相反,建議使用顏色的十六進位制值。

為什麼呢?因為當使用像red這樣的顏色名稱時,在不同的瀏覽器或者裝置中顯示會有所不同。

/* 不提倡 */
.container {
    background-color: red;
}

/* 建議 */
.container {
    background-color: #ff0000;
}

5.使用簡寫屬性

在CSS中,多用簡寫屬性,少用單獨屬性,具體哪些是簡寫屬性,哪些是單獨屬性,下面列舉一下常見的一些屬性,是以通常專案為原則。

簡寫屬性

background、font、margin、padding、border、transition、transform、list-style、border-radius

單獨屬性

rotate、scale、background-color、background-image、background-position、padding-left、padding-right、padding-top、padding-bottom、margin-left、margin-top、margin-right、margin-bottom、border-top、border-right、border-bottom、border-left、border-width、border-color、border-style、

/* 不提倡 */
.container {
    background-image: url(bg.png);
    background-repeat: no-repeat;
    background-position: center;
}

/* 建議 */
.container {
    background: url(bg.png) no-repeat center;
}

6.文字擷取

在專案開發中,有些列表只需要顯示一行文字,有些列表需要顯示固定函式的文字,過去通過字元擷取的方式來實現,但存在擷取不統一(文字內容不同英文、中文、標點符號等),再加上現在各種終端的適配,不足就被放大了。

現在最佳的方式是通過CSS來實現,在文字最後增加省略號(…)。

單行擷取

元素必須是block或inline-block,如果溢位被隱藏,則文字溢位不起作用,並且元素必須具有定義的寬度或最大寬度集。

p {
    display: inline-block;
    max-width: 300px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

多行擷取

p {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3; /* 需要顯示的行數 */
    overflow: hidden;
}

7.垂直居中

垂直居中是一個很常見的需求,有很多實現方式,在伸縮容器內的任何東西垂直居中:

.flex-vertically-center {
    display: flex;
    align-items: center;
}

inline、inline-block、table-cell塊垂直對齊:

img {
    /* 只對block有效 */
    display: inline-block;
    vertical-align: middle;
}

相對容器中垂直居中的絕對元素,下面程式碼是.sub-container在.container垂直居中:

.container {
    position: relative;
}
.sub-container {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

8.水平居中

與垂直對齊類似,不過水平居中更容易一點。

塊居中

.block-element {
    display: block;
    margin: 0 auto;
}

內聯或內聯塊文字居中

.container {
    text-align: center;
}

在相對容器內水平居中絕對元素:

.container {
    position: relative;
}
.sub-container {
    position: absolute;
    top: 50%;
    transform: translateX(-50%);
}

flex 容器內的任何內容水平居中:

.flex-vertically-center {
    display: flex;
    justify-content: center;
}

9.設定下一個或上一個兄弟元素樣式

對元素前面和後面的元素進行樣式設定,在專案開發中很有用。例如10個按鈕,當前按鈕下一個及下一個的兄弟元素設定不同的顏色。

html程式碼如下:

<div>
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
    <button class="current">current</button>
    <button>+ button</button>
    <button>~ button</button>
    <button>~ button</button>
    <button>~ button</button>
    <button>~ button</button>
</div>

css程式碼:

.current ~ button {
    background-color: #000;
    color: #ffffff;
}
.current {
    background-color: #ff0000;
}
.current + button {
    background-color: #333;
}

效果如下:

接下來設定當前按鈕前面樣式,css程式碼如下:

button {
    padding: 10px 15px;
    border: 1px solid #444444;
    font-size: 12px;
    background-color: #ff0000;
    color: #000;
}
.current {
    background-color: #000;
    color: #fff;
}
.current ~ button {
    background: initial;
}
.container {
    width: 1000px;
    margin: 50px auto;
    text-align: center;
}

https://www.98891.com/article-9-1.html

10.寬高比

如果想讓盒子容器有一定的寬高比,如視訊播放器尺寸,可以用幾種方法來實現,其中有一種方法最直觀。可以使用calc函式設定頂部填充(height * width) / 100%。

如下,建立一個720px寬的16 x 9矩形:

html程式碼:

<div class="container">
    <div class="box"></div>
</div>

css程式碼:

.container {
    width: 720px;
}
.box {
    padding-top: calc((9 / 16) * 100%);
    background: #efefef;
}

還可以使用after偽元素來建立比例大小。

.box::after {
    content: "";
    display: block;
    padding-top: calc((9 / 16) * 100%);
    background: #eee;
}

上面的方案會導致裡面所有的元素都必須向上移動或需要使用絕對定位。不過好訊息是,CSS增加了aspect-ratio屬性。

aspect-ratio為box容器規定了一個期待的縱橫比,這個縱橫比可以用來計算自動尺寸以及為其他佈局函式服務。