CSS教程5 Margin和Padding

margin和padding用來隔開元素,margin是隔開元素與外邊,padding是隔開元素裏邊。

  例子h2:

Example Source Code [www.52css.com]
h2 {
font-size: 1.5em;
background-color: #ccc;
margin: 1em;
padding: 3em;
}
  元素四邊可以設置的屬性:margin-top, margin-right, margin-bottom, margin-left, padding-top, padding-right, padding-bottom and padding-left

CSS盒模型

width和height定義的是Content部分的寬度和高度,padding border margin的寬度依次加在外面。背景會填充padding和content部分。但是由於瀏覽器設計上的問題,不同瀏覽器顯示效果會有些不同。左右Margin加倍的問題當box爲float時,IE6中box左右的margin會加倍

W3C定義的盒模式如下:


  width和height定義的是Content部分的寬度和高度,padding border margin的寬度依次加在外面。背景會填充padding和content部分。
  但是由於瀏覽器設計上的問題,不同瀏覽器顯示效果會有些不同。

左右Margin加倍的問題
  當box爲float時,IE6中box左右的margin會加倍。比如:

Example Source Code [www.52css.com]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>www.52css.com</title>
<style>
.outer {
width:500px;
height:200px;
background:#000;
}
.inner {
float:left;
width:200px;
height:100px;
margin:5px;
background:#fff;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
</div>
</body>
</html>
  左面的inner的左面margin明顯大於5px。
  這時候,定義inner的display屬性爲inline。

外層box自動計算高度的問題
  根據W3C定義,沒有float屬性的外層box不會自動計算高度,要計算高度,必須在內層最後一個box加入clear:both。
  Opera、netscape、mozilla等不會計算外層box高度,但是微軟ie6會自動計算外層高度。比如:

Example Source Code [www.52css.com]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>www.52css.com</title>
<style>
.outer {
width:600px;
background:#000;
}
.inner1 {
float:left;
width:200px;
height:100px;
margin:5px;
background:red;
}
.inner2 {
float:left;
width:200px;
height:100px;
margin:5px;
background:yellow;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner1"></div>
<div class="inner2"></div>
</div>
</body>
</html>
  上面的代碼在ie中有黑色的背景,但是沒有正確的計算上下的margin,在inner2下面加上一個包含clear:both屬性的div後,可以正確計算margin。但是firefox中仍然沒有黑色背景,通常的解決辦法是定義一下clear:both這個div的高度,或者插入全角空格,這樣就必須增加額外的高度。網上一種比較好的解決辦法是在外層div中加入overflow屬性,同時使用clear:both,這樣就不會增加額外的高度了。如下:

Example Source Code [www.52css.com]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>www.52css.com</title>
<style>
.outer {
width:600px;
background:#000;
overflow:auto;
}
.inner1 {
display:inline;
float:left;
width:200px;
height:100px;
margin:5px;
background:red;
}
.inner2 {
display:inline;
float:left;
width:200px;
height:100px;
margin:5px;
background:yellow;
}
.clear {
clear:both;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner1"></div>
<div class="inner2"></div>
<div class="clear"></div>
</div>
</body>
</html>
  因此,外層css要定義overflow屬性,內層最後要加上clear屬性。

居中問題
  需要定義元素的寬,並且定義橫向的margin,如果你的佈局包含在一個層(容器)中,就象這樣:
  你可以這樣定義使它橫向居中:

Example Source Code [www.52css.com]
#wrap {
width:760px; /* 修改爲你的層的寬度 */
margin:0 auto;
}
  但是IE5/Win不能正確顯示這個定義,我們採用一個非常有用的技巧來解決:在外層用text-align屬性。就象這樣:

Example Source Code [www.52css.com]
#outer {
text-align:center;
}
#wrap {
width:760px; /* 修改爲你的層的寬度 */
margin:0 auto;
text-align:left;
}
  第一個#outer的text-align:center; 規則定義IE5/Win中#outer的所有元素居中(其他瀏覽器只是將文字居中) ,第二個text-align:left;是將#warp中的文字居左。
  因此,在有居中元素的css中,外層css要定義text-align:center屬性,內層居中用margin:x auto x auto定義,並重新定義text-align。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章