DIV CSS 佈局教程網

前端新手實戰:動手做一個淘寶風格的首頁
編輯:關於JavaScript     

學 CSS 最有效的方式,不是把每個屬性背下來,而是完整做過一版真實的頁面。淘寶首頁是經典的練習素材——它有固定定位的頂欄、三段式搜尋框、左側分類列表、輪播區、多欄資訊流與商品牆,幾乎把常用排版手法都用上了。本文帶你拆解它的結構與實作思路。

先想清楚:版面怎麼切

動手之前先做一件事——把整頁按「通欄區塊」切開,每一塊都統一用一個 .w 版心容器包起來:

.header      /* 頂部導覽,position: fixed 固定在視窗上方 */
.head_main   /* logo + 搜尋框 + 手機二維碼 */
.center_top  /* 左側商品分類 + 右側輪播與個人資訊 */
.center_top2 /* 主題市場等四欄快捷入口 */
.main        /* 三欄主體:特色市場 / 逛逛精選 / 我的淘寶 */
.main_down   /* 熱賣單品商品牆 */
.main_help   /* 消費者保障等服務說明 */
.footer      /* 友情連結與備照 */

版心的寫法全站共用,這是最省事的基礎建設:

.w { width: 1200px; margin: 0 auto; }

切好區塊之後,每一塊都是獨立的小任務,除錯難度立刻下降一個等級。

HTML 骨架

以下保留主要結構,同一區塊內重複的卡片以註解表示,實際開發時複製貼上即可。

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>淘寶風格首頁</title>
<link rel="stylesheet" href="css/base.css">
<style type="text/css"></style>
</head>
<body>

<!-- 頂部導覽 -->
<div class="header">
  <div class="w">
    <div class="top_left">
      <div class="top_left_1"><a href="#" class="top_left_1_a">淘寶</a><span class="top_left_1_span"></span></div>
      <div class="top_left_2"><a href="#" class="top_left_2_a">消息</a><span class="top_left_2_span"></span></div>
      <div class="top_left_3"><a href="#" class="top_left_3_a">手機淘寶</a></div>
    </div>
    <div class="top_right">
      <div class="top_right_1"><a href="#" class="top_right_1_a">我的淘寶</a></div>
      <div class="top_right_2"><a href="#" class="top_right_2_a">購物車</a></div>
      <div class="top_right_3"><a href="#" class="top_right_3_a">收藏夾</a></div>
      <div class="top_right_4"><a href="#" class="top_right_4_a">商品分類</a></div>
      <div class="top_right_5"><a href="#" class="top_right_5_a">賣家中心</a></div>
      <div class="top_right_6"><a href="#" class="top_right_6_a">聯繫客服</a></div>
      <div class="top_right_7"><a href="#" class="top_right_7_a">網站導航</a></div>
    </div>
  </div>
</div>

<!-- logo + 搜尋 -->
<div class="head_main">
  <div class="w">
    <div class="head_main_img"><a href="#"><img src="img/logo.gif" alt="logo"></a></div>
    <div class="head_main_search">
      <div class="head_main_search_box">
        <div class="head_main_search_box_left"><a href="#" class="top_left_1_a">寶貝</a></div>
        <input type="search" class="head_main_search_box_input">
        <input type="button" class="head_main_search_box_button" value="搜索">
      </div>
      <div class="head_main_search_a"><a href="#">高級搜索</a></div>
      <div class="head_main_search_down">
        <a href="#">女士涼鞋</a><a href="#">男士T恤</a><a href="#">連衣裙</a>
        <a href="#" class="head_main_search_down_a">更多</a>
      </div>
    </div>
    <div class="head_main_right"><a href="#">手機淘寶</a><img src="img/code.png" alt=""></div>
  </div>
</div>

