從平面設計師那裡拿到的 PSD 圖檔,在重構時需要先對網頁美工圖進行分析,只有分析得透徹,才能做出好的 CSS 版面。因此 DIV CSS 版面在分析中佔了很大比重——我們分析美工圖並非看它好不好看,而是從版面結構出發,這一步直接影響後續重構是否順利。
版面知識:CSS 版面是指用 div 標籤搭配 CSS 代碼製作的 HTML 網頁的統稱。div+css 版面的好處是便於維護、有利於 SEO、開啟速度更快、符合標準。
製作前先思考版面:拿到一張美工圖後,先從上下、上中下、左右、上中(中再含左右)下等框架來思考。以列表頁為例,我們可以分析出它是「上、中、下」結構,中間又包含左右結構。因此撰寫 CSS 與 HTML 時,就遵循「從上到下、從外到內」的原則。
先建立一個 web 資料夾,在其中新增 index.html 與 index.css。小訣竅是:可以套用模板來建立初始頁面,再在模板基礎上添加 CSS。
index.html 的代碼:
<!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=gb2312" /> <title>css 版面案例頁面</title> <link href="index.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header">我是頭部(上)</div> <div id="centers"> <div class="c_left">我是中的左</div> <div class="c_right">我是中的右</div> <div class="clear"> </div> </div> <div id="footer">我是底部(下)</div> </body> </html>
index.css 如下:
body, div, address, blockquote, iframe, ul, ol, dl, dt, dd, li, dl, h1, h2, h3, h4, h5, h6, p, pre, table, caption, th, td, form, legend, fieldset, input, button, select, textarea {margin:0; padding:0; font-weight: normal;font-style: normal;font-size: 100%; font-family: inherit;}
ol, ul ,li{list-style: none;}
img {border: 0;}
body {color:#000;background:#FFF; text-align: center; font: 12px/1.5 Arial, Helvetica, sans-serif;}
.clear{clear:both;height:1px; margin-top:-1px; width:100%;}
/* 以下為新寫的版面框架代碼 */
#header ,#centers ,#footer{ width:100%; margin:0 auto; clear:both;font-size:18px; line-height:68px; font-weight:bold;}
#header{ height:68px; border:1px solid #CCCCCC; }
#centers{ padding:8px 0;}
#footer{ border-top:1px solid #CCCCCC; background:#F2F2F2;}
#centers .c_left{ float:left; width:230px; border:1px solid #00CC66; background:#F7F7F7; margin-right:5px; }
#centers .c_right{ float:left; width:500px;border:1px solid #00CC66; background:#F7F7F7}
把這兩段代碼新建出來試試,就能還原出美工頁面的 CSS 與 HTML 框架,再依各區塊內容繼續添加樣式。以上案例未逐一詳解,希望你能親自多實踐——只有實踐才能練好技術。