我有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;
但是div
类main-cta
似乎还在第二行?这是为什么?我认为当你浮动一个div时,它会从文档流中弹出,下一个元素将占据浮动元素所在的第一行并被它隐藏?
我有点困惑。
谢谢!
答案 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;
}
答案 3 :(得分:0)
要将div
弹出流量,您需要设置position
绝对值的div
。
float
要求div
向同一行的left
移动。