有些廣告區塊看起來質感很好,其實不一定需要背景圖,也能用純 CSS 做出投影與層次。以下以 300×250 的廣告區塊為例,說明如何用定位與邊框完成視覺效果。
外層容器負責定位與尺寸,內層可分為陰影層與廣告層。陰影層利用邊框形成斜角或視覺延伸,廣告層則承載實際內容。
.google {
position:relative;
width:440px;
height:260px;
}
.adsense {
width:300px;
height:250px;
border:5px solid #d2d2d2;
margin-left:130px;
position:absolute;
z-index:1;
}
.adsense:hover {
border-color:#565656;
}
.shadow {
border-color:#e9e9e9 #fff #fff;
border-style:solid;
border-width:130px 0 0 130px;
height:0;
width:0;
position:absolute;
left:0;
top:130px;
}這個做法主要利用定位與邊框組合,讓區塊產生視覺上的陰影與層次。優點是不需要圖片,能減少資源負擔;限制則是漸層變化較難還原,且部分舊版瀏覽器對 hover 效果支援有限。
整體而言,這種純 CSS 的處理方式很適合用於廣告框、卡片或提示區塊。只要掌握定位、邊框與 z-index 的搭配,就能做出輕量而有效果的介面。