重溫CSS:Border屬性

2018-06-16 18:15 更新

邊界是眾所周知的,有什么新的東西嗎?好吧,我敢打賭,在這篇文章中,有很多你不看永遠(yuǎn)不知道的東西!

不僅可以用CSS3來創(chuàng)建圓角,使用原有CSS一樣可以顯示自定義圖形。這是正確的(有待考究);在過去,沒發(fā)現(xiàn)這種技術(shù)之前,我們可能使用背景圖像定位來顯示一個園或箭頭。幸運(yùn)的是,我們能放下PS圖象處理軟件了。

基礎(chǔ)

你可能很熟悉邊的最基本用法。

border: 1px solid black;

上面的代碼將給元素應(yīng)用1px的邊。即簡潔又簡單;但我們也可以稍作修改。

border-width: thick;
border-style: solid;
border-color: black;

除了指定具體的邊框?qū)挾戎担部梢允褂眠@三個關(guān)鍵詞:thin,mediumthick

雖然乍看起來單個屬性的方式?jīng)]必要,但有極少數(shù)的情況下,當(dāng)它是有利的,例如當(dāng)你需要在特定的事件發(fā)生時更新邊的部分屬性。

也許你需要在用戶將鼠標(biāo)懸停在一個特定的元素上時改變這個元素的邊框顏色。使用復(fù)合屬性需要重復(fù)像素值和邊的樣式。

box {
    border: 1px solid red;   
}
 
.box:hover {
    border: 1px solid green;
}

一個更優(yōu)雅的和簡潔(DRY,don’t repeat yourself)的做法是只更新邊的顏色屬性。

.box {
    border: 1px solid red;   
}
 
.box:hover {
    border-color: green;
}

此外,一會你會發(fā)現(xiàn),這種單個屬性的方式有助于通過CSS創(chuàng)建自定義的形狀。

圓角

border-radius CSS3中的代表——第一個在社區(qū)中得到廣泛使用新屬性。這意味著,除去Internet Explorer 8及更低版本,所有的瀏覽器可以顯示圓角。

為了使樣式能正確應(yīng)用,需要為Webkit和Mozilla內(nèi)核添加前綴。

-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;

然而,今天我們不關(guān)心前綴,只簡單堅(jiān)持官方形式:border-radius。

如你所料,我們可以為每個角指定不同的值。

border-top-left-radius: 20px;
border-top-right-radius: 0;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 0;

在上面的代碼中,設(shè)置border-top-right-radiusborder-bottom-left-radius為零是多余的,除非該元素有繼承的值。

就像marginpadding一樣,如果需要的話,這些設(shè)置可以合并為一個單一的屬性。

/* 左上角, 右上角, 右下角, 左下角 */
border-radius: 20px 0 30px 0;

舉個例子(網(wǎng)頁設(shè)計(jì)師經(jīng)常這樣做),可以用CSS的border-radius屬性模擬檸檬的形狀,如下:

.lemon {
   width: 200px; height: 200px;
 
   background: #F5F240;
   border: 1px solid #F0D900;     
   border-radius: 10px 150px 30px 150px;
}

擴(kuò)展知識

許多設(shè)計(jì)師一直用的是目前為止在本章列出的知識,然而,有一些方法我們可以進(jìn)一步擴(kuò)展!

多個邊

當(dāng)給一個元素應(yīng)用多個邊的時候,有各種各樣的技術(shù)可以參考。

邊的樣式

solid,dashed和dotted是最常用的border-style屬性值,還有其他幾個我們可以使用的值,包括grooveridge。

border: 20px groove #e3e3e3;

或者寫成單個屬性形式:

border-color: #e3e3e3;
border-width: 20px;
border-style: groove;

雖然這看起來不錯,但ridgegroove效果并不是真正的多個邊。

輪廓

創(chuàng)建兩條邊的最流行的方式是利用outline屬性。

.box {
   border: 5px solid #292929;
   outline: 5px solid #e3e3e3;
}

這個方法表現(xiàn)的非常棒,然而,最多兩個邊界。您應(yīng)該需要創(chuàng)建一個層,實(shí)現(xiàn)漸變梯度效果,需要一種不同的方法。

偽元素

當(dāng)輪廓技術(shù)無法滿足要求時,另一種方法是利用::before:after偽元素,并利用生成內(nèi)容產(chǎn)生額外的邊。

.box {
  width: 200px; height: 200px;
  background: #e3e3e3;
  position: relative;
 
  border: 10px solid green; 
}
 
