新浪有兩隻吉祥物,小新是女生,小浪是男生。既然現在流行用 CSS3 畫圖,那 SINA CDC 的模特兒自然就是他們了。
熬了一晚做出小新的頭,還不到完美,仍有更像的空間。訣竅其實只有一句:用各種圓形互相堆疊,拼出一個形體。
先來看跨瀏覽器的成果。就圖形表現而言,Firefox 最乾淨;Safari 與 Chrome 邊緣略有毛邊,但優勢是支援 CSS 動畫。至於 IE,就當它是路過的。
本次用到的 CSS3 屬性:
各瀏覽器下的實測:Firefox 3.6.8、Chrome 5.0、Safari 5.0、IE6/IE7/IE8。
整體思路非常直白:先畫一個長方塊,用 CSS3 為它加上圓角、陰影、傾斜與漸層光效,這就是小新的臉。
接著:
眼睛就容易多了:先畫一個白色圓形,加灰邊與粉色上陰影;再蓋一個棕色圓,加黑邊與陰影;最後點上兩個小白點,就是眼神。
組合起來即完成。

實現起來最花時間的不是技術難度,而是反覆調位置的耐心。需要特別注意的是 position 與 z-index 的用法——堆疊順序錯了,畫面就會前後倒置。
當然,這還比不上設計師用 PS 畫的那般生動,程式碼版本的小新,那幾捲頭髮看著像一根根粗壯的小手臂。組裡同學畫的小浪相當漂亮。但抱著精心製作的精神,相信你會畫得更好。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>小新頭像-新浪CDC</title>
</head>
<style>
.sina{ margin:60px 0 0 22px;}
.head{ position:relative; width:195px; height:220px;}
.head_bg{ position:absolute; width:200px; height:175px; background:#ab2b41; left:6px; z-index:6;
-moz-border-radius-topleft:200px 180px;
-moz-border-radius-topright: 200px 180px;
-moz-border-radius-bottomleft: 200px 180px;
-moz-border-radius-bottomright: 200px 180px;
-webkit-border-radius:200px 180px;
-moz-transform: rotate(-6deg);
-webkit-transform:rotate(-6deg);
-moz-box-shadow:1px 4px 3px #333;
-webkit-box-shadow:1px 4px 3px #333;
border:1px solid #c03461;
}
.head_top{ position:absolute; top:-3px; left:5px; width:197px; height:167px; z-index:7;
-moz-border-radius-topleft:200px 180px;
-moz-border-radius-topright:200px 180px;
-moz-border-radius-bottomleft:200px 180px;
-moz-border-radius-bottomright:200px 180px;
-webkit-border-radius:200px 180px;
-moz-transform:rotate(-6deg);
-webkit-transform:rotate(-6deg);
background: -moz-linear-gradient(left top, #e16591, #ce3551 50%);
background: -webkit-gradient(linear, 0 0, 0 50%, from(#e16591), to(#ce3551));
}
.head_shadow{ position:absolute; width:190px; height:162px; top:2px; left:10px; z-index:8;
-moz-border-radius-topleft:200px 180px;
-moz-border-radius-topright:200px 180px;
-moz-border-radius-bottomleft:200px 180px;
-moz-border-radius-bottomright:200px 180px;
-webkit-border-radius:200px 180px;
background: -moz-linear-gradient(left top, white, #ce3552 50%);
background: -webkit-gradient(linear, left top, 10% 50%, from(#fff), to(#ce3552));
}
.head_a1{ width:54px; height:45px; position:absolute; top:-24px; right:32px; background:#d53960; z-index:5;
border:1px solid #c03461;
-moz-border-radius:180px 0;
-webkit-border-radius:180px 0 180px 0;
-moz-transform: rotate(-6deg);
-webkit-transform:rotate(-6deg);
-moz-box-shadow:4px 4px 0px #c03461;
-webkit-box-shadow:4px 4px 0px #c03461;
}
.head_a1 .top{width:100px; height:100px; position:absolute; top:-80px; right:-11px; background:#fff; z-index:1;
-moz-border-radius:135px;
-webkit-border-radius:135px;
}
.head_a1 .body{width:23px; height:23px; position:absolute; top:-10px; right:-3px; background:#d53960; z-index:2;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-top-left-radius:135px;
-webkit-border-top-right-radius:135px;
-webkit-border-bottom-right-radius:135px;
-webkit-border-bottom-left-radius:0;
-moz-transform: rotate(-60deg);
-webkit-transform: rotate(-60deg);
-moz-box-shadow:3px 4px 0px #c03461;
-webkit-box-shadow:3px 4px 0px #c03461;
}
.head_a1 .shadow{width:19px; height:19px; position:absolute; top:-8px; right:-2px; z-index:3;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-60deg);
-webkit-transform: rotate(-60deg);
background: -moz-linear-gradient(top, white, #ce3552 50%);
background: -webkit-gradient(linear, left top, 0 50%, from(#fff), to(#ce3552));
}
.head_a2{ width:60px; height:45px; position:absolute; top:-22px; right:-13px; background:#d53960; z-index:4;
border:1px solid #c03461;
-moz-border-radius:180px 0;
-webkit-border-radius:180px 0 180px 0;
-moz-transform: rotate(-10deg);
-webkit-transform: rotate(-10deg);
-moz-box-shadow:4px 4px 0px #c03461;
-webkit-box-shadow:4px 4px 0px #c03461;
}
.head_a2 .top{width:40px; height:38px; position:absolute; top:-12px; right:7px; background:#fff; z-index:1;
-moz-border-radius:180px 0 180px 0;
-webkit-border-radius:180px 0 180px 0;
-moz-transform: rotate(20deg);
-webkit-transform: rotate(20deg);
}
.head_a2 .body{width:23px; height:23px; position:absolute; top:-10px; right:-3px; background:#d53960; z-index:2;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-60deg);
-webkit-transform: rotate(-60deg);
-moz-box-shadow:3px 4px 0px #c03461;
-webkit-box-shadow:3px 4px 0px #c03461;
}
.head_a2 .shadow{width:19px; height:19px; position:absolute; top:-8px; right:-1px; z-index:3;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-60deg);
-webkit-transform: rotate(-60deg);
background: -moz-linear-gradient(top, white, #ce3552 50%);
background: -webkit-gradient(linear, left top, 0 50%, from(#fff), to(#ce3552));
}
.head_a3{ width:80px; height:47px; position:absolute; top:6px; right:-46px; background:#d53960; z-index:3;
border:1px solid #c03461;
-moz-border-radius:100px 0 110px 0;
-webkit-border-radius:100px 0 110px 0;
-moz-transform: rotate(-10deg);
-webkit-transform: rotate(-10deg);
-moz-box-shadow:4px 4px 0px #c03461;
-webkit-box-shadow:4px 4px 0px #c03461;
}
.head_a3 .top{width:67px; height:38px; position:absolute; top:-18px; right:15px; background:#fff; z-index:1;
-moz-border-radius:180px 0 180px 0;
-webkit-border-radius:180px 0 180px 0;
-moz-transform: rotate(40deg);
-webkit-transform: rotate(40deg);
}
.head_a3 .body{width:23px; height:23px; position:absolute; top:-8px; right:-1px; background:#d53960; z-index:2;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-72deg);
-webkit-transform: rotate(-72deg);
-moz-box-shadow:3px 4px 0px #c03461;
-webkit-box-shadow:3px 4px 0px #c03461;
}
.head_a3 .shadow{width:19px; height:19px; position:absolute; top:-6px; right:0px; z-index:3;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-60deg);
-webkit-transform: rotate(-60deg);
background: -moz-linear-gradient(top, white, #ce3552 50%);
background: -webkit-gradient(linear, left top, 0 50%, from(#fff), to(#ce3552));
}
.head_a4{ width:80px; height:47px; position:absolute; top:44px; right:-55px; background:#d53960; z-index:2;
border:1px solid #c03461;
-moz-border-radius:80px 0 110px 0;
-webkit-border-radius:80px 0 110px 0;
-moz-transform: rotate(8deg);
-webkit-transform: rotate(8deg);
-moz-box-shadow:4px 4px 0px #c03461;
-webkit-box-shadow:4px 4px 0px #c03461;
}
.head_a4 .top{width:67px; height:38px; position:absolute; top:-18px; right:13px; background:#fff; z-index:1;
-moz-border-radius:180px 0 180px 0;
-webkit-border-radius:180px 0 180px 0;
-moz-transform: rotate(40deg);
-webkit-transform: rotate(40deg);
}
.head_a4 .body{width:23px; height:23px; position:absolute; top:-9px; right:-2px; background:#d53960; z-index:2;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-70deg);
-webkit-transform: rotate(-70deg);
-moz-box-shadow:3px 4px 0px #c03461;
-webkit-box-shadow:3px 4px 0px #c03461;
}
.head_a4 .shadow{width:19px; height:19px; position:absolute; top:-7px; right:-1px; z-index:3;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-70deg);
-webkit-transform: rotate(-70deg);
background: -moz-linear-gradient(top, white, #ce3552 50%);
background: -webkit-gradient(linear, left top, 0 50%, from(#fff), to(#ce3552));
}
.head_a5{ width:54px; height:45px; position:absolute; top:87px; right:-45px; background:#d53960; z-index:1;
border:1px solid #c03461;
-moz-border-radius:135px 0 180px 0;
-webkit-border-radius:135px 0 180px 0;
-moz-transform: rotate(35deg);
-webkit-transform: rotate(35deg);
-moz-box-shadow:4px 4px 0px #c03461;
-webkit-box-shadow:4px 4px 0px #c03461;
}
.head_a5 .top{width:100px; height:100px; position:absolute; top:-78px; right:-7px; background:#fff; z-index:1;
-moz-border-radius:135px;
-webkit-border-radius:135px;
}
.head_a5 .body{width:23px; height:23px; position:absolute; top:-6px; right:0px; background:#d53960; z-index:2;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px;
-moz-transform: rotate(-70deg);
-webkit-transform: rotate(-70deg);
-moz-box-shadow:3px 4px 0px #c03461;
-webkit-box-shadow:3px 4px 0px #c03461;
}
.head_a5 .shadow{width:19px; height:19px; position:absolute; top:-4px; right:2px; z-index:3;
-moz-border-radius:135px 135px 135px 0;
-webkit-border-radius:135px 135px 135px 0;
-moz-transform: rotate(-70deg);
-webkit-transform: rotate(-70deg);
background: -moz-linear-gradient(top, white, #ce3552 50%);
background: -webkit-gradient(linear, left top, 0 50%, from(#fff), to(#ce3552));
}
.eye{ position:absolute; top:47px; left:27px; width:161px; height:88px; z-index:10;
-moz-transform: rotate(-2deg);
-webkit-transform: rotate(-2deg);
}
.eye1{ position:absolute; top:57px; left:59px; width:8px; height:8px; background:#fff; z-index:5;
-moz-border-radius:180px;
-webkit-border-radius:180px;
}
.eye2{ position:absolute; top:29px; left:81px; width:22px; height:22px; background:#fff; z-index:4;
-moz-border-radius:180px;
-webkit-border-radius:180px;
}
.eye3{ position:absolute; top:25px; left:56px; width:47px; height:47px; background:#000; z-index:3;
-moz-border-radius:180px;
-webkit-border-radius:180px;
}
.eye4{ position:absolute; top:14px; left:46px; width:67px; height:67px; border:1px solid #312613; background:#66383d; z-index:2;
-moz-border-radius:180px;
-webkit-border-radius:180px;
-moz-box-shadow:0px 0px 5px #000;
-webkit-box-shadow:0px 0px 5px #000;
}
.eye5{ position:absolute; top:1px; left:8px; width:137px; height:86px; background:#fff; z-index:1;
border-top:5px solid #999; border-left:5px solid #999;
-moz-border-radius:300px 30px 300px 30px;
-webkit-border-radius:300px 30px 300px 30px;
-moz-transform: rotate(20deg);
-webkit-transform: rotate(20deg);
-moz-box-shadow:0px -7px 0 #ec9dc2;
-webkit-box-shadow:0px -7px 0 #ec9dc2;
}
</style>
<body>
<div class="sina">
<div class="head">
<div class="head_bg"></div>
<div class="head_top"></div>
<div class="head_shadow"></div>
<div class="head_a1">
<div class="top"></div>
<div class="body"></div>
<div class="shadow"></div>
</div>
<div class="head_a2">
<div class="top"></div>
<div class="body"></div>
<div class="shadow"></div>
</div>
<div class="head_a3">
<div class="top"></div>
<div class="body"></div>
<div class="shadow"></div>
</div>
<div class="head_a4">
<div class="top"></div>
<div class="body"></div>
<div class="shadow"></div>
</div>
<div class="head_a5">
<div class="top"></div>
<div class="body"></div>
<div class="shadow"></div>
</div>
<div class="eye">
<div class="eye1"></div>
<div class="eye2"></div>
<div class="eye3"></div>
<div class="eye4"></div>
<div class="eye5"></div>
</div>
</div>
</div>
</body>
</html>
這份程式碼最值得借鑑的地方,是把「一張圖」拆成「多個圓形 + 層級」的思路。同一套方法也能拿來畫圖示、載入動畫或裝飾插畫,而且 SVG 崛起之前,這幾乎是唯一不用圖片就能畫圖的路。