Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Text doesn't wrap?

I'm developing a web app and in one of the <div>s on my page, the text doesn't wrap, instead it just overflows.

Now, the <div> has 2 stages: one where it is not expanded, and one where it is. When it is not expanded, I use the following CSS:

div.todo-item-title{
    display: inline-block;
    max-width: 90%;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden; 
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

Using this CSS, I achieve the following effect:

When the div is not expanded

Basically it has a max-width: 90% (so that it doesn't go out of the box), overflow: hidden (so that the overflow is hidden), white-space: nowrap (so that the text doesn't wrap), and text-overflow: ellipsis (so that it shows the 3 dots on the end). And this result is exactly what I want.

Now, in the 2nd state, I extend the height of the <div>'s parent (the black box that you see on the image), and I want the text to show fully and to wrap when necessary.

So this is the CSS that I used on the text:

div.todo-item-title{
    display: inline-block;
    max-width: none;
    text-overflow: initial;
    white-space: normal;
    overflow: auto; 
    position: relative;
    top: 4px;
}

So, there I have:

  • Set max-width to none, since I want the text to wrap
  • Set white-space to normal so that it does wrap when necessary
  • Set overflow to auto so that it shows the scrollbar when it eventually becomes too long.

But, now it looks like this:

2nd stage

For some reason, it still doesn't wrap. What am I doing wrong?

like image 263
bool3max Avatar asked Apr 21 '26 00:04

bool3max


1 Answers

It does not wrap because you have a long word with no spaces.

In this case, use:

word-wrap: break-word;
like image 62
alephao Avatar answered Apr 23 '26 05:04

alephao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!