MozTW 討論區
https://forum.moztw.org/

設計內部 DIV 要空行, 使用 ie 與 firefox, 有margin-top設定, 但 Firefox 確沒空行
https://forum.moztw.org/viewtopic.php?f=13&t=31068
1 頁 (共 1 頁)

發表人:  isnick [ 2010-09-21, 19:19 ]
文章主題 :  設計內部 DIV 要空行, 使用 ie 與 firefox, 有margin-top設定, 但 Firefox 確沒空行

內部 div #header 有設定 margin-top:5px; 與外部 div #main_wrapper 要空 5px, ie 結果正常, firefox 不正常.

html:

<head>

<!-- Meta Tags -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>test1</title>

<!-- CSS -->
<!-- 畫面版面設計樣式表 -->
<link href="css/layouts/style_layout_test01.css" rel="stylesheet" media="all" />
<link href="css/texts/style_text_test01.css" rel="stylesheet" media="all" />

</head>
<body>
<div id="main_wrapper">
<div id="header">AA
</div><!-- header -->
</div><!-- main_wrapper -->
</body>
</html>

css用來定 layout:
body {
text-align:center; /* IE6 needs this to center the layout in the browser window */
}

#main_wrapper {
width:940px; /* widths of columns will change proportionately as this width is changed */
margin-left:auto; /* centers layout in browser */
margin-right:auto; /* centers layout in browser */
text-align:left; /* resets the centering hack for IE6 on the body tag */
}

css用來定 顏色:
body {
background-color:#FF0000;
margin:0px;
}

/* 主封底 */
#main_wrapper{
background-color:#FFFF00;
height:60px;

}

/* 頁首 */
#header{
background-color:#FF00FF;
height:50px;
margin-top:5px;
margin-left:5px;
margin-right:5px;
margin-bottom:5px;
}

附加檔案:
檔案註釋: ie 與 ff 不同.
err.JPG [84.72 KiB]
被下載 410 次

發表人:  isnick [ 2010-09-22, 11:46 ]
文章主題 :  Re: 設計內部 DIV 要空行, 使用 ie 與 firefox, 有margin-top設定, 但 Firefox

要內部div與外部div空行, 今天看了書, 加了position:relative; top:5px;, 刪掉 margin-top:5px;
就可以了, 請問這樣的做法 ok ? 所以內部的 div 的定位要用position:relative, 沒辦法用 margin-top:5px; ?? 謝謝.

/* 頁首 */
#header{
height:50px;
position:relative; top:5px;
margin-left:5px;
margin-right:5px;
margin-bottom:5px;
}

發表人:  訪客 [ 2010-09-22, 23:24 ]
文章主題 :  Re: 設計內部 DIV 要空行, 使用 ie 與 firefox, 有margin-top設定, 但 Firefox

簡單點的做法:在#main_wrapper設定padding(padding-bottom)

發表人:  風痕影 [ 2010-10-13, 04:00 ]
文章主題 :  Re: 設計內部 DIV 要空行, 使用 ie 與 firefox, 有margin-top設定, 但 Firefox

我本身在排這樣的排版時,也是用樓上說的辦法,在 #main_wrapper 使用 padding
#header 本身就不用設定任何 margin 了

不過我也疑惑為何 Firefox 與 Chrome 都會把 margin-top 吃掉
所以剛才跑去查了 W3C 的網頁標準文件,發覺造成這種情形是有條件的:

#main_wrapper 必須有 border-top 或 padding-top
瀏覽器才不會把 margin-top 吃掉

發表人:  訪客 [ 2010-10-13, 11:05 ]
文章主題 :  Re: 設計內部 DIV 要空行, 使用 ie 與 firefox, 有margin-top設定, 但 Firefox

謝謝樓上兩位的建議, 我都試了.

1.在 main_wrapper 加入了 border-top, 是可以了, 但一要有數值不是能是 "border-top:0pt", 是0pt 效果就不對了, 但是加入這樣等於強迫了之間一定會空大於零的pt, 版面會受影響.

2.在 main_wrapper 加入了 "padding-bottom: 5px", 好像沒有效.

3.在 main_wrapper 加入了 "padding-top: 5px", 可以了, 內部的div可以正常, 但就是數值不能為零. 如果外部div與內部div不要空, 這個方法就不行了.

發表人:  風痕影 [ 2010-10-13, 13:57 ]
文章主題 :  Re: 設計內部 DIV 要空行, 使用 ie 與 firefox, 有margin-top設定, 但 Firefox

訪客 寫:
謝謝樓上兩位的建議, 我都試了.

1.在 main_wrapper 加入了 border-top, 是可以了, 但一要有數值不是能是 "border-top:0pt", 是0pt 效果就不對了, 但是加入這樣等於強迫了之間一定會空大於零的pt, 版面會受影響.

2.在 main_wrapper 加入了 "padding-bottom: 5px", 好像沒有效.

3.在 main_wrapper 加入了 "padding-top: 5px", 可以了, 內部的div可以正常, 但就是數值不能為零. 如果外部div與內部div不要空, 這個方法就不行了.


抱歉沒說清楚,確實數值必須大於零才可以

然後,另一位訪客的辦法其實是可以解決你的問題的
你可以把 #header 的四個 margin 全都去掉
而在 #main_wrapper 設定四個 padding,這樣就能解決了

當然也可以只加 padding-top: 5px;,但與此同時你要記得把 margin-top: 5px; 去掉
這個辦法是用 padding 取代 margin

順帶一提,其實四邊 margin 數值一樣的時候,可以只寫成一行 margin: 5px;
padding 也可以這樣做 =)

1 頁 (共 1 頁) 所有顯示的時間為 UTC + 8 小時
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/