Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - how to trim text output?

Tags:

text

css

scroll

I have an arbitrary amount of text that will be displayed in a confined space.

How can I trim the output so that whatever is "beyond" the box is not displayed, or how can I force the box to create a vertical scroll bar?

like image 445
warren Avatar asked Dec 01 '25 14:12

warren


2 Answers

For HTML:

<div id="smallBoxWithLotsOfText">There is way more text in here than what
    I have typed.  I mean, this text is long.  There is lots of it.  
    You can't even imagine how long this text is gonna get.  No joking.  
    It's long; it's very, very long.  It keeps going, and going, and going. 
    It's the Energizer Bunny of text.  Like, seriously dude.  It's crazy.  
    Absolutely crazy.
</div>

Try CSS:

#smallBoxWithLotsOfText {
    width: 100%;
    height: 100px;
    overflow: auto;
}

The height property tells the box how high to be. The overflow property tells the box to add a scroll bar when the contents get bigger, but not to always have a scrollbar (like scroll does).

You can see this in action.

like image 61
justkt Avatar answered Dec 03 '25 12:12

justkt


usually 'overflow: auto' should work if there is a set height/width. You can force a scroll bar with 'overflow: scroll'. You can hide anything with 'overflow: hidden;'

The key to overflow with CSS styles is the height and width have to be determined by the browser in order for it to know when to start overflowing.