邊界是眾所周知的,有什么新的東西嗎?好吧,我敢打賭,在這篇文章中,有很多你不看永遠不知道的東西!
不僅可以用CSS3來創(chuàng)建圓角,使用原有CSS一樣可以顯示自定義圖形。這是正確的(有待考究);在過去,沒發(fā)現這種技術之前,我們可能使用背景圖像定位來顯示一個園或箭頭。幸運的是,我們能放下PS圖象處理軟件了。
你可能很熟悉邊的最基本用法。
border: 1px solid black;
上面的代碼將給元素應用1px的邊。即簡潔又簡單;但我們也可以稍作修改。
border-width: thick;
border-style: solid;
border-color: black;
除了指定具體的邊框寬度值,也可以使用這三個關鍵詞:thin
,medium
和 thick
。
雖然乍看起來單個屬性的方式沒必要,但有極少數的情況下,當它是有利的,例如當你需要在特定的事件發(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ā)現,這種單個屬性的方式有助于通過CSS創(chuàng)建自定義的形狀。
border-radius
CSS3中的代表——第一個在社區(qū)中得到廣泛使用新屬性。這意味著,除去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
,dashe
d和dotted
是最常用的border-style
屬性值,還有其他幾個我們可以使用的值,包括groove
和ridge
。
border: 20px groove #e3e3e3;
或者寫成單個屬性形式:
border-color: #e3e3e3;
border-width: 20px;
border-style: groove;
雖然這看起來不錯,但ridge
或groove
效果并不是真正的多個邊。
創(chuàng)建兩條邊的最流行的方式是利用outline
屬性。
.box {
border: 5px solid #292929;
outline: 5px solid #e3e3e3;
}
這個方法表現的非常棒,然而,最多兩個邊界。您應該需要創(chuàng)建一個層,實現漸變梯度效果,需要一種不同的方法。
當輪廓技術無法滿足要求時,另一種方法是利用::before
和:after
偽元素,并利用生成內容產生額外的邊。
.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)雅的方法,但它確實起作用了。需要注意的地方是很容易混淆邊界顏色的順序。確保正確的序列。
創(chuàng)建無限數量的邊界更酷的方法是利用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ī)范的本意。
通過設置x
,y
,和模糊屬性為0
,我們可以使用多個值在需要的位置創(chuàng)建實線邊界。因為box-shadow
屬性可以疊加,通過逗號分割,可以使用無限的值。
這種技術能很好的運行。在不能識別box-shadow
屬性的老式瀏覽器中,這只會呈現單一紅色5px
邊界。
謹記:在所有瀏覽器里實現相同的設計是不必要的。為大部分現代瀏覽器書寫你的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;
}
也許最干脆的是直接使用邊界,給寬和高為零的元素巧妙的應用邊界。令人困惑,是嗎?讓我們看看一個演示。
在接下來的幾個例子,假設以下標記……
<div class="box"></div>
……和基本樣式如下:
.box {
width: 200px;
height: 200px;
background: black;
}
最常用的例子是如何使用CSS形狀創(chuàng)建一個箭頭。
關鍵是了解如何用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;
}
很有趣,不是嗎?不過,當我們后退一步時更有趣?,F在,如果我們將除了藍邊之外的所有的border-color
設置為透明的將會怎樣?
.arrow {
width: 0; height: 0;
border: 100px solid;
border-bottom-color: blue;
}
太棒了!但用div
創(chuàng)建一個箭頭似乎不太符合語義化。然而,通過after
或before
等相關偽元素可以用來創(chuàng)建箭頭。
創(chuàng)建一個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;
}
當創(chuàng)建CSS形狀是,因為我們不能使用width屬性來指定箭頭的寬度,而是應該使用border-width
屬性。在這種情況下,箭頭應該更大點;所以border-width
可以增加到15px
。我們將箭頭定位到容器的底部居中,通過利用top
和left
屬性。
.speech-bubble:after {
content: '';
position: absolute;
width: 0;
height: 0;
border: 15px solid;
border-top-color: red;
top: 100%;
left: 50%;
}
到這里就差不多了;最后一個步驟是更新箭頭的顏色和容器的背景顏色相同。定位也需要修改,根據邊界的寬度(15 px
)。當我們在這里,我們還將應用一個微妙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; /* 調整邊框寬度 */
}
不錯,不是嗎?將這代碼抽象為幾個可重用的類,好應用到你將來的項目。
/*
對話氣泡
用法:使用.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
帶來可怕的表格布局的老式回憶,別擔心。這些屬性是指顯示一個元素的樣式。
我們不局限于三角形;CSS能產生各種各樣的形狀,甚至心和生物危害標志!
.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;
語法很有幫助,如果我們聰明,我們可以創(chuàng)建各種有益的效果,圖標和形狀。誰會想到邊界可以如此強大?關鍵是要記住常見的形狀或對話框的樣式應該只被創(chuàng)建一次,然后抽象出來實用的類為將來使用。
注
更多建議: