这个有点难描述,但基本上我的页面上有一个浮动的div会留下不必要的空白。以下是描述问题的图片。黑色方框是div。
浮动之前: 浮动之后: 期望效果: 我不确定这是否有影响,但是我还放了一个带“clear:both”的空div在浮动的div之后。
如何实现这个效果?
浮动之前: 浮动之后: 期望效果: 我不确定这是否有影响,但是我还放了一个带“clear:both”的空div在浮动的div之后。
如何实现这个效果?
float:right
的<div class="a1">a1</div>
<div class="a2">a2</div>
.a1
{
background-color:Red;
width:200px;
height:200px;
float:left;
}
.a2
{
background-color:Green;
width:200px;
height:200px;
float:left;
}
=======尝试这个
HTML
<div id="container">
<div id="main">
blah blah blah blah blah
</div>
<div id="aside">
this goes to the side
</div>
<div id="clear"></div>
</div>
CSS
div#container
{
width : 80%;//your preference
height : auto;
margin : 0 auto;//Centers the page
}
div#main
{
width : 70%;
height : auto;
display : block;//to wrap it up inside its width
float : left;//float it towards left
}
div#aside
{
width : 30%;//take up rest of the width size
height : auto;
display : block;
float :right;//float towards right
}
#clear
{
clear : both;//this will do the job
}
删除clearing div。同时检查这些div的padding / margin,并确保包含div(父div)足够宽以容纳两个子div。
div
应该设置为 float: left
。否则第一个块级元素的垂直空间将全部占用。<div id="container" style="width: 410px;">
<div style="float: right; width: 200px;">
<p> Right div</p>
</div>
<div style="width: 200px; margin-right: 210px;">
<p> Left Div</p>
</div>
<div style="clear:both;"></div>
</div>
或者:
<div id="container" style="width: 410px;">
<div style="float: left; width: 200px; margin-right: 10px;">
<p> Left Div</p>
</div>
<div style="float: left; width: 200px;">
<p> Right div</p>
</div>
<div style="clear:both;"></div>
</div>
<div class="container">
<div class="a1">a1</div>
<div class="a2">a2</div>
</div>
<style>
div
{
border: solid 2px #000;
background-color: #eee;
}
.a1
{
background-color:Red;
width:200px;
height:200px;
float:right; /* the trick is, a1 appears BEFORE a2 in the html, yet
we are floating a1 right . if you do it the other way around
( try and float a2 ) then it will work like you showed
(separate line)*/
}
.a2
{
background-color:Green;
width:200px;
height:200px;
/* don't float this one */
}
</style>
浮动元素存在空白间隙问题,这就是为什么第二个框稍微低一些的原因。
<div style="float:left">a1</div><div style="float:left">a2</div>
会起作用。
<div style="float:left">a1</div>
<div style="float:left">a2</div>
无法工作
两个 div 元素应该向左浮动,并确保它们的宽度等于或小于它们所在容器的宽度。