DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> WEB網站前端 >> 前端技巧
我的 CSS 基礎框架 base.css:一次搞定跨瀏覽器樣式重置
編輯:前端技巧     

每個瀏覽器都帶著自己的預設樣式出廠,h1 的粗細、清單的縮排、表單元素的間距各不相同。若不先抹平這些差異,後面的樣式就得靠一堆 hack 補洞。base.css 的任務就是「歸零」。

@charset "utf-8";
/*
@名稱: base
@功能: 重設瀏覽器預設樣式
*/

/* 防止使用者自訂背景色影響網頁,同時允許自訂字體 */
html {
  color: black;
  background: white;
}

/* 內外邊距是各瀏覽器落差最大的地方 */
body, div, dl, dt, dd, ul, ol, li,
h1, h2, h3, h4, h5, h6, pre, code, form,
fieldset, legend, input, textarea, p, blockquote,
th, td, hr, button, article, aside, details,
figcaption, figure, footer, header, hgroup,
menu, nav, section {
  margin: 0;
  padding: 0;
}

/* 表單元素不會繼承父層 font,必須另外指定 */
body, button, input, select, textarea {
  font: 12px SimSun, tahoma, arial, sans-serif;
}
input, select, textarea {
  font-size: 100%;
}

/* 讓表格邊框合併,消除 cell 間距 */
table {
  border-collapse: collapse;
  border-spacing: 0;
}

/* IE bug: th 不繼承 text-align */
th {
  text-align: inherit;
}

fieldset, img { border: none; }

/* 修正 IE6/7/8  quirks mode 下 iframe 的行內表現 */
iframe { display: block; }

/* 移除 Firefox 下 abbr 的虛線邊框 */
abbr, acronym {
  border: none;
  font-variant: normal;
}

del { text-decoration: line-through; }

address, caption, cite, code, dfn, em, th, var {
  font-style: normal;
  font-weight: 500;
}

/* 移除清單符號,li 會繼承 */
ol, ul { list-style: none; }

/* 對齊是排版的基礎,不要讓所有東西預設居中 */
caption, th { text-align: left; }

/* 標題統一交給使用者自訂 */
h1, h2, h3, h4, h5, h6 {
  font-size: 100%;
  font-weight: 500;
}

q:before, q:after { content: ''; }

/* 統一上標下標 */
sub, sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}
sup { top: -0.5em; }
sub { bottom: -0.25em; }

/* 只在 hover 時顯示底線 */
a:hover { text-decoration: underline; }
ins, a { text-decoration: none; }

/* 移除 IE6/7 的焦點虛線框 */
a:focus, *:focus { outline: none; }

/* 清除浮動 */
.clearfix:before, .clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
  overflow: hidden;
}
.clearfix { zoom: 1; /* IE6/7 */ }

.clear {
  clear: both;
  display: block;
  overflow: hidden;
  height: 0;
  line-height: 0;
  font-size: 0;
}

/* 顯示與隱藏,常與 JS 搭配 */
.hide {
  display: none !important;
  visibility: hidden;
}
.block { display: block !important; }

/* 行內化浮動,可避開 IE6 的雙倍 margin bug */
.fl { float: left;  display: inline; }
.fr { float: right; display: inline; }

幾個設計取捨值得說明

html 上寫死黑字白底,是為了讓使用者自訂色彩時有明確對照,同時不干擾字型偏好。

表單元素單獨指定 font,因為 input、select、textarea、button 在多数瀏覽器中不會繼承 body 的字體設定,這是最常被忽略的一條。

標題字級全部歸 100%,把視覺層級的決定權交回給具體的頁面樣式,而不是依賴瀏覽器預設的 h1 比 h2 大多少。

.fl / .fr 加上 display:inline,這條是專為 IE6 的雙倍邊距問題準備的,對現代瀏覽器無害,可以安心保留。

base.css 只是起點,不是終點。真正上線前建議再補上 box-sizing、圖片 max-width、以及無障礙相關的焦點樣式。

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