简体   繁体   English

使用float和margin-left创建html布局时出现问题

[英]Problems creating html layout using float and margin-left

I have the following HTML code that displays some DIV elements on a page. 我有以下HTML代码在页面上显示一些DIV元素。

 <div style="float: left; width:182px; height:200px; border-style: solid; border-width:1px">Left Column</div> <div style="margin-left: 200px; border-style:solid; border-width:1px"> <div style="float: left; border-style: solid;border-width:1px;">Div1</div> <div style="float: left; border-style: solid;border-width:1px;">Div2</div> <div style="clear:left; border-style: solid;border-width:1px;">Div3</div> <div style="float: left; border-style: solid;border-width:1px;">Div4</div> </div> 

I would like Div3 to be shown just below Div1 and Div2, but instead it is shown where the left column ends, so there is a large space between div1/div2 and div3. 我希望将Div3显示在Div1和Div2的正下方,但是将其显示在左列的末尾,因此div1 / div2和div3之间有很大的空间。

How can I get div3 to be shown just below div1/div2? 如何才能在div1 / div2下方显示div3? It is also important the div3 uses the whole available width. div3使用整个可用宽度也很重要。

use this may help u 用这个可能对你有帮助

<div style="float: left; width:182px; height:200px; border-style: solid; border-width:1px">Left Column</div>
<div style="margin-left: 200px; border-style:solid; border-width:1px">

  <div style="float: left; border-style: solid;border-width:1px;">Div1</div>
  <div style="border-style: solid;border-width:1px;">Div2</div>
  <div style="border-style: solid;border-width:1px; display:block;">Div3</div>
  <div style="border-style: solid;border-width:1px; display:block">Div4</div>
</div>

 div{ border: 1px solid #000; box-sizing: border-box; } .sidebar{ float: left; width:27%; height: 200px; } .content{ float: left; width: 70%; margin-left: 3%; } .content:after{ content: ''; clear: both; display: block; } 
 <div class="sidebar">Left Column</div> <div class="content"> <div style="float: left; ">Div1</div> <div style="float: left;">Div2</div> <div style="clear:left;">Div3</div> <div style="float: left;">Div4</div> </div> 

https://jsfiddle.net/3sfa0jc1/ https://jsfiddle.net/3sfa0jc1/

You need to be careful with floats, there are for float elements, not to design layout. 您需要小心浮动,这里有浮动元素,而不是设计布局。 I recommend that changes floats by displays. 我建议更改按显示浮动。 Otherwise, I solve your issue with floats. 否则,我会用花车解决您的问题。

You can view it running: http://jsfiddle.net/h5o50n41/ 您可以运行它来查看它: http : //jsfiddle.net/h5o50n41/

The code: 编码:

<div style="float: left; width:182px; height:200px; border-style: solid; border-width:1px"> Left Column </div>   
<div style="float:left;margin-left: 200px; border-style:solid; border-width:1px"> 

   <div style="float: left; border-style: solid;border-width:1px;"> Div1 </div>
   <div style="float: left; border-style: solid;border-width:1px;"> Div2 </div>
   <div style="clear:left;float:left; width:100%; border-style: solid;border-width:1px;"> Div3 </div>  
   <div style="float: left; border-style: solid;border-width:1px;"> Div4 </div> 
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM