CSS單位和CSS默認值大全

這兩天我對css做了較爲深入的研究,更體會到了css的強大,由於我不是美工,所以我主要深入其中原理和一些常用的CSS。

CSS默認值

em
相對長度單位。相對於當前對象內文本的字體尺寸。
如當前對行內文本的字體尺寸未被人爲設置,則相對於瀏覽器的默認字體尺寸。

ex
相對長度單位。相對於字符“x”的高度。此高度通常爲字體尺寸的一半。
如當前對行內文本的字體尺寸未被人爲設置,則相對於瀏覽器的默認字體尺寸。

px
像素(Pixel)。相對長度單位。
像素是相對於顯示器屏幕分辨率而言的。譬如,WONDOWS的用戶所使用的分辨率一般是96像素/英寸。而MAC的用戶所使用的分辨率一般是72像素/英寸。

pt
點(Point)。絕對長度單位。

em vs px

em指字體高,任意瀏覽器的默認字體高都是16px。所以未經調整的瀏覽器都符合: 1em=16px。那麼12px=0.75em, 10px=0.625em。爲了簡化font-size的換算,需要在css中的body選擇器中聲明Font-size=62.5%,這就使em值變爲16px*62.5%=10px, 這樣12px=1.2em, 10px=1em, 也就是說只需要將你的原來的px數值除以10,然後換上em作爲單位就行了。

em有如下特點:
1. em的值並不是固定的;
2. em會繼承父級元素的字體大小。

長度單位優化方法:

1. body選擇器中聲明Font-size=62.5%;

2. 將你的原來的px數值除以10,然後換上em作爲單位;

簡單吧,如果只需要以上兩步就能解決問題的話,可能就沒人用px了。經過以上兩步,你會發現你的網站字體大得出乎想象。因爲em的值不固定,又會繼承父級元素的大小,你可能會在content這個div裏把字體大小設爲1.2em, 也就是12px。然後你又把選擇器p的字體大小也設爲1.2em,但如果p屬於content的子級的話,p的字體大小就不是12px,而是1.2em=1.2 * 12px=14.4px。這是因爲content的字體大小被設爲1.2em,這個em值繼承其父級元素body的大小,也就是16px * 62.5% * 1.2=12px, 而p作爲其子級,em則繼承content的字體高,也就是12px。所以p的1.2em就不再是12px,而是14.4px。

3. 重新計算那些被放大的字體的em數值。避免字體大小的重複聲明,也就是避免以上提到的1.2 * 1.2= 1.44的現象。比如說你在#content中聲明瞭字體大小爲1.2em,那麼在聲明p的字體大小時就只能是1em,而不是1.2em, 因爲此em非彼em,它因繼承#content的字體高而變爲了1em=12px。

4.以上方法得到的12px(1.2em)大小的漢字在IE中並不等於直接用12px定義的字體大小,而是稍大一點。解決方法:只需在body選擇器中把62.5%換成63%就能正常顯示了。

HTML的CSS默認值

view plaincopy to clipboardprint?
html, address,   
blockquote,   
body, dd, div,   
dl, dt, fieldset, form,   
frame, frameset,   
h1, h2, h3, h4,   
h5, h6, noframes,   
ol, p, ul, center,   
dir, hr, menu, pre   { display: block }   
li              { display: list-item }   
head            { display: none }   
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 }   
body            { margin: 8px; line-height: 1.12 }   
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 }   
blockquote      { margin-left: 40px; margin-right: 40px }   
i, cite, em,   
var, address    { font-style: italic }   
pre, tt, code,   
kbd, samp       { font-family: monospace }   
pre             { white-space: pre }   
button, textarea,   
input, object,    
select          { display:inline-block; }   
big             { font-size: 1.17em }   
small, sub, sup { font-size: .83em }   
sub             { vertical-align: sub }    
sup             { vertical-align: super }   
table           { border-spacing: 2px; }   
thead, tbody,   
tfoot           { vertical-align: middle }   
td, th          { vertical-align: inherit }   
s, strike, del  { text-decoration: line-through }   
hr              { border: 1px inset }   
ol, ul, dir,   
menu, dd        { margin-left: 40px }   
ol              { list-style-type: decimal }   
ol ul, ul ol,   
ul ul, ol ol    { margin-top: 0; margin-bottom: 0 }   
u, ins          { text-decoration: underline }   
br:before       { content: "/A" }   
:before, :after { white-space: pre-line }    
 
center          { text-align: center }   
abbr, acronym   { font-variant: small-caps; letter-spacing: 0.1em }   
:link, :visited { text-decoration: underline }   
:focus          { outline: thin dotted invert }   
 
/* Begin bidirectionality settings (do not change) */   
BDO[DIR="ltr"]  { direction: ltr; unicode-bidi: bidi-override }   
BDO[DIR="rtl"]  { direction: rtl; unicode-bidi: bidi-override }   
 
*[DIR="ltr"]    { direction: ltr; unicode-bidi: embed }   
*[DIR="rtl"]    { direction: rtl; unicode-bidi: embed }   
 
@media print {   
  h1            { page-break-before: always }   
  h1, h2, h3,   
  h4, h5, h6    { page-break-after: avoid }   
  ul, ol, dl    { page-break-before: avoid } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章