简体   繁体   English

如何在100%宽度的div上换行?

[英]How to wrap text on a 100% width div?

I have a 3 div with their display property set to table-cell. 我有一个3格,其显示属性设置为table-cell。 Div 1 is fixed width (22px). Div 1是固定宽度(22像素)。 Div 2 needs to take up the rest of the available space. 第2部门需要占用剩余的可用空间。 Div 3 needs to be pinned to the right side and only take up as much as the text that is in it. Div 3需要固定在右侧,并且只占用其中的文本。

So the issue I am having is when the contents of Div 2 are too big it pushes Div 3 off the screen. 所以我遇到的问题是,当Div 2的内容太大时,它将Div 3推离屏幕。

没有剪裁的文字

大胆的总数被推离屏幕

This is a mobile web application. 这是一个移动Web应用程序。 Ideally what would happen is the text in Div 2 would clip if it was going to push the bold totals of the screen. 理想情况下,如果要推送屏幕的粗体总计,则将发生Div 2中的文本将被剪切的情况。 I could do it in javascript, but don't think I should have to. 我可以用javascript来做,但不要以为应该这样做。

Here is the template code for the list item: 这是列表项的模板代码:

<div class="liContainer">
    <div class="cell a"><img src="badge.png" width="20px"/></div>
    <div class="cell b">{title}</div>
   <div class="cell c"><b>{total}</b></div>
</div>

Here is the css: 这是CSS:

.cell{
    display: table-cell;
    vertical-align: middle;
}

.a{
    width: 22px;
    padding-right: 15px;
}

.b{
    width: 100%;
}

.c{
    text-align: right;
}

.liContainer{
    width: 100%;
    display: table-row;
}

EDIT: The result of employing the technique Jithesh describes below 编辑:下面应用Jithesh技术的结果描述

You can do it with floats and it works perfect. 您可以使用浮子来完成它,并且效果很好。

  • Give float(left or right) to the elements that has fixed width or width of its contents. 将float(左或右)赋予宽度固定或宽度固定的元素。
  • And overflow: hidden to the element that should take up the remaining width. overflow: hidden在应该占用剩余宽度的元素上。

Important: The cells have to be in the order a > c > b as the floating cells must preceed the div that takes up the remaining space. 重要提示:单元格必须按照a> c> b的顺序排列,因为浮动单元格必须位于div占据剩余空间的前面。

Here is the html: 这是html:

<div class="liContainer">
    <div class="a cell"><img src="https://www.google.co.in/images/srpr/logo4w.png" width="20px"/></div>
    <div class="c cell"><b>197</b></div>
    <div class="b cell">Hey, This is the long long long title</div>
</div>

And the CSS 和CSS

.cell{
    padding: 20px 10px;
}

.a{
    width: 22px;
    padding-right: 15px;
    float: left;
    background: #ccc;
}

.b{
    overflow: hidden;
    background: #eee;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.c{
    float: right;
    background: #ccc;
}

.liContainer{
    width: 100%;
    //display: table-row;
}

More improvements 更多改进

Add text-overflow: ellipsis and white-space: nowrap; 添加text-overflow: ellipsiswhite-space: nowrap; to the block 'c' to clip the overflowing text with '...' that looks cool than normal clipping. 到'c'块,用'...'剪切溢出的文本,看上去比普通剪切还酷。

See it here: http://jsfiddle.net/Y3JEr/ 在这里查看: http : //jsfiddle.net/Y3JEr/

What can be done is that if the b. 可以做的是,如果b. is going to be too big, it will wrap the text to the second "row" (the height of whole section would double if needed). 将会太大,它将文本包装到第二个“行”(如果需要,整个部分的高度将增加一倍)。 For this you have to set up something else than width: 100% for the .b container. 为此,您必须设置width: 100%以外的其他内容width: 100% .b容器的width: 100%

Why don't you just set width of an .a to 10%, .b to 80% and .c to 10%? 为什么不将.a width设置为10% .b width设置为80% .c width设置为10%? If you won't define the height the content of a .b will just jump into second row. 如果您不定义高度,则.b的内容将跳至第二行。


Alternatively, if you need right div to be pinned to the right part of the screen, set absolute position for him like this 另外,如果您需要将右div固定在屏幕的右侧,请像这样为他设置绝对位置

.c {
    position: absolute;
    right: 0px;
}

But be carefull, the .liContainer should have 但是要小心, .liContainer应该具有

position: relative;

otherwise, the absolute positioning for .c won't work. 否则, .c的绝对定位将不起作用。

This won't solve the whole problem probably, you will still need to make .b div less than 100% I guess. 这可能无法解决整个问题,我仍然需要将.b div设置为小于100%。

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

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