[英]Why a div with float: right does not respect the margin of the hr tag below?
I'm using twitter-bootstrap framework and I'm trying to put a form-control with some text aligned to the right and some text aligned the the left (all in the same line). 我正在使用twitter-bootstrap框架,我正在尝试将一个文本控件放在右边,一些文本对齐左边(所有文本都在同一行)。
Here is the fiddle: http://jsfiddle.net/2gu29/3/ 这是小提琴: http : //jsfiddle.net/2gu29/3/
HTML HTML
<div class="inlineb">Hello</div>
<div class="floatr inlineb">
<input type="text" class="form-control inlineb"> cm
</div>
<hr>
<button class="btn btn-primary">Hello stackoverflow</button>
CSS CSS
hr {
margin: 30px 0px 30px 0px;
border-color: red;
}
body {
padding: 30px;
}
.form-control {
width: 100px;
}
.floatr {
float: right;
}
.inlineb {
display: inline;
}
But, as you see, the div which contains the input above the hr
tag, does not respect the margin setted to the hr in the css (margin-top: 30px) if I use float: right
. 但是,如您所见,包含
hr
标记上方输入的div不考虑在css中设置为hr的边距(margin-top:30px),如果我使用float: right
。 But if I don't use this property, it respect it. 但如果我不使用这个属性,它会尊重它。 see it: http://jsfiddle.net/2gu29/9/
看到它: http : //jsfiddle.net/2gu29/9/
Why my solution does not work? 为什么我的解决方案不起作用? Could you give me an exmplanation, please?
你能告诉我一个例外吗? Do you have another alternative to do it?
你还有其他选择吗?
Any tip, advice or help will be appreciated, and if you need more info, let me know and I'll edit the post. 任何提示,建议或帮助将不胜感激,如果您需要更多信息,请告诉我,我将编辑该帖子。
The float CSS property specifies that an element should be taken from the normal flow and placed along the left or right side of its container, where text and inline elements will wrap around it.
float CSS属性指定一个元素应该从正常流中获取并放置在其容器的左侧或右侧,其中文本和内联元素将环绕它。
If you read this line from MDN Float Documentation you can understand that whenever we apply float
property to any element, it is taken out from normal flow of the page. 如果您从MDN Float文档中读取此行,您可以理解,每当我们将
float
属性应用于任何元素时,它都会从页面的正常流程中取出。
Hence, margin is getting calculated from the Hello word instead of floated element. 因此,从Hello字而不是浮动元素计算边际。
Solution 解
Wrap the divs inlineb and floatr
in another div and you should be fine. 将divs
inlineb and floatr
在另一个div中你应该没问题。 Checkout this JS Fiddle 看看这个JS小提琴
Updated Code 更新的代码
<div class='container'>
<div class="inlineb">Hello</div>
<div class="floatr inlineb">
<input type="text" class="form-control inlineb" /> cm
</div>
</div>
<hr>
<button class="btn btn-primary">Hello stackoverflow</button>
@blunderboy has already explained what the problem is. @blunderboy已经解释了问题所在。 My answer is just an alternate solution, which I have floated the left element and aligned the input to the right, therefore it still has height.
我的答案只是一个替代解决方案,我已经浮动左侧元素并将输入对齐到右侧,因此它仍然具有高度。
HTML HTML
<div class="floatl">Hello</div>
<div class="rightinput">
<input type="text" class="form-control inlineb"/> cm
</div>
<hr>
<button class="btn btn-primary">Hello stackoverflow</button>
CSS CSS
hr {
margin: 30px 0px 30px 0px;
border-color: red;
}
body {
padding: 30px;
}
.form-control {
width: 100px;
}
.floatl {
float: left;
}
.inlineb {
display: inline;
}
.rightinput {
text-align: right;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.