這個範例要做出的是「在地面上鑿出一圈凹槽」的效果。做法並不難:外圈用 linear-gradient 線性漸層加內陰影,讓上緣偏暗、下緣偏亮,看起來就像被壓進地面;再用 :after 偽元素畫出中央的圓形凸塊,透過它的大小、位置與陰影,營造出環與中心之間的深度關係;最後只要替換背景色,就能換出不同顏色的環。第四個環上多一個小凸塊,同樣用 :after 處理,並以 z-index 決定疊放順序。
四個環共用 .circlegraph 類別,只用 margin-left 把它們排成一列,再用 .white/.blue/.orange/.red 切換顏色。
body, html { height: 100%; width: 100%; }
body {
font-size: 100%;
background-color: #ddd;
color: #524d46;
text-align: center;
}
.circlegraph {
display: block;
width: 12em;
height: 12em;
border-radius: 50%;
/* 內陰影是凹陷感的關鍵 */
box-shadow: .30em .375em .625em rgba(0,0,0,.25) inset,
0em 0em 1.25em rgba(0,0,0,.2) inset;
position: absolute;
left: 50%;
top: 50%;
margin-left: -6em;
margin-top: -6em;
background: linear-gradient(to bottom,
rgba(0,0,0,0.2) 0%,
rgba(255,255,255,0.2) 100%);
}
/* 中央凸起的圓塊 */
.circlegraph:after {
display: block;
content: "";
width: 8em;
height: 8em;
background-color: #f9f8f4;
border-radius: 50%;
position: absolute;
left: 2em;
top: 2em;
box-shadow: 0 0 1em rgba(0,0,0,.25);
}
.white { margin-left: -500px; }
.blue { background-color: #26b7ff; margin-left: -250px; }
.orange { background-color: #ff9300; margin-left: 0px; }
.red { background-color: #DE6040; margin-left: 250px; }
/* 第四個環上的第二個小凸塊 */
.above:after {
display: block;
content: "";
width: 4em;
height: 4em;
background-color: #fff;
border-radius: 50%;
position: absolute;
left: 4em;
top: 4em;
box-shadow: 0.1em 0.4em 0.5em rgba(0,0,0,.25);
z-index: 1;
}
<div class="circlegraph white"></div>
<div class="circlegraph orange"></div>
<div class="circlegraph blue"></div>
<div class="circlegraph red"><div class="above"></div></div>
尺寸全部使用 em 而非 px,這樣只要改動 body 的 font-size,整組圖形就能等比例縮放,這是做純 CSS 圖形時很值得借用的技巧。定位則用 left/top: 50% 搭配負的 margin 來置中,比 transform: translate(-50%, -50%) 兼容性更好,在舊版瀏覽器上也能正常運作。
漸層段落中可以看到完整的跨瀏覽器前綴:Firefox 與 Chrome 能得到最準確的還原效果,IE8 以下則完全不支援;若必須支援舊 IE,可以引入 css3pie 之類的 htc 修補程式,讓圓角與陰影至少能呈現個大概。