最經常使用的CSS技巧收集筆記

 

1.重置瀏覽器的字體大小 php

重置瀏覽器的默認值 ,而後重設瀏覽器的字體大小你可使用雅虎的用戶界面重置的CSS方案 ,若是你不想下載9MB的文件,代碼以下:html

複製代碼
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,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%; font-weight:normal; } 
q:before,q:after { content:; } 
abbr,acronym { border:0; }
複製代碼

其次,咱們重設瀏覽器字體的大小爲10像素,使用以下: 瀏覽器

html {font-size: 62.5%;}post

這個大小基本合適,而後您能夠根據本身的須要調整大小,如 標題1爲120像素:
h1 {font-size: 2em;} 字體


2.設置水平居中
大多數的網站目前都是固定寬度的。CSS代碼以下:
div#container {margin: 0 auto;}網站

3.控制位置:絕對位置,相對位置
假若有兩個div google

<div id='parent'> 
<div id='son'></div> 
</div>

div有left和top屬性,是用來定位的. spa

若是內層的div的position屬性是absolute.那他就是相對於文檔的左上角的位置..
若是內層的div(id爲son的那個)position屬性爲relative,那它的left和top值就是相對於外層的div的左上角的距離. .net


4.將重要元素放置在屏幕中央
若是你但願將您想要的東西放在最中央,可使用如下CSS: 設計

div.popup { height:400px; width:500px; position: absolute; top: 50%; left: 50%;} 
div.popup { margin-top: -200px; margin-left: -250px;}

您必須明確的指定寬度和高度,再把top和left屬性設爲他們的一半,這樣就能夠是這個部分回到屏幕的中心。

5.能夠重複利用的規則

.left {float: left;} 
.right {float: right;} 
img .left { border:2px solid #aaaaaa; margin: 0 10px 0 0;} 
img .right { border:2px solid #aaaaaa; margin: 0 0 0 10px; padding: 1px;}

設置本身的CSS樣式表,就能夠在您須要的時候直接的添加標記便可。 

6. 解決IE6 的浮動元素的雙倍邊距問題
對一個div設置了float:left 和 margin-left:100px 那麼在IE6中,這個bug就會出現。您只須要多設置一個display便可,代碼以下:
div {float:left;margin:40px;display:inline;} 

7.簡單的導航菜單
在您的設計中預設一個導航欄是很是有益的。可讓別人對你網頁的主要內容有一個大體的瞭解。第一次來的XHTML:

複製代碼
<div id=」navbar」> 
<ul> 
<li><a href=」http://www.peakflowdesign.com」>Peakflow Design</a></li> 
<li><a href=」http://www.google.com」">Google</a></li> 
<li><a href=」http://zenhabits.net/」>Zen Habits</a></li> 
</ul> 
</div>
複製代碼

CSS代碼: 

#navbar ul li {display:inline;margin:0 10px 0 0;} 
#navbar ul li a {color: #333;display:block;float:left;padding:5px;} 
#navbar ul li a:hover {background:#eee;color:black;}

8.不使用table的form表單 

正如咱們如今進行網站設計的table-free,把重點是放在使用DIVs上。再也不對錶的列和域進行約束,因此咱們須要一些好用的CSS,在JeddHowden.com 發現
XHTML:

複製代碼
<form action=」form.php」 method=」post」> 
<fieldset> 
<legend>Personal Information</legend> 
<div> 
<label for=」first_name」>First Name:</label> 
<input type=」text」 name=」first_name」 id=」first_name」 size=」10″ value=」" /> 
</div> 
<div> 
<label for=」last_name」>Last Name:</label> 
<input type=」text」 name=」last_name」 id=」last_name」 size=」10″ value=」" /> 
</div> 
<div> 
<label for=」postal」>Zip/Postal Code:</label> 
<input type=」text」 name=」postal」 id=」postal」 size=」10″ value=」" /> 
</div> 
</fieldset> 
</form>
複製代碼

CSS: 

form div {clear:left;display:block;width:400px;zoom:1;margin:5px 0 0 0;padding:1px 3px;} 
form div label {display:block;float:left;width:130px;padding:3px 5px;margin: 0 0 5px 0;text-align:right;}

9.讓footer老是停留在頁面的底部 

在網頁的底部老是保留着公司的版本信息,如何是這部分信息來實現呢?這是一個很古老的技術,這都要歸功於The Man in Blue 。
XHTML:

複製代碼
<body> 
<div id=」nonFooter」> 
<div id=」content」> *Place all page content here* </div> 
</div> 
<div id=」footer」> *Place anything you want in your footer here* 
</div> 
</body> 
複製代碼

CSS: 

html, body { height: 100%; } 
#nonFooter { position: relative; min-height: 100%; } 
* html #nonFooter { height: 100%; } 
#content { padding-bottom: 9em; } 
#footer { position: relative; margin-top: -7.5em; }

10.在同一元素上使用多種類 

隨着有用的功能愈來愈多的,大多數的人都忽略了內部CSS的選擇。一個元素能夠套用不少的類,例如:
.red {color: red;}
.bold {font-weight: strong;}

咱們能夠運用它:
<p class=」red bold」>This text will be red yet also bold!</p>

但願這些能對您有所幫助!

相關文章
相關標籤/搜索