/* 創(chuàng)建和容器寬度相同的兩個容器 */
.box:after, .box:before {
  content: '';
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
 
.box:after {
  border: 5px solid red;
  outline: 5px solid yellow;
}
 
.box:before {
  border: 10px solid blue;
}

這也許不是最優(yōu)雅的方法,但它確實(shí)起作用了。需要注意的地方是很容易混淆邊界顏色的順序。確保正確的序列。

對象陰影

創(chuàng)建無限數(shù)量的邊界更酷的方法是利用CSS3的box-shadow屬性。

.box {
    border: 5px solid red;
     box-shadow:
       0 0 0 5px green,
       0 0 0 10px yellow,
       0 0 0 15px orange;
}

在這種情況下,我們靈活使用box-shadow屬性,這種方法,并不是css規(guī)范的本意。

通過設(shè)置x,y,和模糊屬性為0,我們可以使用多個值在需要的位置創(chuàng)建實(shí)線邊界。因?yàn)?code>box-shadow屬性可以疊加,通過逗號分割,可以使用無限的值。

這種技術(shù)能很好的運(yùn)行。在不能識別box-shadow屬性的老式瀏覽器中,這只會呈現(xiàn)單一紅色5px邊界。

謹(jǐn)記:在所有瀏覽器里實(shí)現(xiàn)相同的設(shè)計(jì)是不必要的。為大部分現(xiàn)代瀏覽器書寫你的CSS,然后為老式瀏覽器提供可行的回退版本。

自定義角度

除了給border-radius傳遞一個值外,我們也可以提供兩個——由/分隔——為水平和垂直半徑指定不同的值。

例如……

border-radius: 50px / 100px; /* 水平半徑, 垂直半徑 */

……相當(dāng)于:

border-top-left-radius: 50px 100px;
border-top-right-radius: 50px 100px;
border-bottom-right-radius: 50px 100px;
border-bottom-left-radius: 50px 100px;

這種技術(shù)是特別有用,當(dāng)你需要模擬一個平緩的,冗長的曲線,而不是一個通用的圓角。例如,下面的代碼允許我們稍微變形一個正方形形狀,模擬出更多卷紙一樣的效果。

.box {
    width: 200px; height: 200px;
    background: #666;
 
    border-top-left-radius: 15em 1em;
    border-bottom-right-radius: 15em 1em;
 
}

CSS形狀

也許最干脆的是直接使用邊界,給寬和高為零的元素巧妙的應(yīng)用邊界。令人困惑,是嗎?讓我們看看一個演示。

在接下來的幾個例子,假設(shè)以下標(biāo)記……

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

……和基本樣式如下:

.box {
   width: 200px;
   height: 200px;
   background: black;
}

最常用的例子是如何使用CSS形狀創(chuàng)建一個箭頭。

關(guān)鍵是了解如何用CSS生成箭頭,通過為每個邊設(shè)置不同的顏色,并且將容器的寬和高都減為零。

假設(shè)一個有arrow類的div作為容器:

.arrow {
  width: 0; height: 0;
 
  border-top: 100px solid red;
  border-right: 100px solid green;
  border-bottom: 100px solid blue;
  border-left: 100px solid yellow;  
}

在本章的開始,更清潔的語法是不使用復(fù)合語法:

.arrow {
  width: 0; height: 0;
 
  border: 100px solid; 
  border-top-color: red;
  border-right-color: green;
  border-bottom-color: blue;
  border-left-color: yellow;
}

我們甚至可以進(jìn)一步精簡,通過合并顏色值。

.arrow {
  width: 0; height: 0;
 
  border: 100px solid;
  border-color: red green blue yellow;
}

很有趣,不是嗎?不過,當(dāng)我們后退一步時更有趣?,F(xiàn)在,如果我們將除了藍(lán)邊之外的所有的border-color設(shè)置為透明的將會怎樣?

.arrow {
  width: 0; height: 0;
 
  border: 100px solid;
  border-bottom-color: blue;
}

太棒了!但用div創(chuàng)建一個箭頭似乎不太符合語義化。然而,通過afterbefore等相關(guān)偽元素可以用來創(chuàng)建箭頭。

創(chuàng)建一個氣泡

創(chuàng)建一個100%CSS的氣泡,我們從下面的標(biāo)記考試。

<div class="speech-bubble">Hi there!</div>

接下來,應(yīng)用一些基本樣式。

.speech-bubble {
    position: relative;
    background-color: #292929;
 
    width: 200px;
    height: 150px;
    line-height: 150px; /* 垂直居中 */
 
    color: white;
    text-align: center;
}

箭頭將通過after偽元素實(shí)現(xiàn)。

.speech-bubble:after {
    content: '';   
}

:before:after偽元素可以用來在元素內(nèi)容之前或之后插入生成內(nèi)容。 接下來,只是簡單復(fù)制箭頭,并定位到適當(dāng)?shù)奈恢?。我們開始通過絕對定位的內(nèi)容,重置寬度和高度,并應(yīng)用邊界顏色。

