利用CSS的Sass預(yù)處理器(框架)來制作居中效果

雖然使用 CSS 創(chuàng)建居中效果需要耍一些花招,特別是垂直居中效果,但我認為由此生出的詆毀,對于 CSS 則是不公平的。實際上我們有太多的方式使用 CSS 創(chuàng)建居中效果了,而且作為一名前端開發(fā)者,你真的有必要對其中的原理了解一二。
寫這篇文章的目的不是為了向各位解釋這些方法的工作原理,而是介紹將這些方法編寫為 Sass mixin 的方式,繼而將它們復(fù)用到各類項目中。
總體概述
本文將會專注于解決子元素居中于父類容器的問題,就實踐經(jīng)驗來說,這也是最常使用到的居中效果。當(dāng)你請教別人 CSS 中和居中效果相關(guān)的問題時,他們往往會反問你:你知道元素具體的寬高嗎?之所以會有這樣的反問,是因為如果知道元素的寬高,那么最好的解決方案就是使用 CSS transform 屬性。雖然該屬性在瀏覽器中的支持度稍低,但卻有著高度靈活的特性;如果因為瀏覽器兼容性令你不能使用 CSS transform 屬性,或者也不知道元素的寬高,那么實現(xiàn)居中效果的最簡單方法就是使用負向 margin。
我們今天要創(chuàng)建的 Sass mixin 就是基于上述的方法:將元素的左上角絕對定位到容器的中心位置,然后為 mixin 添加兩個可選參數(shù),分別代表元素的寬高,如果傳遞了參數(shù),那么就使用負向 margin 的方法實現(xiàn)居中;如果沒有傳遞參數(shù),就使用 CSS transform 的方法。
當(dāng)我們的 Sass mixin 創(chuàng)建成功后,基本的使用方式如下所示:
- /**
- * 為子元素設(shè)定定位上下文
- */
- .parent {
- position: relative;
- }
- /**
- * 讓子元素絕對居中于父容器
- * 沒有向 Sass mixin 傳遞參數(shù),使用 CSS transform 屬性實現(xiàn)居中效果
- */
- .child-with-unknown-dimensions {
- @include center;
- }
- /**
- * 讓子元素絕對居中于父容器
- * 向 Sass mixin 傳遞了寬度,所以就使用負向 margin 處理水平位置,
- * 使用 CSS transform 處理垂直位置
- */
- .child-with-known-width {
- @include center(400px);
- }
- /**
- * 讓子元素絕對居中于父容器
- * 向 Sass mixin 傳遞了高度,所以就使用負向 margin 處理垂直位置,
- * 使用 CSS transform 處理水平位置
- */
- .child-with-known-height {
- @include center($height: 400px);
- }
- /**
- * 讓子元素絕對居中于父容器
- * 向 Sass mixin 傳遞了高度和寬度,所以就使用負向 margin 處理水平和垂直位置
- */
- .child-with-known-dimensions {
- @include center(400px, 400px);
- }
上述 Sass 代碼經(jīng)過編譯之后,輸出結(jié)果如下:
- .parent {
- position: relative;
- }
- .child-with-unknown-dimensions {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- }
- .child-with-known-width {
- position: absolute;
- top: 50%;
- left: 50%;
- margin-left: -200px;
- width: 400px;
- transform: translateY(-50%);
- }
- .child-with-known-height {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translateX(-50%);
- margin-top: -200px;
- height: 400px;
- }
- .child-with-known-dimensions {
- position: absolute;
- top: 50%;
- left: 50%;
- margin-left: -200px;
- width: 400px;
- margin-top: -200px;
- height: 400px;
- }
還不錯,就是看起來有點啰嗦,不過鑒于是用來做 demo 的,也不必太過強求了。
創(chuàng)建 mixin
思路屢清楚了,下面開工!根據(jù)上面的代碼片段,我們已經(jīng)知道了這個 mixin 的主要特征:接收兩個可選的參數(shù),用來表示元素的寬高($width 和 $height)。
- /// Horizontal, vertical or absolute centering of element within its parent
- /// If specified, this mixin will use negative margins based on element's
- /// dimensions. Else, it will rely on CSS transforms which have a lesser
- /// browser support but are more flexible as they are dimension-agnostic.
- ///
- /// @author Hugo Giraudel
- ///
- /// @param {Length | null} $width [null] - Element width
- /// @param {Length | null} $height [null] - Element height
- ///
- @mixin center($width: null, $height: null) { .. }
- 然后,由分析知,要實現(xiàn)居中必須讓元素絕對定位:
- @mixin center($width: null, $height: null) {
- position: absolute;
- top: 50%;
- left: 50%;
- }
在這里讓我們暫停一下,深入分析后續(xù)邏輯的層次:
width | height | solution |
---|---|---|
null | null | translate |
defined | defined | margin |
defined | null | margin-left + translateY |
null | defined | margin-right + translateX |
秀代碼:
- @mixin center($width: null, $height: null) {
- position: absolute;
- top: 50%;
- left: 50%;
- @if not $width and not $height {
- // Go with `translate`
- } @else if $width and $height {
- // Go width `margin`
- } @else if not $height {
- // Go with `margin-left` and `translateY`
- } @else {
- // Go with `margin-top` and `translateX`
- }
- }
通過上面的代碼,我們已經(jīng)搭好了 mixin 的骨架,只需要再添加上具體的邏輯代碼即可:
- @mixin center($width: null, $height: null) {
- position: absolute;
- top: 50%;
- left: 50%;
- @if not $width and not $height {
- transform: translate(-50%, -50%);
- } @else if $width and $height {
- width: $width;
- height: $height;
- margin: -($width / 2) #{0 0} -($height / 2);
- } @else if not $height {
- width: $width;
- margin-left: -($width / 2);
- transform: translateY(-50%);
- } @else {
- height: $height;
- margin-top: -($height / 2);
- transform: translateX(-50%);
- }
- }
注意!上面代碼中的 #{0 0} 實際上一種容錯措施,如果直接使用 0 0 的話,Sass 解析器會嘗試進行數(shù)值運算(在這里會自動進行 0 -($height / 2) 的數(shù)學(xué)運算),進而導(dǎo)致我們得到 margin: mt 0 ml; 而不是想要得到的 margin: mt 0 0 ml;。
深入淺出
基本的功能實現(xiàn)后,我們還可以添加更多的特性,比如添加 @support 來檢查瀏覽器對 CSS transform 的支持度,進而可以根據(jù) CSS transform 的支持度輸出相應(yīng)的條件樣式。此外,我們還可以更嚴謹?shù)厝y試出入的參數(shù)是否是有效數(shù)值……
使用 Flexbox
看到 Flexbox 這個詞是不是就很興奮啊,少年!確實,使用 Flexbox 確實是最簡單的方式,它和前面方法主要的差別在于,使用 Flexbox 需要為父容器設(shè)定相關(guān)樣式,而使用前面的方法則主要是為子元素設(shè)定相關(guān)樣式(當(dāng)然,父容器需要被設(shè)定為除 static 之外的任意 position)。
使用 Flexbox 實現(xiàn)子元素的居中效果,只需三行代碼:
- @mixin center-children {
- display: flex;
- justify-content: center;
- align-items: center;
- }
由于 Flexbox 還是比較新的屬性,那么添加上相關(guān)的瀏覽器前綴的話,會讓它擁有更廣泛的兼容性。
- .parent {
- @include center-children;
- }
正如你料想的那樣,就這么簡單我們就實現(xiàn)了:
- .parent {
- display: flex;
- justify-content: center;
- align-items: center;
- }
總結(jié)
我們就想要一個簡短的 mixin 讓元素在父容器中居中,我們做到了,而且做的更好。它不僅僅簡單易用無副作用,而且提供了良好的開發(fā)接口。
相關(guān)文章
學(xué)習(xí)CSS預(yù)處理器:Sass和less進行對比
Css可以讓你做很多事情,但它畢竟是給瀏覽器認的東西,對開發(fā)者來說,Css缺乏很多特性2012-07-11- 這篇文章主要介紹了CSS預(yù)處理器Sass詳解的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-12