一段div css代码, 让页面中的两个div左右排列。 如果不使用float应该如何实现
一段div css代码, 让页面中的两个div左右排列。 如果不使用float应该怎么实现?
页面里有一个container, 里面有两个div
<div id="container">
<div id="leftDiv"></div>
</div id="rightDiv"></div>
</div>
已知container的宽是800
让leftDiv宽为200, rightDiv宽为600 横向排列起来,请问要怎么写?
如果不使用float应该怎么实现?(我的页面比这个复杂很多,用了float后ie和firefox显示差异太大,我想知道有没有什么好的技巧能够代替float,所以提了这个问题)
------解决方案--------------------
1,页面再复杂,各种浏览器对float解释的还是不错的。
2,不用float的话,我唯一知道的就是绝对定位了。。。一侧用绝对定位,另外一侧用margin 赋值。
3,推荐还是用float,绝对定位会越搞越麻烦。
4,float的话,左右都要float,下面用clear清除一下,就不会有问题了。ie6 下的双倍边距用_display:inline解决。firefox下的清除,可以写#container:after{overflow:hidden;display:block,content:"";height:0;line-height:0;}就可以解决了。
------解决方案--------------------
几点额外的建议:
1. 不要认为你把container宽度设为800,leftDiv设为200,rightDiv设为600就能拥有“实诚”的布局。
实际上,有些宽度是你很容易忽略的,比如border,padding,margin,这些都是很容易忽略的,建议把leftDiv和rightDiv按百分比设定,并且加起来小雨100%。比如:leftDiv = 24%, rightDiv = 74%。
2. 不用一味地float:left; 不要忘记还有float:right。给rightDiv设定float:right有时会有意想不到的收获。
简单地,可以这样写:
页面里有一个container, 里面有两个div
<div id="container">
<div id="leftDiv"></div>
</div id="rightDiv"></div>
</div>
已知container的宽是800
让leftDiv宽为200, rightDiv宽为600 横向排列起来,请问要怎么写?
如果不使用float应该怎么实现?(我的页面比这个复杂很多,用了float后ie和firefox显示差异太大,我想知道有没有什么好的技巧能够代替float,所以提了这个问题)
------解决方案--------------------
1,页面再复杂,各种浏览器对float解释的还是不错的。
2,不用float的话,我唯一知道的就是绝对定位了。。。一侧用绝对定位,另外一侧用margin 赋值。
3,推荐还是用float,绝对定位会越搞越麻烦。
4,float的话,左右都要float,下面用clear清除一下,就不会有问题了。ie6 下的双倍边距用_display:inline解决。firefox下的清除,可以写#container:after{overflow:hidden;display:block,content:"";height:0;line-height:0;}就可以解决了。
------解决方案--------------------
几点额外的建议:
1. 不要认为你把container宽度设为800,leftDiv设为200,rightDiv设为600就能拥有“实诚”的布局。
实际上,有些宽度是你很容易忽略的,比如border,padding,margin,这些都是很容易忽略的,建议把leftDiv和rightDiv按百分比设定,并且加起来小雨100%。比如:leftDiv = 24%, rightDiv = 74%。
2. 不用一味地float:left; 不要忘记还有float:right。给rightDiv设定float:right有时会有意想不到的收获。
简单地,可以这样写:
- CSS code
<!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=gb_2312" /> <title>No Title</title> </head> <style> #container { width:800px; padding:0; margin:0 auto; height:100%;} #leftDiv { width:24%; float:left; text-align:center; line-height:600px; height:600px; display:block; background-color:#ccc; color:#f00;} #rightDiv { width:74%; float:right; text-align:center; line-height:600px; height:600px; display:block; background-color:#c00; color:#fff;} </style> <body> <div id="container"> <div id="leftDiv"> leftDiv </div> <div id="rightDiv"> rightDiv </div> </div> </body> </html>
------解决方案--------------------
float这里就不贴代码了,不用float的话,可以用定位来做,这种方法的优点是可以按DIV的顺序来决定优先显示的顺序,代码如下:
- HTML code
<!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" xml:lang="zh-CN" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <style type="text/css"> body { font-size:9pt; text-align:center; } #container { position:relative; margin:auto; width:800px; height:200px; } #leftDiv { position:absolute; top:0; left:0; width:200px; height:200px; background:#ddd; } #rightDiv { position:absolute; top:0; right:0; width:600px; height:200px; background:#eee; } </style> 1、先显示leftDiv <div id="container"> <div id="leftDiv">leftDiv</div> <div id="rightDiv">rightDiv</div> </div> 2、先显示rightDiv <div id="container"> <div id="rightDiv">rightDiv</div> <div id="leftDiv">leftDiv</div> </div> </body> </html>
------解决方案--------------------