<!-- 分類 + 輪播 + 側欄 -->
<div class="center_top">
  <div class="w">
    <div class="center_top_left">
      <h5>商品服務分類</h5>
      <div class="center_top_left_ul">
        <ul>
          <li class="center_top_left_li_1"><h3>服裝內衣</h3></li>
          <li class="center_top_left_li_2"><h3>鞋包配飾</h3></li>
          <!-- 其餘 14 項同構,加上 li_3 ~ li_16 更換背景圖 -->
        </ul>
      </div>
    </div>
    <div class="center_top_right">
      <div class="center_top_right_head">
        <a href="#" class="center_top_right_head_a">天貓</a>
        <a href="#" class="center_top_right_head_a">聚劃算</a>
        <a href="#">拍賣</a>
      </div>
      <div class="center_top_right_left">
        <div class="center_top_right_left_lunbo1">
          <a href="#"><img src="img/1.jpg" alt=""></a>
          <div class="center_top_right_left_lunbo1_btn1">
            <span></span><span></span><span></span><span></span><span></span>
          </div>
        </div>
        <a href="#"><img src="img/6.6.jpg" alt=""></a>
      </div>
      <div class="center_top_right_right">
        <div class="center_top_right_right_top">
          <ul>
            <li class="li_1">公告</li><li>規則</li><li>論壇</li>
          </ul>
          <div class="center_top_right_right_top_a"><a href="#">公告標題一</a></div>
        </div>
        <div class="center_top_right_right_center">
          <div class="center_top_right_right_center_box">
            <img src="img/user-head.jpg" alt="" width="62" height="62">
            <div class="center_top_right_right_center_box_right">
              <h3>Hi 天之狼2011</h3>
              <a href="#" class="a_1">零淘金幣抵錢</a>
              <a href="#" class="a_2">會員俱樂部</a>
            </div>
          </div>
          <div class="center_top_right_right_center_down">
            <a href="#" class="a1">登錄</a><a href="#">免費注冊</a>
          </div>
        </div>
        <div class="center_top_right_right_down">
          <h3>便民服務</h3>
          <div><a href="#"><h5><img src="img/phone.png" alt=""></h5><h6>話費</h6></a></div>
          <!-- 遊戲、旅行、保險、彩票……同構 -->
        </div>
      </div>
    </div>
  </div>
</div>

<!-- 主題市場等四欄 -->
<div class="center_top2">
  <div class="w">
    <div class="center_top2_left center_top2_left1">
      <h3>主題市場</h3>
      <ul><li><a href="#">愛美女人</a></li><li><a href="#">品質男人</a></li></ul>
    </div>
    <!-- left2 特色購物、left3 當前熱點、left4 更多精彩 結構相同 -->
  </div>
</div>

<!-- 主體三欄 -->
<div class="main">
  <div class="w">
    <div class="main_left">
      <div class="main_left_top">
        <h1>萬能的淘寶</h1>
        <span>下面有 30 個特色市場等你來逛哦!</span>
      </div>
      <div class="main_left_box">
        <div class="main_left_center">
          <div><span class="s1">|</span><span class="s2">女人愛搭配</span></div>
          <div class="main_left_center_left">
            <img src="img/show1.png" alt="">
            <h1><a href="#">有到一年雪絨坊</a></h1>
            <span>優雅的溫柔</span>
          </div>
          <div class="main_left_center_right">
            <div class="main_left_center_right_top">
              <a href="#"><img src="img/shooes.jpg" alt=""><p>春夏皮鞋</p></a>
              <table>
                <tr><td><a href="#">潮流女裝</a></td><td><a href="#">小个子美搭</a></td></tr>
                <tr><td><a href="#">胖MM顯瘦</a></td><td><a href="#">復古多包袋</a></td></tr>
              </table>
            </div>
          </div>
        </div>
      </div>
      <!-- 重複 17 個 main_left_box -->
    </div>

    <div class="main_right">
      <div class="main_right_top">
        <div class="main_right_top_up">
          <h1><a href="#">我收藏的店鋪上新</a><span>更新 <span>3</span></span></h1>
          <div class="main_right_top_up_box">
            <a href="#"><img src="img/inner1.png" alt=""><p>¥118.00</p></a>
            <h3><a href="#">更多店鋪上新</a></h3>
          </div>
        </div>
        <h2 class="weitao">微淘動態 <a href="#">去廣場看看</a></h2>
        <div class="main_right_top_center">
          <h3>天貓美妝 <span>04-19 10:00</span></h3>
          <p>【美妝課堂】羽你相約 <a href="#">查看全文</a></p>
          <h6>評論(26)</h6>
        </div>
      </div>

      <div class="main_center">
        <div class="main_center_up">
          <h1>逛逛 - 精選</h1>
          <ul class="main_center_up_ul">
            <li><em>1</em><a href="#">85後小夫妻!3室2廳溫馨窩</a><span>jjwwlove99</span></li>
          </ul>
        </div>
      </div>
    </div>

    <div class="main_down">
      <div class="main_down_top">
        <h1>熱賣單品</h1>
        <div class="main_down_top_right"><a href="#">s5皮套</a>| <a href="#">更多</a></div>
      </div>
      <div class="main_down_goods">
        <a href="#"><img src="img/sing1.jpg" alt=""><h3>皺紋立領雪紡衫</h3></a>
        <p class="p1"><span class="red">¥129.00</span> <span class="green">包郵</span></p>
        <p class="p2"><del>399.00</del> | 月銷29件</p>
      </div>
      <!-- 重複 13 個商品卡片 -->
    </div>
  </div>
</div>

<div class="footer">
  <div class="w">
    <div class="footer_left">
      <div class="footer_left_1"><a href="#">關於淘寶</a>| <a href="#">合作夥伴</a></div>
      <p class="footer_left_3"><span>© 2014 Taobao.com 版權所有</span></p>
    </div>
    <div class="footer_right"><img src="img/tao-man.png" alt=""></div>
  </div>
</div>

</body>
</html>

