如何迫使div出现在下面而不是另一个旁边?


74

我想将div放在列表下方,但实际上它位于列表旁边。列表是动态生成的,因此没有固定的高度。我想将map div放在右边,将div放在列表的顶部(在地图的旁边),将第二div放在列表的下面(但仍然在map的右边)

#map { float:left; width:700px; height:500px; }
#list { float:left; width:200px; background:#eee; list-style:none; padding:0; }
#similar { float:left; width:200px; background:#000; } 
<div id="map">Lorem Ipsum</div>        
<ul id="list"><li>Dolor</li></li>Sit</li><li>Amet</li></ul>
<div id ="similar">
    this text should be below, not next to ul.
</div>

有任何想法吗?

Answers:


41

我认为您想要的是一个额外的包装div。

#map {
    float: left; 
    width: 700px; 
    height: 500px;
}
#wrapper {
    float: left;
    width: 200px;
}
#list {
    background: #eee;
    list-style: none; 
    padding: 0; 
}
#similar {
    background: #000; 
}
<div id="map">Lorem Ipsum</div>        
<div id="wrapper">
  <ul id="list"><li>Dolor</li><li>Sit</li><li>Amet</li></ul>
  <div id ="similar">
    this text should be below, not next to ul.
  </div>
</div>


70

使用clear:left; 或清除:两者都在您的CSS中。

#map { float:left; width:700px; height:500px; }
 #list { float:left; width:200px; background:#eee; list-style:none; padding:0; }
 #similar { float:left; width:200px; background:#000; clear:both; } 


<div id="map"></div>        
<ul id="list"></ul>
<div id ="similar">
 this text should be below, not next to ul.
</div>

我的意思是它应该在列表下方,而不是在地图下方。我想在地图的右侧,但在列表下方。
Vonder 2010年

10
#similar { 
float:left; 
width:200px; 
background:#000; 
clear:both;
}

7

浮动#list#similar,然后添加clear: right;#similar

像这样:

#map { float:left; width:700px; height:500px; }
#list { float:right; width:200px; background:#eee; list-style:none; padding:0; }
#similar { float:right; width:200px; background:#000; clear:right; } 


<div id="map"></div>        
<ul id="list"></ul>
<div id="similar">this text should be below, not next to ul.</div>

尽管左右元素的宽度相同,但您可能需要围绕它们全部使用wrapper(div)。


1

您还可以做什么?在上一个div之前放置一个额外的“虚拟” div。

使其高度为1 px,并使其宽度足以覆盖容器div / body

这将使最后一个div从左开始显示。


0
#map {
  float: right;
  width: 700px;
  height: 500px;
}
#list {
  float:left;
  width:200px;
  background: #eee;
  list-style: none;
  padding: 0;
}
#similar {
  float: left;
  clear: left;
  width: 200px;
  background: #000;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.