DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5教程
純 HTML5 + CSS3 手繪多啦A夢:零圖片的形狀組合術
編輯:HTML5教程     

不用任何圖片素材,只用 div 加上 CSS3 的 border-radius、transform 與絕對定位,就能畫出一隻多啦A夢。這個練習很適合拿來熟悉 CSS 的定位與變形機制。完整程式碼如下。

做法拆解

整體包在一個 .whole 容器裡,再分成頭、臉、脖子與鈴鐺、身體、手、腳幾個區塊。每個區塊都是絕對定位的方塊,靠圓角拉出曲線,靠 rotate 轉出鬍鬚與手臂的角度,最後用 z-index 決定前後遮蓋關係。

完整程式碼

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>機器貓</title>
<style type="text/css">
* { margin: 0; padding: 0; }
.whole {
    width: 800px;
    margin: 20px auto;
    background-color: white;
    position: relative;
}
.head {
    margin: 0 auto;
    position: relative;
    width: 500px;
    height: 440px;
    background-color: #00b7e7;
    border-radius: 220px;
    border: 1px solid red;
}
/* 眼睛 */
.eye .left_eye,
.eye .right_eye {
    width: 100px; height: 130px;
    background-color: white;
    border: 2px solid black;
    border-radius: 50px;
    position: absolute;
    top: 50px;
    z-index: 3;
}
.eye .LeyeBall,
.eye .ReyeBall {
    width: 20px; height: 20px;
    background: black;
    border-radius: 10px;
    position: absolute;
    top: 65px;
}
.eye .left_eye  { left: 146px; }
.eye .right_eye { left: 250px; }
.eye .LeyeBall  { right: 10px; }
.eye .ReyeBall  { left: 10px; }
/* 臉 */
.face { position: relative; z-index: 2; }
.face .feature {
    width: 400px; height: 320px;
    border-radius: 160px;
    position: absolute;
    top: 100px; left: 50px;
    background: white;
}
.face .nose {
    width: 45px; height: 50px;
    border-radius: 23px;
    background-color: #cf3318;
    border: 2px solid black;
    position: absolute;
    top: 165px; left: 225px;
    z-index: 3;
}
.face .Nline {
    width: 3px; height: 160px;
    background: black;
    position: absolute;
    top: 218px; left: 248px;
    z-index: 3;
}
.face .mouth {
    width: 280px; height: 280px;
    border-bottom: 5px solid black;
    border-radius: 140px;
    position: absolute;
    top: 98px; left: 105px;
}
/* 鬍鬚 */
.face .mustache .MutR_higher,
.face .mustache .MutR_middle,
.face .mustache .MutR_lower,
.face .mustache .MutL_top,
.face .mustache .MutL_center,
.face .mustache .MutL_bottom {
    width: 80px; height: 2px;
    background: black;
    position: absolute;
    z-index: 2;
}
.face .mustache .MutR_higher { top: 220px; left: 295px; }
.face .mustache .MutR_middle { top: 240px; left: 295px; }
.face .mustache .MutR_lower  { top: 260px; left: 295px; }
.face .mustache .MutL_top    { top: 220px; left: 115px; }
.face .mustache .MutL_center { top: 240px; left: 115px; }
.face .mustache .MutL_bottom { top: 260px; left: 115px; }
.face .mustache .MutL_bottom,
.face .mustache .MutR_higher { transform: rotate(160deg); }
.face .mustache .MutL_top,
.face .mustache .MutR_lower  { transform: rotate(200deg); }
/* 脖子與鈴鐺 */
.neck {
    width: 300px; height: 30px;
    background-color: #a31f12;
    border: 2px solid black;
    border-radius: 15px;
    position: relative;
    top: 0; left: 250px;
    z-index: 4;
}
.neck .bell {
    width: 60px; height: 60px;
    overflow: hidden;
    border: 2px solid black;
    border-radius: 60px;
    background-color: #cfcb3c;
    position: absolute;
    top: 5px; left: 120px;
}
.bell .Bline {
    width: 60px; height: 2px;
    background-color: #cfcb3c;
    border: 2px solid black;
    border-radius: 3px 3px 0 0;
    position: absolute;
    top: 15px;
}
.bell .Bcircle {
    width: 20px; height: 16px;
    background: black;
    border-radius: 8px;
    position: absolute;
    top: 25px; left: 20px;
}
.bell .Bunderpart {
    width: 3px; height: 20px;
    background-color: black;
    position: absolute;
    left: 28px; top: 40px;
}
/* 身體 */
.body { position: relative; top: -300px; z-index: 1; }
.body .tummy {
    width: 280px; height: 240px;
    background-color: #00b1e1;
    border: 2px solid black;
    position: absolute;
    top: 267px; left: 260px;
}
.body .bellyband {
    width: 220px; height: 220px;
    background-color: white;
    border: 2px solid black;
    border-radius: 110px;
    position: absolute;
    left: 290px; top: 267px;
}
.body .pocket {
    width: 160px; height: 160px;
    border-radius: 80px;
    background-color: white;
    border: 2px solid black;
    position: absolute;
    top: 305px; left: 320px;
}
.cover {
    width: 164px; height: 80px;
    background-color: white;
    border-bottom: 2px solid black;
    position: absolute;
    top: 300px; left: 320px;
}
/* 手 */
.left_hand,
.right_hand {
    height: 100px; width: 100px;
    position: absolute;
    top: 272px;
}
.left_hand  { left: -10px; }
.right_hand { left: -10px; }
.left_hand .Larm,
.right_hand .Rarm {
    width: 70px; height: 100px;
    background-color: #00bee8;
    border: 1px solid black;
    position: relative;
    top: 200px;
}
.left_hand .Larm  { left: 535px; transform: rotateZ(135deg); }
.right_hand .Rarm { left: 215px; transform: rotateZ(45deg); }
.left_hand .Lpalm,
.right_hand .Rpalm {
    width: 80px; height: 80px;
    border-radius: 40px;
    border: 2px solid black;
    background-color: white;
    position: absolute;
    z-index: 1;
}
.right_hand .Rpalm { left: 580px; top: 260px; }
.left_hand .Lpalm  { left: 160px; top: 260px; }
/* 腳 */
.foot .left_foot,
.foot .right_foot {
    width: 150px; height: 40px;
    background-color: white;
    border: 2px solid black;
    border-radius: 80px 60px 60px 40px;
    position: relative;
}
.foot .left_foot  { left: 240px; top: 210px; }
.foot .right_foot { top: 165px; left: 410px; }
.foot .crotch {
    width: 40px; height: 20px;
    background-color: white;
    border: 2px solid black;
    border-bottom: none;
    border-radius: 40px 40px 0 0;
    position: relative;
    top: 103px; left: 382px;
    z-index: 2;
}
</style>
</head>
<body>
<div class="wrap">
<div class="whole">
  <!-- 頭 -->
  <div class="head">
    <!-- 眼 -->
    <div class="eye">
      <div class="left_eye"><div class="LeyeBall"></div></div>
      <div class="right_eye"><div class="ReyeBall"></div></div>
    </div>
    <!-- 臉 -->
    <div class="face">
      <div class="feature"></div>
      <div class="nose"></div>
      <div class="Nline"></div>
      <div class="mouth"></div>
      <!-- 鬍鬚 -->
      <div class="mustache">
        <div class="MutL_top"></div>
        <div class="MutL_center"></div>
        <div class="MutL_bottom"></div>
        <div class="MutR_higher"></div>
        <div class="MutR_middle"></div>
        <div class="MutR_lower"></div>
      </div>
    </div>
  </div>
  <!-- 脖子 -->
  <div class="neck">
    <!-- 鈴鐺 -->
    <div class="bell">
      <div class="Bline"></div>
      <div class="Bcircle"></div>
      <div class="Bunderpart"></div>
    </div>
  </div>
  <!-- 身體 -->
  <div class="body">
    <div class="tummy"></div>
    <div class="bellyband"></div>
    <div class="pocket"></div>
    <div class="cover"></div>
  </div>
  <!-- 左手 -->
  <div class="left_hand">
    <div class="Larm"></div>
    <div class="Lpalm"></div>
  </div>
  <!-- 右手 -->
  <div class="right_hand">
    <div class="Rarm"></div>
    <div class="Rpalm"></div>
  </div>
  <!-- 腳 -->
  <div class="foot">
    <div class="left_foot"></div>
    <div class="right_foot"></div>
    <div class="crotch"></div>
  </div>
