繁体   English   中英

CSS-float:在div上不离开流程

[英]CSS - float:left on a div not taking out of flow

我有2个div:

  div.main-info { background: yellow; width: 50%; float: left; } div.main-cta { background: red; width: 20px; } 
  <div id="wrapper"> <div class="main-info"> <h1>Lorem Ipsum?</h1> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> </div> <div class="main-cta"> <div class="window-cta"> <p>Compare Now!</p> </div> </div> </div> 

但是类main-ctadiv似乎仍然在第二行? 这是为什么? 我以为,当您浮动div时,它将从文档流中弹出,下一个元素将占据浮动元素所在的第一行并被其隐藏?

我有点困惑。

谢谢!

Try this

<style>
div.main-info {
    background: yellow;
    width: 50%;
    float: left;
}

div.main-cta {
    background: red none repeat scroll 0 0;
    float: left;
    width: auto;
}

</style>

我认为这对您有帮助。

 div.main-info { background: yellow; width: 50%; float: left; } div.main-cta { background: red; background: red none repeat scroll 0 0; float:right; } 
  <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <div id="wrapper"> <div class="main-info"> <h1>Lorem Ipsum?</h1> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> <div class="main-cta"> <div class="window-cta"> <p>Compare Now!</p> </div> </div> </div> </div> 

只需在div.main-cta添加float:left即可。

div.main-info {
    background: yellow;
    width: 50%;
    float: left;
}
div.main-cta {
    background: red;
    width: 20px;
    float: left; /*new line*/
}

演示小提琴: https : //jsfiddle.net/nikhilvkd/f6stjstk/

或者您不喜欢添加float:left ,则可以使用display:inline-block;

div.main-info {
    background: yellow;
    width: 50%;
    display: inline-block; /*new line*/
}
div.main-cta {
    background: red;
    width: 20px;
    display: inline-block; /*new line*/
    vertical-align: top; 
}

演示小提琴: https : //jsfiddle.net/nikhilvkd/f6stjstk/2/

为了使div从流中弹出,您需要使divposition为绝对position

float要求div在同一行中left移动。

暂无
暂无

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

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