简体   繁体   English

CSS 使订单项文本换行

[英]CSS to make line items text wrap

How can I extend the vertical size of a <li> when text wrapping is required, but keep a minimum value?当需要文本换行时,如何扩展<li>的垂直大小,但保持最小值?

在此处输入图像描述

The look of this is exactly what I want when the text is short enough to fit on one line.当文本足够短以适合一行时,它的外观正是我想要的。 But if the text is longer (I'm not expecting more than 2 lines), it gets cut.但如果文本较长(我预计不会超过 2 行),它就会被删减。

My HTML looks like this:我的 HTML 看起来像这样:

 .items { margin: 0 0 2em 0; list-style-type: none; padding: 0em 0.5em; width: 20em; }.items li { cursor: pointer; position: relative; left: 0.2em; background-color: #EEE; margin: .3em; padding: .3em 0; height: 1.6em; border-radius: 4px; }.items li.test { font-weight: bold; }.items.badge { display: inline-block; font-size: small; color: white; padding: 0.8em 0.7em 0 0.7em; background-color: #607D8B; line-height: 1em; position: relative; left: -1px; top: -4px; height: 1.8em; margin-right: .8em; border-radius: 4px 0 0 4px; width: 2em; }
 <ul class="items"> <li class="test"> <span class="badge">0</span> This is a long string that doesnt fit on one line. Need to extend this box vertically </li> <li class="test"> <span class="badge">0</span> Next item </li> </ul>

My problem with changing .items li { height: 1.6em; }我改变.items li { height: 1.6em; }的问题.items li { height: 1.6em; } to max-height is I end up with this: .items li { height: 1.6em; }max-height是我最终得到这个:

在此处输入图像描述

I don't mind the messed up long string, but the height of the second <li> is now larger.我不介意弄乱的长字符串,但是第二个<li>的高度现在更大了。

Try swapping height for min-height :尝试将height换成min-height

 .items { margin: 0 0 2em 0; list-style-type: none; padding: 0em 0.5em; width: 20em; }.items li { cursor: pointer; position: relative; left: 0.2em; background-color: #EEE; margin: .3em; padding: .3em 0; min-height: 1.6em; border-radius: 4px; }.items li.test { font-weight: bold; }
 <ul class="items"> <li class="test"> This is a long string that doesnt fit on one line. Need to extend this box vertically </li> <li class="test"> Next item </li> </ul>

Update:更新:

To account for your follow-up comment: Since you're using a padding on the <li> element and you want the badge to occupy the full height without using position: absolute , remove the explicit height declaration from the badge and top: -4px and use negative margins instead:考虑到您的后续评论:由于您在<li>元素上使用了填充,并且您希望徽章在不使用position: absolute的情况下占据整个高度,因此从徽章和top: -4px删除显式height声明:- top: -4px并改用负边距:

 .items { width: 20em; margin: 0 0 2em 0; padding: 0em 0.5em; list-style-type: none; }.items li { min-height: 1.6em; position: relative; left: 0.2em; margin: .3em; padding: .3em 0; background-color: #EEE; border-radius: 4px; cursor: pointer; }.items li.test { font-weight: bold; }.items.badge { color: white; font-size: small; line-height: 1em; width: 2em; display: inline-block; position: relative; left: -1px; margin-top: -4px; margin-bottom: -4px; margin-right: .8em; padding: 0.8em 0.7em; background-color: #607D8B; border-radius: 4px 0 0 4px; }
 <ul class="items"> <li class="test"> <span class="badge">0</span> This is a long string that doesnt fit on one line. Need to extend this box vertically </li> <li class="test"> <span class="badge">0</span> Next item </li> </ul>

If your li is too short for single-line cases, then min-height should do it (instead of height ).如果您的li对于单行情况来说太短,那么min-height应该这样做(而不是height )。 The element will take up a minimal height that you set and might expand if needed.该元素将占用您设置的最小高度,并且可能会在需要时扩展。

 .items { margin: 0 0 2em 0; list-style-type: none; padding: 0em 0.5em; width: 20em; }.items li { cursor: pointer; position: relative; left: 0.2em; background-color: #EEE; margin: .3em; padding: .3em 0; min-height: 1.6em; /* Change this */ border-radius: 4px; }.items li.test { font-weight: bold; }
 <ul class="items"> <li class="test"> This is a long string that doesnt fit on one line. Need to extend this box vertically that is really something something something. </li> <li class="test"> This is a long string that doesnt fit on one line. Need to extend this box vertically </li> <li class="test"> Next item </li> </ul>

 .items { margin: 0 0 2em 0; list-style-type: none; padding: 0em 0.5em; width: 20em; }.items li { cursor: pointer; position: relative; left: 0.2em; background-color: #EEE; margin: .3em; padding: .3em 0; min-height: 1.6em; border-radius: 4px; }.items li.test { font-weight: bold; }
 <ul class="items"> <li class="test"> This is a long string that doesnt fit on one line. Need to extend this box vertically </li> <li class="test"> Next item </li> </ul>

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

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