简体   繁体   English

如何通过文本换行符保留div高度

[英]How to preserve div height with text line-breaks

I'm trying to show boxes of people's names. 我正在尝试显示人们的名字。 The problem is that these names can get pretty long, and with a fixed width box, crazyness is sure to ensue. 问题在于这些名称可能会变得很长,并且使用固定宽度的框,肯定会出现疯狂。 Long spaced text breaks, causing problems on new rows ( http://jsfiddle.net/9MhWW/ ) and disabling line-breaks with white-space: nowrap; 较长的文本换行符,在新行上引起问题( http://jsfiddle.net/9MhWW/ ),并用white-space: nowrap;禁用换行符white-space: nowrap; will overflow the box ( http://jsfiddle.net/9MhWW/1/ ). 将溢出该框( http://jsfiddle.net/9MhWW/1/ )。

Working with line breaks seem like the best option. 使用换行符似乎是最好的选择。 If you agree, that begs the following question: Is there a way to make sure the row-problem doesn't occur without losing part of the text, having a div for every row or giving the paragraph a fixed height? 如果您同意,那么将提出以下问题:是否有一种方法可以确保在不丢失部分文本,每行有一个div或给段落固定高度的情况下不会出现行问题?

If just one row is ok for you, set a overflow: hidden : 如果只有一行适合您,请设置一个overflow: hidden

p { overflow: hidden; }

A nice effect will be using ellipsis to replace the overflowing text with ... : 一个很好的效果是使用省略号将溢出的文本替换为...

p { overflow: hidden; text-overflow: ellipsis; }

See this update to your fiddle . 看到这个更新到您的小提琴

I think I have come up with a solution. 我想我已经提出了一个解决方案。 So bootstrap adds loads of stuff in to make it look nice of course, but in your case we have to undo some of that. 因此,引导程序会添加很多东西以使其看起来不错,但是在您的情况下,我们必须撤消其中的一些操作。 So we have to get rid of the float left and replace it with display inline-block: 因此,我们必须摆脱浮点数,并用display inline-block替换它:

.span2{
    float: none !important;
    display:inline-block;
}

See the fiddle update . 参见小提琴更新 I had to change the text alignment to match what you had previously. 我必须更改文本对齐方式以匹配您以前的内容。

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

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