CSS word-wrapping in div

CssHtmlWord Wrap

Css Problem Overview


I have a div with a width of 250px. When the innertext is wider than that i want it to break down. The div is float: left and now has an overflow. I want the scrollbar to go away by using word-wrapping. How can i achieve this?

<div id="Treeview">
<div id="HandboekBox">
    <div id="HandboekTitel">
        <asp:Label ID="lblManual" runat="server"></asp:Label>
    </div>
    <div id="HandboekClose">
        <asp:ImageButton ID="btnCloseManual" runat="server" 
            ImageUrl="Graphics/close.png" onclick="btnCloseManual_Click" 
            BorderWidth="0" ToolTip="Sluit handboek" />
    </div>
</div>
<asp:TreeView ID="tvManual" runat="server" RootNodeStyle-CssClass="RootNode">
    <Nodes>
        
    </Nodes>
</asp:TreeView>
</div>

CSS:

#Treeview
{
padding-right: 5px;
width: 250px;
height: 100%;
float: left;
border-right: solid 1px black;
overflow-x: scroll;
}

Css Solutions


Solution 1 - Css

As Andrew said, your text should be doing just that.

There is one instance that I can think of that will behave in the manner you suggest, and that is if you have the whitespace property set.

See if you don't have the following in your CSS somewhere:

white-space: nowrap

That will cause text to continue on the same line until interrupted by a line break.

OK, my apologies, not sure if edited or added the mark-up afterwards (didn't see it at first).

The overflow-x property is what's causing the scroll bar to appear. Remove that and the div will adjust to as high as it needs to be to contain all your text.

Solution 2 - Css

Or simply use

word-wrap: break-word;

supported in IE 5.5+, Firefox 3.5+, and WebKit browsers such as Chrome and Safari.

Solution 3 - Css

try white-space:normal; This will override inheriting white-space:nowrap;

Solution 4 - Css

It's pretty hard to say definitively without seeing what the rendered html looks like and what styles are being applied to the elements within the treeview div, but the thing that jumps out at me right away is the

overflow-x: scroll;

What happens if you remove that?

Solution 5 - Css

you can use:

overflow-x: auto; 

If you set 'auto' in overflow-x, scroll will appear only when inner size is biggest that DIV area

Solution 6 - Css

I'm a little surprised it doesn't just do that. Could there another element inside the div that has a width set to something greater than 250?

Solution 7 - Css

Setting just the width and float css properties would get a wrapping panel. The folowing example work just fine:

<div style="float:left; width: 250px">
Pellentesque feugiat tempor elit. Ut mollis lacinia quam. 
Sed pharetra, augue aliquam   ornare vestibulum, metus massa
laoreet tellus, eget iaculis lacus ipsum et diam. 
</div>

Maybe there are other styles in place that modify the appearance?

Solution 8 - Css

I found that word-wrap: anywhere worked (as opposed to word-wrap: break-word mentioned in another answer).

See also:

https://stackoverflow.com/questions/56304531/what-does-anywhere-mean-in-word-wrap-css-property

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartijnView Question on Stackoverflow
Solution 1 - CssJayxView Answer on Stackoverflow
Solution 2 - CssJc FigueroaView Answer on Stackoverflow
Solution 3 - CssgordonView Answer on Stackoverflow
Solution 4 - CssAndrewView Answer on Stackoverflow
Solution 5 - CssYamil BendekView Answer on Stackoverflow
Solution 6 - CssAndrewView Answer on Stackoverflow
Solution 7 - CssAlerisView Answer on Stackoverflow
Solution 8 - Cssatomh33lsView Answer on Stackoverflow