在「CSS 邊框 border」一節中,我們已經把邊框屬性講得很細。替圖片加上邊框,用的也是同一個 border 屬性,語法完全通用。
語法:
border-width:像素值; border-style:屬性值; border-color:顏色值;
註:實務上多採用 border 簡潔寫法,例如「border:1px solid gray;」。
說明:
若忘了 border 各值的含義,請自行回去複習那一節。
舉例 1:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>圖片邊框border</title>
<style type="text/css">
img { width:60px; height:60px; border:1px solid red; }
</style>
</head>
<body>
<img src="../App_images/lesson/run_cj/cartoongirl.gif" alt=""/>
</body>
</html>
在瀏覽器中預覽,效果如下:
舉例 2:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>圖片邊框border</title>
<style type="text/css">
img{width:60px;height:60px;}
img:hover{border:1px solid gray;}
</style>
</head>
<body>
<img src="../App_images/lesson/run_cj/cartoongirl.gif" alt=""/>
</body>
</html>
在瀏覽器中預覽,效果如下:
分析:
這裡用到了「:hover 偽類」,讓滑鼠移經圖片時才浮現灰色邊框,移開即消失。這類細節互動在圖片列表、產品縮圖上很常見,成本極低但對質感的提升很明顯。