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?
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.
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.
The overflow property. You can set the value to hidden to hide the data and scroll to scroll the data.
<div class="text">this is some text that would be very long...</div>
//Hidden
.text
{
overflow: hidden;
width: 50px;
height: 50px
}
//Scroll
.text
{
overflow: scroll;
width: 50px;
height: 50px
}
Take a look at overflow. overflow: hidden clips content, overflow:Scroll adds a scrollbar.
How can I trim the output so that
whatever is "beyond" the box is not
displayed
Use overflow: hidden
How can I force the box to create a
vertical scroll bar?
Use overflow: auto. To use this, though, make sure you have a width/height specified on the box
You may try using ellipsis by adding the following in CSS:
.truncate {
width: 250px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
More ways to trim text and show ellipsis can be found here:
http://blog.sanuker.com/?p=631
Related
In this example...
HTML
<body>
<div>
<div>foo bar</div>
</div>
</body>
CSS
body, html, div {
height: 100%;
margin: 0;
}
div div {
display: inline-block;
overflow: hidden;
}
Why does overflow: hidden cause a vertical scrollbar? Additionally, why is this height not attributed to anything on the page? It's like an invisible margin.
The 100% height of all the elements is intentional. In theory, that should cause the inner-most div to expand to meet the viewport. And it does! ...so long as overflow: hidden is not there, Why?
The extra height is the same height as the height difference between vertical-align: baseline, and vertical-align: bottom. The "descender line". That's where the seemingly random "5 extra pixels" comes from. If the font size is 10 times as large, this gap will also be 10 times as large.
Also, it seems that when overflow: hidden is not there, the inline-block element has its baseline as the same baseline of its last line of text.
This leads me to believe that overflow: hidden forces the baseline of the entire inline-block element to be at the bottom of the element. Even though there is no text there, the parent of the inline-block element reserves space for the descender line. In the example given in the question, it cannot be easily seen since the parent of the inline-block element has height: 100%. So, instead, that extra space reserved for the descender line overflows out of that parent div.
Why is this space still there, even though there's no text? I think that's because the inline-block creates an inline formatting context, which is what causes this space. Were this element to be a block, it would only create this inline formatting context once it encounters an inline element or text.
This is just a theory, but it seems to explain it. It also explains why #Jonny Synthetic's answer works: adding overflow: hidden to the parent hides that extra descender line.
Thanks to #Hashem Qolami for the jsbins that gave me this theory.
Adding overflow: hidden to the parent div wasn't an option for me, and also because of my HTML's structure it wasn't working.
However, I noticed thanks to #Tony Gustafsson's comment in the OP that this does fix the problem:
div div {
vertical-align: bottom;
}
Items with a height of 100% need to have overflow: hidden as well. The first css rule only targets the outside div, the overflow hidden is being applied to the inside div.
Jsfiddle with this CSS and it worked fine:
body, html, div {
height: 100%;
margin: 0px;
padding:0px;
overflow: hidden;
}
div div {
display: inline-block;
overflow: hidden;
}
I have tried all the solutions I can find to this problem, but the damn thing keeps overflowing. To be specific, when I add anything more than an extra word to the "Watch the space" text to the right here http://robinlovelace.net/ , it disappears from where it should be an moves to the bottom of the page, below the 'comments' link.
I've tried adding overflow: auto; and word-wrap: break-word; in the CSS #home_right area, (as recommended here) but this fails also. It's such a simple problem I think I must be missing something that may be of use to others.
You need to set a width on it.. try the following:
#home_right {
width: 220px;
}
This is what it currently is:
#home_right {
overflow: auto width: 10 em; /* Syntax is incorrect, you forgot the ; */
}
In this case, there is actually no need for overflow.. all you needed was a width!
This is because the div is floated and when you add content to it the width is extended by the text and becomes larger than the available area and so it floats to the nearest available space. To prevent the text from widening the div, you need to set a width on the sidebar. Using firebug I figured out that in your case you need to add this in your CSS for #home_right:
width: 224px;
What you have:
overflow: auto width: 10 em;
What you should have:
overflow: auto;
width: 10em;
Spaces and semi-colons, be careful!
I've been working on creating a Spell check dialog for the tinyMCE editor using jQuery's dialog control. My reason for doing this is because our users have touch screens and the default method of click-word/click-replacement is too cumbersome.
I've finally got the spellcheck dialog to the point of everything works.
However, the div where I display the text can go nuts in 2 different ways if provoked.
For example, here is the dialog as I want it to work. Seen "working" here in ie7. http://jsfiddle.net/PMX8r/2/
Viewed in ie8 (or any newer browser) it is a much different matter.
The other issue is if the user enters a ridiculously long word my buttons get pushed away! Seen here in ie7. http://jsfiddle.net/PMX8r/3/
What style properties should I be looking at get this div under control?
Edit:
Sweet, it looks like overflow: hidden solves the 2nd issue as well.
The buttons aren't pushed aside for me, but the first issue, where text overflows the div, can be fixed by
.SpellCheckDiv {
overflow: hidden;
}
Assuming there's some other way to scroll, otherwise use overflow: scroll;
Try adding
overflow: scroll
or
overflow: hidden
to the style tag of your container-textarea.
This will add scrollbars (1st case) or hide the overflowing text.
The reason of this behavior is your div is 100px in height and your content is exceeding the limit of 100px
use overflow:auto; in your .SpellCheckDiv class.
.SpellCheckDiv
{
height: 100px;
width: 318px;
border: 2px solid black;
word-wrap: break-word;
overflow:auto;
}
overflow:hidden; - if you dont need scrollbars
you can use overflow-x:{scroll|auto|hidden} and overflow-y:{scroll|auto|hidden} to even controll your vertical and horizontal scrollbars too.(CSS 3)
You can test the result of different overflow behavior here
http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_overflow-xy
The overflow property should help you. With respect to your code:
.SpellCheckDiv
{
height: 100px;
width: 318px;
border: 2px solid black;
word-wrap: break-word;
overflow: scroll;
}
It says when there is a overflow of text (i.e. it doesn't fit in the current window size), it should automatically scroll.
This is the modified JSFiddle code.
Basically, I need the overflow to behave like "auto", but hiding the scrollbar.
If I use "hidden" I cannot select the text beyond the limit.
try using something like this applied to the DIV that you want to control with scrolling.
#foo {
height: 100px;
overflow-y; scroll;
overflow-x: hidden;
}
If you really want to hide the scrollbar, then replace it with JS or CSS for a blank image?
I'm a css noob, and though I want this DIV to resize when the window is resized, I don't want inner content to change the size of it.
Use the overflow statement. e.g.
overflow: hidden; /* all content hidden as it spills over */
overflow: auto; /* Scroll bars appear on div when required to allow moving around */
overflow: scroll; /* Scroll bars will be present at all times */
Try using:
div {
overflow: hidden;
}
Read more here.
set overflow: hidden on the containing div
Have you looked into CSS and the overflow directive? You can use this to tell the div to scroll or truncate/hide its content when the content is too large.