簡體   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