DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
團隊共用的 CSS 基礎樣式表:reset、字體、色彩與版面工具類
編輯:CSS詳解     

以下是我們開發團隊預設使用的一份基礎樣式表,攤在這裡供大家參考與補充。它的定位不是好看,而是「把地基鏟平」:先抹掉瀏覽器預設樣式的差異,再提供一組約定俗成的工具類,讓後續每個專案都能在同一套語彙上協作。

Reset:抹平瀏覽器差異

body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,p,blockquote,th,td{
   margin: 0;
   padding: 0;
}
table{ border-collapse: collapse; border-spacing: 0; }
fieldset, img{ border: 0; }
address,caption,cite,code,dfn,em,strong,th,var{ font-style: normal; font-weight: normal; }
ol,ul{ list-style: none; }
caption,th{ text-align: left; }
h1,h2,h3,h4,h5,h6{ font-size: 100%; }
q:before,q:after{ content: ''; }

這一段做四件事:清除元素外距與內距、合併表格邊框、拿掉圖片與表單的預設框線、把標題與強調元素的字級字重歸零。最後一行清空 q 標籤自動產生的引號,避免不同瀏覽器加的引號樣式不一致。

字體工具類

.f12{ font-size: 12px !important; }
.f14{ font-size: 14px !important; }
.bold{ font-weight: bold; }
.normal{ font-weight: normal; }
.font_none{ font-size: 0px; line-height: 0px; }

.font_none 常用於圖片替換文字時把原文藏掉,又不影響版面高度。

色彩工具類

.red, .red a{ color: #f00 !important; }
.gray, .gray a{ color: #666 !important; }
.green, .green a{ color: #76c56a !important; }
.orange, .orange a{ color: #fd864a !important; }

連 a 一起寫,是為了避免連結的預設藍色蓋掉工具類的顏色。

版面工具類

.block{ display: block; }
.hidden{ display: none; }
.bg_none{ background: none !important; }
.float_left{ float: left; }
.float_right{ float: right; }
.clear{ clear: both !important; }
.margin_10{ margin: 10px; }
.margin_20{ margin: 20px; }
.margin_top{ margin-top: 10px; }
.margin_right{ margin-right: 10px; }
.margin_bottom{ margin-bottom: 10px; }
.margin_left{ margin-left: 10px; }
.padding_10{ padding: 10px; }
.padding_20{ padding: 20px; }
.padding_top{ padding-top: 10px; }
.padding_right{ padding-right: 10px; }
.padding_bottom{ padding-bottom: 10px; }
.padding_left{ padding-left: 10px; }
.nopadding{ padding: 0 !important; }

另一套思維:固定寬度的骨架

除了工具類路線,團隊裡也有另一種起手式,直接定義頁面骨架:

* { margin: 0; padding: 0; }
html { text-align: center; background: #fff; }
body {
   width: 960px;
   position: relative;
   margin: 0 auto;
   padding: 10px 0;
   text-align: left;
   font: 12px/1.6em Tahoma, Verdana, Lucida, Arial, Helvetica, 宋體, sans-serif;
   color: #666;
}
h1, h2, h3, h4, h5, h6 { font-size: 100%; }
address, caption, cite, code, dfn, em, strong, th, var, b, i { font-style: normal; font-weight: normal; }
table { border-collapse: collapse; border-spacing: 0; empty-cells: show; }
ol, ul { list-style: none outside; }
:focus { outline: 0; }
fieldset, img { border: 0; }
caption, th { text-align: left; }
q:before, q:after { content: ''; }
a { text-decoration: none; }
a:hover { text-decoration: underline; }
input, select, textarea { font-size: 12px; padding: 2px; }
.clearfix { overflow: hidden; zoom: 1; }

這裡有兩個值得留意的細節:html 設 text-align: center、body 再設回 left 並搭配 margin: 0 auto,是為了兼顧舊 IE 與標準瀏覽器的置中行為;.clearfix 用 overflow: hidden 搭配 zoom: 1,則是同時照顧標準瀏覽器的 BFC 與 IE 的 hasLayout。

使用建議

基礎樣式表的價值在於「全隊一致」,不在於「無敵完整」。工具類與其多而濫,不如少而穩;一旦約定,就不要任意更動命名,否則每個專案都得跟著改。若你的團隊已有慣用寫法,也歡迎在此基礎上補齊,重點是把共識寫下來。

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