Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide too long texts in div elements?

Tags:

css

How can you hide the overflow in a div?

Here the text just wraps down to a new line if the text is longer than the div

<div style="width:50px; border:1px solid black; overflow-y:hidden">     test test test test </div> 
like image 607
clarkk Avatar asked Sep 30 '11 14:09

clarkk


People also ask

How do I hide text-overflow?

A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.


2 Answers

If you only want to show a single line of text in a fixed width div, give white-space:nowrap a go. Together with overflow:hidden it will force your browser not to break the line and crop the view to your forced width.

You might also add text-overflow:ellipsis to avoid cutting words and letters in half, it adds a nice ... to the end to show you there's some bits missing. I'm not sure about the browser support though, and I've never tried this one.

like image 158
DarthJDG Avatar answered Oct 14 '22 06:10

DarthJDG


The CSS property text-overflow: ellipsis maybe can help you out with that problem.

Consider this HTML below:

<div id="truncateLongTexts">     test test test test </div>  #truncateLongTexts {   width: 100px;   white-space: nowrap;   overflow: hidden;   text-overflow: ellipsis // This is where the magic happens } 
like image 33
jhunlio Avatar answered Oct 14 '22 05:10

jhunlio