简体   繁体   English

并排使用float <div>

[英]side by side using float <div>

I seem to have problem with understanding float property 我似乎对理解float属性有问题

<div style="background-color:green;width:40px;height:50px;float:left;"> test </div>
<div style="background-color:blue;width:50px;height:40px"> hello  </div>

i am floating the first div to the left and the 2nd one should be next to it but it just goes on top of it, and BTW it works if i add to the 2nd div : overflow:scroll 我将第一个div浮动到左边,第二个div应该在它旁边,但是它就在它的顶部,顺便说一句,如果我添加到第二个div ,它会起作用overflow:scroll

By adding float: left; 通过添加float: left; to the second div, we can fix this. 到第二个div,我们可以解决此问题。 You could also use float: right; 您也可以使用float: right; on the second div. 在第二个分区

http://jsfiddle.net/x3Lgu/ http://jsfiddle.net/x3Lgu/

You need to float both of the div's. 您需要同时浮动两个div。 Floating only one div creates space that other elements can wrap up and around the one that is floated. 仅浮动一个div会创建其他元素可以包裹的空间,并围绕浮动的元素。 It looks weird in your case as you have limited the width to be very narrow. 您的情况看起来很奇怪,因为您将宽度限制为非常窄。 If the width's were larger you would see what I mean more clearly. 如果宽度较大,您会更清楚地看到我的意思。 You see this commonly with pages that have text wrapping around an image. 在带有文字环绕图像的页面上,通常会看到这种情况。 For example.... 例如....

http://jsfiddle.net/ErjcN/ http://jsfiddle.net/ErjcN/

Notice how the text wraps around the image. 请注意文本如何环绕图像。 This is exactly whats happening to your document. 这正是您的文档所发生的事情。

You will have to float the other div also to the left. 您还必须将另一个div浮动到左侧。 Float works with two divs if you float both. 如果您同时浮动两个div,则浮动会起作用。 Right now the div without float is just the same. 现在不带浮点数的div是一样的。

So: 所以:

<div style="background-color:green;width:40px;height:50px;float:left;"> test </div>
<div style="background-color:blue;width:50px;height:40px; float: left;"> hello  </div>

It might seem strange, but it's just how css works. 可能看起来很奇怪,但这只是CSS的工作原理。

add a float:left; 添加一个float:left; to your 2nd div as well and it should put them next to each other 到您的第二个div也应该将它们彼此相邻

<div style="background-color:green;width:40px;height:50px;float:left;"> test </div>
<div style="background-color:blue;width:50px;height:40px;float:left"> hello  </div>

You have to give the float: left property to both of the div's otherwise it will not work. 您必须为两个div都赋予float: left属性,否则它将不起作用。

What you also can do is apply display: inline-block , it floats the 2 div s next to each other aswell. 您还可以做的就是应用display: inline-block ,它也使2 div彼此相邻地浮动。

HTML HTML

<div class='first'>First div</div>
<div>Second div</div>

CSS CSS

div {
    float: left;
    width: 50px;
    background-color: blue;
}

.first {
    width: 40px;
    background-color: green;
}

How this works... 运作方式...

Consider the following HTML snippets: 考虑以下HTML代码段:

<h2>Default Behaviour</h2>
<div style="background-color:green;width:40px;height:50px;float:left;">test</div>
<div style="background-color:lightblue;width:50px;height:40px;">a a a a hello</div>

<br><br>
<h2>New Block Formatting Context</h2>
<div style="background-color:green;width:40px;height:50px;float:left;">test</div>
<div style="background-color:lightblue;width:50px;height:40px; overflow: auto;">a a a a hello</div>

In the first case, I added some single letter words to demonstrate the behavior. 在第一种情况下,我添加了一些单字母单词来演示这种行为。 CSS places the non-floated element to the top and left of the containing block since the previous element is floated hence out of the regular content flow. CSS将非浮动元素放置在包含块的顶部和左侧,因为前一个元素是浮动的,因此不在常规内容流中。

CSS then places the floated element to the top left (as high as it can go) and starts wrapping the in-flow content around it. 然后,CSS将浮动元素放在左上方(尽可能高),并开始将流内内容包装在其周围。 Since the in-flow div has a fixed width, the content is constrained to the right as shown by the single letters wrapping around the floated element. 由于流入div的宽度是固定的,因此内容会限制在右侧,如环绕浮动元素的单个字母所示。

In the second case, I establish a new block formatting context by assigning overflow: auto ( scroll would also work, and float ). 在第二种情况下,我通过分配overflow: auto建立了一个新的块格式化上下文overflow: autoscroll也将起作用,并且float )。 In this case, the left edge of the in-flow element is placed to the right of the floated element, so there is no text wrapping around the floated element. 在这种情况下,流入元素的左边缘位于浮动元素的右侧,因此没有文本环绕浮动元素。

See demo at: http://jsfiddle.net/audetwebdesign/Rd3Fp/ 参见演示: http//jsfiddle.net/audetwebdesign/Rd3Fp/

Results look like: 结果如下:

在此处输入图片说明

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

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