简体   繁体   English

如何防止文本环绕在浮动到其左侧的元素下?

[英]How do I keep text from wrapping under an element which floats to its left?

I have two block elements.我有两个块元素。 The first is floated to the left.第一个浮动到左侧。 I'd expect the right element to be a block as well and retain its square shape.我希望正确的元素也是一个块并保持其方形。 Instead, text within it is wrapping under the element which is floating to the left.相反,其中的文本环绕在向左浮动的元素下方。

 .comment-date { float: left; }
 <div class="comment-date">07/08 23:08</div> <div class="comment-body"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation. </div>

The lorem ipsum text wraps under the date. lorem ipsum 文本在日期下换行。 I'd expect it to retain a block shape, floating to the right of the date.我希望它保持块状,浮动到日期的右侧。 How can I achieve that?我怎样才能做到这一点?

Here is a fiddle: http://jsfiddle.net/CS2Hs/这是一个小提琴: http : //jsfiddle.net/CS2Hs/

An alternative to the other solutions here would be to simply add a margin-left to .comment-body .此处其他解决方案的替代方案是简单地向.comment-body添加一个margin-left It would also be helpful to apply a set width to .comment-date将设置的宽度应用于.comment-date也会有帮助

.comment-date { 
  float: left;     
  font-weight: bold;    
  width: 90px;
}
.comment-body {
  margin-left: 90px;
}

This will ensure that the text in .comment-body does not appear underneath the date.这将确保.comment-body中的文本不会出现在日期下方。

Here is a demo:这是一个演示:

--- jsFiddle DEMO --- --- jsFiddle 演示 ---

The body block is not in the float model.身体块不在浮动模型中。 Simply set a float: left for .comment-body too.只需设置一个float: left.comment-body Also, you could use some widths.此外,您可以使用一些宽度。 See: http://jsfiddle.net/CS2Hs/3/见: http : //jsfiddle.net/CS2Hs/3/

I will try to understand your problem.我会尽力理解你的问题。 As I understand, you want the .comment-body to be on the right and the .comment-date to be on the left but on the same line.据我了解,您希望.comment-body位于右侧,而.comment-date位于左侧但在同一行上。

If I am right, maybe this will works:如果我是对的,也许这会奏效:

.comment-date {
    display: inline-block;
    float: left;
    width: 15%;
    font-weight: bold;
}
.comment-body {
    display: inline-block;
    float: right;
    width: 85%;
}

I made a JSFiddle just for you ;)我为你制作了一个JSFiddle ;)

jsfiddle appears to be down at the moment so sorry if this doesn't work/isn't what you wanted but have you tried adding "overflow:hidden" to "comment-body"? jsfiddle 目前似乎已关闭,很抱歉,如果这不起作用/不是您想要的,但您是否尝试过将“溢出:隐藏”添加到“评论正文”? This creates a new layout context which stops the text from wrapping underneath.这会创建一个新的布局上下文,阻止文本在下面换行。 I find this amazingly useful!我发现这非常有用! Hope that helps!希望有帮助!

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

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