html中的大部分的標籤都有一些糟糕的樣式,有的是標籤自然自帶的,有的是瀏覽器默認設置的,咱們在寫網頁時,這些默認的樣式就會時不時的跳出來搗一下亂,搞得咱們非常無奈。因此成手在寫css樣式時,通常都會在開頭寫一段初始化程序,來去掉這些默認樣式,好比最簡單的方法就是使用*{margin:0;padding:0;}。可是一般咱們不多使用這種方法,由於通配符*的效率極低。那咱們應該使用哪一種方法呢,下面博主就來爲你們介紹一些經常使用的方法。css
在介紹初始化程序以前,咱們先了解一下都有那些標籤被設置了哪些默認樣式,只有瞭解清楚了這些,咱們才能更好的對標籤樣式初始化。html
//塊級元素 html, body, div,ol, p, ul, h1, h2,h3,h4,h5, h6, address,blockquote, form, dd,dl, dt, fieldset, frame, frameset,noframes,center, dir, hr, menu, pre { display: block } //列表元素類 li{ display:list-item } ol{list-style-type: decimal } ol ul, ul ol,ul ul, ol ol { margin-top: 0; margin-bottom: 0 } ol, ul{ margin-left: 40px } //預格式文本類 i, cite, em,var, address{ font-style: italic } big{ font-size:1.17em } small, sub, sup{ font-size: .83em } sub{ vertical-align:sub } sup{ vertical-align:super } s, strike, del{ text-decoration: line-through } u, ins{ text-decoration:underline } //標題類 h1{ font-size:2em; margin: .67em 0 } h2{ font-size:1.5em; margin: .75em 0 } h3{ font-size:1.17em; margin: .83em 0 } h4, p,blockquote, ul,fieldset, form,ol, dl, dir,menu { margin: 1.12em 0} h5 { font-size:.83em; margin: 1.5em 0 } h6{ font-size:.75em; margin: 1.67em 0 } h1, h2, h3, h4,h5, h6, b,strong { font-weight: bolder } //僞類 br:before{ content: 」\A」 } :before, :after{ white-space: pre-line } :link, :visited { text-decoration: underline } :focus{ outline: thin dotted invert } //表格類 table{ display: table } tr{ display:table-row } thead{ display:table-header-group } tbody{ display:table-row-group } tfoot{ display:table-footer-group } col{ display:table-column } colgroup{ display:table-column-group } td, th{ display: table-cell;} caption{ display: table-caption } th{font-weight: bolder; text-align: center } caption{ text-align: center } table{ border-spacing: 2px;} thead, tbody,tfoot { vertical-align:middle } td, th { vertical-align:inherit } //其它元素 head{ display: none } body{ margin: 8px;line-height: 1.12 } button, textarea,input, object,select { display:inline-block;} blockquote{ margin-left: 40px;margin-right: 40px } pre, tt, code,kbd, samp { font-family: monospace } pre{ white-space: pre} hr{ border: 1px inset } center{ text-align: center } abbr, acronym{ font-variant: small-caps; letter-spacing:0.1em } BDO[DIR="ltr"] { direction: ltr; unicode-bidi:bidi-override } BDO[DIR="rtl"] { direction: rtl; unicode-bidi:bidi-override } /*定義BDO元素當其屬性爲DIR="ltr/rtl"時的默認文本讀寫顯示順序*/ *[DIR="ltr"]{ direction: ltr;unicode-bidi: embed } *[DIR="rtl"] { direction: rtl;unicode-bidi: embed } /*定義任何元素當其屬性爲DIR="rtl/rtl"時的默認文本讀寫顯示順序*/ @media print { h1{page-break-before: always } h1, h2, h3,h4, h5, h6 { page-break-after: avoid } ul, ol, dl{ page-break-before: avoid } } /*定義標題和列表默認的打印樣式*/
不一樣的瀏覽器默認的樣式可能不盡相同,因此開發時的第一件事可能就是如何把它們統一。若是沒對CSS初始化每每會出現瀏覽器之間的頁面差別。瀏覽器
1.頁邊距ide
IE默認爲10px,經過body的margin
屬性設置
FF默認爲8px,經過body的padding
屬性設置spa
2.段間距code
IE默認爲19px,經過p的margin-top
屬性設置
FF默認爲1.12em,經過p的margin-bottom
屬性設orm
3.標題樣式htm
h1~h6默認加粗顯示:font-weight:bold;
。
默認大小請參上表
還有是這樣的寫的blog
h1 {font-size:xx-large;}
h2 {font-size:x-large;}
h3 {font-size:large;}
h4 {font-size:medium;}
h5 {font-size:small;}
h6 {font-size:x-small;}
4.列表樣式
IE默認爲40px,經過ul、ol的margin屬性設置
FF默認爲40px,經過ul、ol的padding屬性設置
dl無縮進,但起內部的說明元素dd默認縮進40px,而名稱元素dt沒有縮進。圖片
5.元素居中
IE默認爲text-align:center;
FF默認爲margin-left:auto;margin-right:auto;
6.超連接<a>樣式
a 樣式默認帶有下劃線,顯示顏色爲藍色,被訪問過的超連接變紫色
7.鼠標樣式
IE默認爲cursor:hand;
FF默認爲cursor:pointer;
該聲明在IE中也有效
8.圖片連接樣式
IE默認爲紫色2px的邊框線
FF默認爲藍色2px的邊框線
博主總結了一些初始化的代碼,博友們能夠參考一下,具體使用時能夠適當地刪減一些。
@charset "UTF-8";
/*css 初始化 */
html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, img {
margin: 0;
padding: 0;
}
fieldset, img, input, button {
border: none;
padding: 0;
margin: 0;
outline-style: none;
}
ul, ol {
list-style: none;
}
input {
padding-top: 0;
padding-bottom: 0;
font-family: "SimSun", "宋體";
}
select, input {
vertical-align: middle;
}
select, input, textarea {
font-size: 12px;
margin: 0;
}
textarea {
resize: none;
}
/*防止拖動*/
img {
border: 0;
vertical-align: middle;
}
/* 去掉圖片低測默認的3像素空白縫隙*/
table {
border-collapse: collapse;
}
body {
font: 12px/150% Arial, Verdana, "\5b8b\4f53";
color: #666;
background: #fff
}
h1, h2, h3, h4, h5, h6 {
text-decoration: none;
font-weight: normal;
font-size: 100%;
}
s, i, em {
font-style: normal;
text-decoration: none;
}