不少個人網站都會放一面個性圖片牆,靠小幅旋轉營造出手工拼貼的隨性感。這一節就用前面學過的 CSS3 知識把它做出來。
實現代碼如下:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS3實現個性圖片牆</title>
<style type="text/css">
#container {
position:relative;
width:800px;
height:600px;
margin:0 auto;
background-image:url("../App_images/lesson/run_css3/bg.jpg");
}
img {
position:absolute;
padding:10px;
background-color:White;
}
img:Hover { box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); }
#container img:first-child {
left:80px;
top:60px;
-webkit-transform:rotate(30deg);
}
#container img:nth-child(2) {
left:240px;
top:60px;
-webkit-transform:rotate(-30deg);
}
#container img:nth-child(3) {
left:420px;
top:60px;
-webkit-transform:rotate(30deg);
}
#container img:nth-child(4) {
left:100px;
top:240px;
-webkit-transform:rotate(-30deg);
}
#container img:nth-child(5) {
left:270px;
top:240px;
-webkit-transform:rotate(0);
}
#container img:last-child {
left:420px;
top:240px;
-webkit-transform:rotate(30deg);
}
</style>
</head>
<body>
<div id="container">
<img src="../App_images/lesson/run_css3/haizei1.png" alt=""/>
<img src="../App_images/lesson/run_css3/haizei2.png" alt=""/>
<img src="../App_images/lesson/run_css3/haizei3.png" alt=""/>
<img src="../App_images/lesson/run_css3/haizei4.png" alt=""/>
<img src="../App_images/lesson/run_css3/haizei5.png" alt=""/>
<img src="../App_images/lesson/run_css3/haizei6.png" alt=""/>
</div>
</body>
</html>
分析:
這個範例其實組合了三個知識點:用 position:absolute 搭配 left/top 把每張照片釘在容器上,用 rotate() 給出 ±30 度的傾斜,再用結構偽類選擇器(:first-child、:nth-child(n)、:last-child)分別指定各自的角度。
滑鼠移入時的陰影由 box-shadow 負責,rgba(0,0,0,0.2) 讓陰影保持柔和而不突兀;img 上的 padding 加白底則模擬出相紙的白邊。
版面配置只是示範,真正好玩的地方在你自己:換成手邊的照片、調整旋轉角度與間距,就能做出屬於自己的拼貼風格。