Home  >  Article  >  Web Front-end  >  A piece of div css code to arrange the two divs on the page left and right. How to implement it without using float? _html/css_WEB-ITnose

A piece of div css code to arrange the two divs on the page left and right. How to implement it without using float? _html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:08:312097browse

页面里有一个container, 里面有两个div
e0e6b80c2b871989cf94b51f0a97caf2
827ffefe6029f55daaae0f96acea492016b28748ea4df4d9c2150843fecfba68
e2725cf7f2dfd6322cb43961d0c2960316b28748ea4df4d9c2150843fecfba68
16b28748ea4df4d9c2150843fecfba68
已知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有时会有意想不到的收获。

简单地,可以这样写:

<!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>


table
布局格式已经固定的话 最外面直接包TABLE一行2列 何必写这么多CSS搞自己头晕 而且里面2个DIV是独立的以后修改也不麻烦

display:inline吧

100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
cefba791f019827b23c6d5357c0fcbfb16b28748ea4df4d9c2150843fecfba68
cefba791f019827b23c6d5357c0fcbfb16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

float这里就不贴代码了,不用float的话,可以用定位来做,这种方法的优点是可以按DIV的顺序来决定优先显示的顺序,代码如下:

<!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>

说白了就是如果不想用float定位,那么就请使用position:absolute吧
使用时请注意在父层div中设定position:relative
然后left和right两个子层position:absolute

多说一句,子层这时候会脱离文本流,但是如果想让此div中数据能够自动撑开div,那么请在该层最下面添加一个清除浮动的元素如 519e184c3e3c9e9d2446fd6b8ee4047116b28748ea4df4d9c2150843fecfba68

float这里就不贴代码了,不用float的话,可以用定位来做,这种方法的优点是可以按DIV的顺序来决定优先显示的顺序,代码如下:
HTML codeb585974ae3b7dba3039af53a9593f9c4a7d4c161155ae32f950812f79f3ce2e093f0f5c25f18dab9d176bd4f6de5d30ed570094f1f1c89fec1c07886e66d28669c3bca370b5104690d9ef395f2c5f8d16c04bd5ca3fcae76e30b72ad730ca86ddcdb7717c62c7708d2cdd643cd0a399b
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;}531ac245ce3e4fe3d50054a55f265927

1、先显示leftDiv86542c4a784326f7f6d9d0c158d9660aa6aee891db45e2fb1ee617e085d7f039leftDiv16b28748ea4df4d9c2150843fecfba685002f9a2c35ee925b1aa664c9611712arightDiv16b28748ea4df4d9c2150843fecfba6816b28748ea4df4d9c2150843fecfba68

2. Display rightDiv first41eeda04e0cea2640f8321ae4ae217e45002f9a2c35ee925b1aa664c9611712arightDiv950c0e09a208ea6a6c67c615a801fbc634f0d6bfb73f0a39713644a8b6a875celeftDiv16b28748ea4df4d9c2150843fecfba6816b28748ea4df4d9c2150843fecfba6836cc49f0c466276486e50c850b7e495673a6ac4ed44ffec12cee46588e518a5e

If floating positioning is not allowed, let’s use absolute positioning

The default div is a row-level tag. If you do not use float, you can display it: inline and then set the width of the two divs appropriately

In summary
When you start learning css,
Playing with float:left ,right is a big deal...
Let’s study the next few templates

In summary
When I started learning css,
Play with float:left , right It’s so confusing...
Let’s study the next few templates
Not bad, I’ve studied them all,,,

Use positioning or TBALE

float:right;

Just use float, the effect of each browser can be set almost, it is impossible to be exactly the same

1. float
2. position
3. diaplay: inline
4 , table
5. ....is there any?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn