邊框有上下左右四條邊。上一節學的是四條邊的整體設定,那如果只想單獨調整其中某一條呢?
好消息是,CSS 允許我們針對四條邊分別設定樣式。
border-top-width:1px; border-top-style:solid; border-top-color:red;
簡潔寫法:border-top:1px solid red;
border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:orange;
簡潔寫法:border-bottom:1px solid orange;
border-left-width:1px; border-left-style:solid; border-left-color:blue;
簡潔寫法:border-left:1px solid blue;
border-right-width:1px; border-right-style:solid; border-right-color:red;
簡潔寫法:border-right:1px solid green;
不論整體還是局部,原則不變:粗細、外觀、顏色三者都要交代。
忘記要設哪些屬性也沒關係,Visual Studio 有代碼提示,多寫幾次自然就記住了。
舉例 1:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
#div1 { width:100px; /*div元素寬為100px*/ height:60px; /*div元素高為60px*/ border-top:1px solid red; /*上邊框樣式*/ border-right:1px solid orange; /*右邊框樣式*/ border-bottom:1px solid blue; /*下邊框樣式*/ border-left:1px solid green; /*左邊框樣式*/ }
</style>
</head>
<body>
<div id="div1"></div>
</body>
</html>
在瀏覽器中預覽,效果如下:
四條邊各自呈現不同顏色,這是局部樣式最直觀的展示。
舉例 2:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
#div1 { width:100px; /*div元素寬為100px*/ height:60px; /*div元素高為100px*/ border:1px solid gray; /*邊框整體樣式*/ border-bottom:0px; /*去除下邊框*/ }
</style>
</head>
<body>
<div id="div1"></div>
</body>
</html>
在瀏覽器中預覽,效果如下:
分析:
「border-bottom:0px;」把下邊框的粗細設為 0,沒有寬度自然看不見,於是下邊框就被移除了。新手常疑惑:難道不需要像「border-bottom:0px solid gray;」那樣把樣式與顏色也寫上嗎?實際上這是省略寫法——既然這條邊框都不要了,再指定它的樣式和顏色又有什麼意義?
移除邊框有兩種等價寫法:「border-bottom:0px」與「border-bottom:none」,兩者效果相同,任選其一即可。