邊界是眾所周知的,有什么新的東西嗎?好吧,我敢打賭,在這篇文章中,有很多你不看永遠不知道的東西!
不僅可以用CSS3來創建圓角,使用原有CSS一樣可以顯示自定義圖形。這是正確的(有待考究);在過去,沒發現這種技術之前,我們可能使用背景圖像定位來顯示一個園或箭頭。幸運的是,我們能放下PS圖象處理軟件了。
基礎
你可能很熟悉邊的最基本用法。
border: 1px solid black;
上面的代碼將給元素應用1px的邊。即簡潔又簡單;但我們也可以稍作修改。
border-width: thick;
border-style: solid;
border-color: black;
除了指定具體的邊框寬度值,也可以使用這三個關鍵詞:thin,
medium
和 thick
。

雖然乍看起來單個屬性的方式沒必要,但有極少數的情況下,當它是有利的,例如當你需要在特定的事件發生時更新邊的部分屬性。
也許你需要在用戶將鼠標懸停在一個特定的元素上時改變這個元素的邊框顏色。使用復合屬性需要重復像素值和邊的樣式。
box { border: 1px solid red; } .box:hover { border: 1px solid green; }
一個更優雅的和簡潔(DRY,don't repeat yourself)的做法是只更新邊的顏色屬性。
.box { border: 1px solid red; } .box:hover { border-color: green; }
此外,一會你會發現,這種單個屬性的方式有助於通過CSS創建自定義的形狀。
圓角
border-radius CSS3中的代表——第一個在社區中得到廣泛使用新屬性。這意味着,除去Internet Explorer 8及更低版本,所有的瀏覽器可以顯示圓角。
為了使樣式能正確應用,需要為Webkit和Mozilla內核添加前綴。
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
然而,今天我們不關心前綴,只簡單堅持官方形式:border-radius。

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

border-top-left-radius: 20px;
border-top-right-radius: 0;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 0;
在上面的代碼中,設置border-top-right-radius和border-bottom-left-radius為零是多余的,除非該元素有繼承的值。
就像margin和padding一樣,如果需要的話,這些設置可以合並為一個單一的屬性。
/* 左上角, 右上角, 右下角, 左下角 */ border-radius: 20px 0 30px 0;
舉個例子(網頁設計師經常這樣做),可以用CSS的border - radius屬性模擬檸檬的形狀,如下:
.lemon { width: 200px; height: 200px; background: #F5F240; border: 1px solid #F0D900; border-radius: 10px 150px 30px 150px; }

擴展知識
許多設計師一直用的是目前為止在本章列出的知識,然而,有一些方法我們可以進一步擴展!
多個邊
當給一個元素應用多個邊的時候,有各種各樣的技術可以參考。
邊的樣式
solid,dashed和dotted是最常用的border-style屬性值,還有其他幾個我們可以使用的值,包括groove和ridge。
border: 20px groove #e3e3e3;
或者寫成單個屬性形式:
border-color: #e3e3e3;
border-width: 20px;
border-style: groove;

雖然這看起來不錯,但ridge或groove效果並不是真正的多個邊。
輪廓
創建兩條邊的最流行的方式是利用outline屬性。
.box { border: 5px solid #292929; outline: 5px solid #e3e3e3; }

這個方法表現的非常棒,然而,最多兩個邊界。您應該需要創建一個層,實現漸變梯度效果,需要一種不同的方法。
偽元素
當輪廓技術無法滿足要求時,另一種方法是利用::before和:after偽元素,並利用生成內容產生額外的邊。
.box { width: 200px; height: 200px; background: #e3e3e3; position: relative; border: 10px solid green; } /* 創建和容器寬度相同的兩個容器 */ .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; }

這也許不是最優雅的方法,但它確實起作用了。需要注意的地方是很容易混淆邊界顏色的順序。確保正確的序列。
對象陰影
創建無限數量的邊界更酷的方法是利用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規范的本意。
通過設置x,y,和模糊屬性為0,我們可以使用多個值在需要的位置創建實線邊界。因為box-shadow屬性可以疊加,通過逗號分割,可以使用無限的值。
這種技術能很好的運行。在不能識別box-shadow屬性的老式瀏覽器中,這只會呈現單一紅色5 px邊界。
謹記:在所有瀏覽器里實現相同的設計是不必要的。為大部分現代瀏覽器書寫你的CSS,然后為老式瀏覽器提供可行的回退版本。
自定義角度
除了給border-radius傳遞一個值外,我們也可以提供兩個——由/分隔——為水平和垂直半徑指定不同的值。
例如……
border-radius: 50px / 100px; /* 水平半徑, 垂直半徑 */
……相當於:
border-top-left-radius: 50px 100px;
border-top-right-radius: 50px 100px;
border-bottom-right-radius: 50px 100px;
border-bottom-left-radius: 50px 100px;
這種技術是特別有用,當你需要模擬一個平緩的,冗長的曲線,而不是一個通用的圓角。例如,下面的代碼允許我們稍微變形一個正方形形狀,模擬出更多卷紙一樣的效果。
.box { width: 200px; height: 200px; background: #666; border-top-left-radius: 15em 1em; border-bottom-right-radius: 15em 1em; }

CSS形狀
也許最干脆的是直接使用邊界,給寬和高為零的元素巧妙的應用邊界。令人困惑,是嗎?讓我們看看一個演示。
在接下來的幾個例子,假設以下標記……
<div class="box"></div>
……和基本樣式如下:
.box { width: 200px; height: 200px; background: black; }
最常用的例子是如何使用CSS形狀創建一個箭頭。
假設一個有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; }
在本章的開始,更清潔的語法是不使用復合語法:
.arrow { width: 0; height: 0; border: 100px solid; border-top-color: red; border-right-color: green; border-bottom-color: blue; border-left-color: yellow; }
.arrow { width: 0; height: 0; border: 100px solid; border-color: red green blue yellow; }

很有趣,不是嗎?不過,當我們后退一步時更有趣。現在,如果我們將除了藍邊之外的所有的border-colors設置為透明的將會怎樣?
.arrow { width: 0; height: 0; border: 100px solid; border-bottom-color: blue; }

創建一個氣泡
創建一個100%CSS的氣泡,我們從下面的標記考試。
<div class="speech-bubble">Hi there!</div>
接下來,應用一些基本樣式。
.speech-bubble { position: relative; background-color: #292929; width: 200px; height: 150px; line-height: 150px; /* 垂直居中 */ color: white; text-align: center; }

箭頭將通過after偽元素實現。
.speech-bubble:after { content: ''; }
:before和:after偽元素可以用來在元素內容之前或之后插入生成內容。
接下來,只是簡單復制箭頭,並定位到適當的位置。我們開始通過絕對定位的內容,重置寬度和高度,並應用邊界顏色。
.speech-bubble:after { content: ''; position: absolute; width: 0; height: 0; border: 10px solid; border-color: red green blue yellow; }

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

當創建CSS形狀是,因為我們不能使用width屬性來指定箭頭的寬度,而是應該使用border-width屬性。在這種情況下,箭頭應該更大點;所以border-width可以增加到15 px。我們將箭頭定位到容器的底部居中,通過利用top和left屬性。
.speech-bubble:after { content: ''; position: absolute; width: 0; height: 0; border: 15px solid; border-top-color: red; top: 100%; left: 50%; }

.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; /* 調整邊框寬度 */ }

不錯,不是嗎?將這代碼抽象為幾個可重用的類,好應用到你將來的項目。
/* 對話氣泡
用法:使用.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; }

補充:更好的垂直居中
使用line-height實現垂直居中的一個缺點是僅限於一行。當文本需要兩行或兩行以上時,每一行的高度將會太大。一個聰明的解決辦法是設置氣泡的display屬性為table,和包裝段落文本的display為table-cell。這就允許我們將文本設為垂直居中。
<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 帶來可怕的表格布局的老式回憶,別擔心。這些屬性是指顯示一個元素的樣式。

.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; }
總結
雖然最簡單的border:1px solid black語法很有幫助,如果我們聰明,我們可以創建各種有益的效果,圖標和形狀。誰會想到邊界可以如此強大?關鍵是要記住常見的形狀或對話框的樣式應該只被創建一次,然后抽象出來實用的類為將來使用。