.speech-bubble:after {
  content: '';
  position: absolute;
 
  width: 0;
  height: 0;
 
  border: 10px solid;
  border-color: red green blue yellow;
}

因?yàn)槲覀冎牢覀兿胍蛳碌募^,上面的圖片表明,除了紅色(或上)邊境其他的都應(yīng)該被省略,或者設(shè)置為透明。

.speech-bubble:after {
  content: '';
  position: absolute;
 
  width: 0;
  height: 0;
 
  border: 10px solid;
  border-top-color: red;
}

當(dāng)創(chuàng)建CSS形狀是,因?yàn)槲覀儾荒苁褂脀idth屬性來指定箭頭的寬度,而是應(yīng)該使用border-width屬性。在這種情況下,箭頭應(yīng)該更大點(diǎn);所以border-width可以增加到15px。我們將箭頭定位到容器的底部居中,通過利用topleft屬性。

.speech-bubble:after {
  content: '';
  position: absolute;
 
  width: 0;
  height: 0;
 
  border: 15px solid;
  border-top-color: red;
 
  top: 100%;
  left: 50%;
}

到這里就差不多了;最后一個步驟是更新箭頭的顏色和容器的背景顏色相同。定位也需要修改,根據(jù)邊界的寬度(15 px)。當(dāng)我們在這里,我們還將應(yīng)用一個微妙border-radius屬性來使容器更像氣泡。

.speech-bubble {
   /* … 其他樣式 */
   border-radius: 10px;
}
 
.speech-bubble:after {
  content: '';
  position: absolute;
 
  width: 0;
  height: 0;
 
  border: 15px solid;
  border-top-color: #292929;
 
  top: 100%;
  left: 50%;
  margin-left: -15px; /* 調(diào)整邊框?qū)挾?*/
}

不錯,不是嗎?將這代碼抽象為幾個可重用的類,好應(yīng)用到你將來的項(xiàng)目。

/*
   對話氣泡
   用法:使用.speech-bubble和.speech-bubble-DIRECTION類
   <div class="speech-bubble speech-bubble-top">Hi there</div>
*/
 
.speech-bubble {
  position: relative;
  background-color: #292929;
 
  width: 200px;
  height: 150px;
  line-height: 150px; /* 垂直居中 */
 
  color: white;
  text-align: center;
  border-radius: 10px;
 
  font-family: sans-serif;
}
 
.speech-bubble:after {
  content: '';
  position: absolute;
 
  width: 0;
  height: 0;
 
  border: 15px solid;
}
 
 
/* 箭頭的位置 */
 
.speech-bubble-top:after {
  border-bottom-color: #292929;
 
  left: 50%;
  bottom: 100%;
  margin-left: -15px;  
}
.speech-bubble-right:after {
  border-left-color: #292929;
 
  left: 100%;
  top: 50%;
  margin-top: -15px;   
}
 
.speech-bubble-bottom:after {
  border-top-color: #292929;
 
  top: 100%;
  left: 50%;
  margin-left: -15px;  
}
 
.speech-bubble-left:after {
  border-right-color: #292929;
 
  top: 50%;
  right: 100%;
  margin-top: -15px;   
}

補(bǔ)充:更好的垂直居中

使用line-height實(shí)現(xiàn)垂直居中的一個缺點(diǎn)是僅限于一行。當(dāng)文本需要兩行或兩行以上時,每一行的高度將會太大。一個聰明的解決辦法是設(shè)置氣泡的display屬性為table,和包裝段落文本的displaytable-cell。這就允許我們將文本設(shè)為垂直居中。

<div class="speech-bubble speech-bubble-top">
    <p>Text goes here.</p>
</div>

接下來,修改CSS。

.speech-bubble {
 /* 其他樣式 */
 
  display: table;
}
 
.speech-bubble p {
  display: table-cell;
  vertical-align: middle;
}

如果引用display: table 帶來可怕的表格布局的老式回憶,別擔(dān)心。這些屬性是指顯示一個元素的樣式。

我們不局限于三角形;CSS能產(chǎn)生各種各樣的形狀,甚至心和生物危害標(biāo)志!

.biohazard {
  width: 0; height: 0;
 
  border: 60px solid;
  border-radius: 50%;
 
  border-top-color: black;
  border-bottom-color: black;
  border-left-color: yellow;
  border-right-color: yellow;
}

總結(jié)

雖然最簡單的border:1px solid black;語法很有幫助,如果我們聰明,我們可以創(chuàng)建各種有益的效果,圖標(biāo)和形狀。誰會想到邊界可以如此強(qiáng)大?關(guān)鍵是要記住常見的形狀或?qū)υ捒虻臉邮綉?yīng)該只被創(chuàng)建一次,然后抽象出來實(shí)用的類為將來使用。

原文:CSS Refreshers: Borders

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號