CSS - 浮动:留在一个没有流出的div上

时间:2016-01-25 10:02:05

标签: html css css3

我有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>
&#13;
&#13;
&#13;

但是divmain-cta似乎还在第二行?这是为什么?我认为当你浮动一个div时,它会从文档流中弹出,下一个元素将占据浮动元素所在的第一行并被它隐藏?

我有点困惑。

谢谢!

4 个答案:

答案 0 :(得分:2)

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>

答案 1 :(得分:2)

我认为这会对你有所帮助..

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>

答案 2 :(得分:1)

只需将float:left添加到div.main-cta

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/

答案 3 :(得分:0)

要将div弹出流量,您需要设置position绝对值的div

float要求div向同一行的left移动。