CSS 實作要點

樣式檔我依區塊分段,這裡挑出幾個最值得學的技巧。

1. 全域重置與垂直對齊

@charset "UTF-8";
* { margin: 0; padding: 0; }
ul, ol { list-style: none; }
a { color: #6C6C6C; text-decoration: none; }
a:hover { color: red; }
/* 圖片是 inline 元素,會在底部留出約 3px 的空白縫隙 */
img { border: 0; vertical-align: middle; }

2. 固定定位的頂欄

.header {
    position: fixed; z-index: 1000; left: 0; top: 0;
    width: 100%; height: 35px;
    background-color: #F5F5F5; font-size: 12px;
    color: #6C6C6C; line-height: 35px;
}
.top_left { width: 30%; height: 35px; float: left; }
.top_right { width: 44%; height: 35px; float: right; }
/* 因為頂欄脫離文檔流,下方內容要推開對應高度 */
.head_main { height: 115px; margin-top: 45px; }

這裡有個務必注意的連動關係:position: fixed 會讓元素脫離文檔流,後續內容會往上貼,所以 .head_main 必須補上 margin-top 把空間撐回來。

3. 搜尋框的三段式組裝

.head_main_search_box {
    position: relative; float: left;
    margin-top: 10px; width: 664px; height: 40px;
    border: 3px solid #f50;          /* 用外框做出淘寶橘的邊框質感 */
}
.head_main_search_box_left {          /* 左側「寶貝」下拉區 */
    width: 72px; height: 40px; line-height: 40px;
    background: #f6f6f6; float: left;
    border-right: 1px solid #e5e5e5;
}
.head_main_search_box_input {         /* 中間輸入框 */
    float: left; width: 495px; height: 38px;
    padding-left: 10px; border: none; outline: none;
    /* 用多重背景同時放放大鏡與清除圖示 */
    background: url(../img/search.png) no-repeat 98% center,
                url(../img/s.png) no-repeat 2% center;
}
.head_main_search_box_button {        /* 右側搜尋鈕 */
    float: left; width: 95px; height: 40px;
    border: none; outline: none; font-size: 20px;
    letter-spacing: 5px;              /* 字距讓兩個字更舒展 */
    background: #f50; cursor: pointer; color: #fff;
}

多重背景(background 用逗號分層)是這裡最值得學的一招,能省掉額外的裝飾元素。

4. 用背景圖變化做出 16 個分類圖示

.center_top_left_li_1  { background: url("../img/001.png") no-repeat 10px center; }
.center_top_left_li_2  { background: url("../img/002.png") no-repeat 10px center; }
/* …… li_3 到 li_16 依此類推 */

與其為每個項目塞一張 <img>,用 CSS 背景圖讓 HTML 保持乾淨,是清單類圖示的標準做法。

5. 自製輪播指示器

.center_top_right_left_lunbo1 { position: relative; float: left; width: 520px; height: 280px; overflow: hidden; }
.center_top_right_left_lunbo1_btn1 {
    position: absolute; left: 50%; top: 250px; margin-left: -35px;
    width: 70px; height: 11px;
}
.center_top_right_left_lunbo1_btn1 span {
    display: inline-block; float: left;
    width: 9px; height: 9px; margin-right: 4px;
    border-radius: 9px; cursor: pointer; background-color: #cccccc;
}
.center_top_right_left_lunbo1_btn1 span:hover { background-color: #6C6C6C; }

left: 50% 搭配負的 margin-left 是水平居中的經典手法;border-radius 則把方塊變成圓點。要讓它真正能換圖,還需要一段 JS 控制 ul 的 left 位移,那是下一步的練習。

6. 商品牆與多欄排版

.main_left  { width: 790px; float: left; }
.main_right { width: 378px; float: right; overflow: hidden; }
.main_left_box {
    position: relative; float: left;
    width: 393px; height: 268px; border: 1px solid #eee;
}
.main_left_box:hover { border: 1px solid red; }   /* 懸停高亮 */
.main_down_goods { float: left; margin: 0 18px 30px 22px; width: 130px; height: 180px; }
.main_down_goods .green { display: inline-block; padding: 2px; background-color: #8BC144; color: white; }

做完之後,你會學到什麼

這個練習的價值不在「還原得像不像」,而在你被迫處理了這些真實問題:浮動元素的清除與塌陷、固定定位對文檔流的影響、多重背景的圖示化、用 class 命名管理大量重複元件、以及如何在 1200px 的固定版心下把三欄比例算對。

給新手的兩個建議:第一,不要一次寫完再除錯,做完一個通欄區塊就先在瀏覽器看一次;第二,class 命名要有語意,像 main_left_box 這種「位置 + 元件」的命名,遠比 box1、box2 好維護。之後若要進階,可以把這份靜態頁改造成 RWD(用百分比與 media query 取代固定寬度),或加上 JS 讓輪播與選單真正動起來。

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