</div>
</div>
</body>
</html>

幾個關鍵技巧

用 border-radius 拉曲線。頭部用 border-radius: 220px 直接變圓,而「只有下緣有線條的嘴」則靠 border-bottom 搭配圓角,只讓某一邊的框線顯示出來。

用旋轉畫斜線。鬍鬚本質上是 80×2 的細長條,透過 rotate(160deg) 與 rotate(200deg) 轉出左右對稱的張角;手臂同理,用 rotateZ(45deg)/rotateZ(135deg) 擺出張開的姿勢。

z-index 決定層序。眼睛(3)要壓在臉(2)之上,鈴鐺與項圈(4)要壓在頭與身體之上,手掌(1)則要壓在手臂之上。層級安排錯了,畫面就會出現奇怪的穿插。

負值定位做上移。.body 用 top: -300px 把整個身體往上拉,讓它與頭部在視覺上重疊接合——這是全篇最需要試誤的地方。

可以怎麼改進

這份程式碼大量使用絕對定位與寫死的像素值,缺點是完全無法縮放:改動 .whole 的寬度不會有任何效果。若想讓它自適應,可以改用 transform: scale() 整體縮放,或把單位換成 %/em,甚至改用 SVG 的 <circle>、<path> 會更精簡。但作為 CSS 定位與變形的練習題,這種「用方塊拼圖」的寫法反而最能釐清每個屬性的作用。

以上就是這次的實作分享,若有任何疑問歡迎留言,我會盡快回覆。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved