How to wrap long text inside a div without scroll - html

<div style="width:200px; margin:0 auto;"> 01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921.........
</div>
The above code is showing a long scrollbar. I want to keep it inside the div's declared width (width: 200px;) without scrolling.
I have tried float, display, position, overflow, but nothing works here.

Alternative you can use overflow-wrap: break-word;. Also avoid inline styles.
css
div{
width: 200px;
margin: 0 auto;
overflow-wrap: break-word;
word-wrap: break-word; /* Firefox support */
}
Fiddle

Try this:-
word-wrap: break-word;
DEMO: http://jsfiddle.net/dwebexperts/thyD9/

<div style="width:200px; margin:0 auto; word-wrap:break-word;"> 01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921.........
</div>
JSFiddle: http://jsfiddle.net/Emr84/

Try this:
div {
width:200px;
margin:0 auto;
white-space: pre-wrap; /* CSS3 */
white-space: -moz-pre-wrap; /* Firefox */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* IE */
}
Note: Avoid inline styling.
Demo

Try with these CSS rules
white-space: nowrap; //keep the text on one line
overflow: hidden; //prevent the text from being shown outside the border
text-overflow: ellipsis; //cut off text with an elipsis

Use text-overflow: ellipsis;.
Full explanation: http://quirksmode.org/css/user-interface/textoverflow.html

for me following worked, my div was inside a scrollable parent div, just add to your child or content div
div{
white-space: normal;
}
OR
div{
white-space: pre-wrap;
}

Related

Flexwrap not working for text inside a container [duplicate]

I have a situation where there can be long words like 'hellowordsometext' or integer like '1234567891122' without any space in between. check this js please. http://jsfiddle.net/rzq5e/6/
how is it possible to break it in to next line after it reach the div width. what happens now is, it spans out out along with th div
<div>Solutionforentprise</div>
What you need is word-wrap: break-word;, this property will force the non spaced string to break inside the div
Demo
div {
width: 20px;
word-wrap: break-word;
}
I have found this solution my self.
word-break: break-all;
but it doesn't work for Opera.
http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_word-break
The other answers have some problems with old browsers, like before Chrome 32.
Its better to use this code:
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-word;
word-break: break-word;
You can also add a hyphen where the word breaks (if supported):
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
Source
.c1 {
width: 200px;
border: 1px solid;
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-word;
word-break: break-word;
/* Adds a hyphen where the word breaks, if supported (No Blink) */
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
<div class="c1">
For more information, please visit: http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.
</div>
give an id or class to your div
then
#divid
{
width: 30px;
word-wrap: break-word;
}
Word-wrap is supported in IE 5.5+, Firefox 3.5+, and WebKit browsers such as Chrome and Safari, Opera 10.5+.
Like this
DEMO
CSS
div {
width: 20px;
word-wrap:break-word;
}
Fix the size of the DIV and apply 'overflow: hidden'
so it will not effect the grid size an all na.
div{width: 40px;
overflow: hidden;}
do you need to view entire text?

How to make LI elements wrap in UL? [duplicate]

I have a situation where there can be long words like 'hellowordsometext' or integer like '1234567891122' without any space in between. check this js please. http://jsfiddle.net/rzq5e/6/
how is it possible to break it in to next line after it reach the div width. what happens now is, it spans out out along with th div
<div>Solutionforentprise</div>
What you need is word-wrap: break-word;, this property will force the non spaced string to break inside the div
Demo
div {
width: 20px;
word-wrap: break-word;
}
I have found this solution my self.
word-break: break-all;
but it doesn't work for Opera.
http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_word-break
The other answers have some problems with old browsers, like before Chrome 32.
Its better to use this code:
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-word;
word-break: break-word;
You can also add a hyphen where the word breaks (if supported):
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
Source
.c1 {
width: 200px;
border: 1px solid;
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-word;
word-break: break-word;
/* Adds a hyphen where the word breaks, if supported (No Blink) */
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
<div class="c1">
For more information, please visit: http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.
</div>
give an id or class to your div
then
#divid
{
width: 30px;
word-wrap: break-word;
}
Word-wrap is supported in IE 5.5+, Firefox 3.5+, and WebKit browsers such as Chrome and Safari, Opera 10.5+.
Like this
DEMO
CSS
div {
width: 20px;
word-wrap:break-word;
}
Fix the size of the DIV and apply 'overflow: hidden'
so it will not effect the grid size an all na.
div{width: 40px;
overflow: hidden;}
do you need to view entire text?

How to make html elements scale correctly, when dealing with long text [duplicate]

I have a situation where there can be long words like 'hellowordsometext' or integer like '1234567891122' without any space in between. check this js please. http://jsfiddle.net/rzq5e/6/
how is it possible to break it in to next line after it reach the div width. what happens now is, it spans out out along with th div
<div>Solutionforentprise</div>
What you need is word-wrap: break-word;, this property will force the non spaced string to break inside the div
Demo
div {
width: 20px;
word-wrap: break-word;
}
I have found this solution my self.
word-break: break-all;
but it doesn't work for Opera.
http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_word-break
The other answers have some problems with old browsers, like before Chrome 32.
Its better to use this code:
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-word;
word-break: break-word;
You can also add a hyphen where the word breaks (if supported):
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
Source
.c1 {
width: 200px;
border: 1px solid;
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-word;
word-break: break-word;
/* Adds a hyphen where the word breaks, if supported (No Blink) */
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
<div class="c1">
For more information, please visit: http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.
</div>
give an id or class to your div
then
#divid
{
width: 30px;
word-wrap: break-word;
}
Word-wrap is supported in IE 5.5+, Firefox 3.5+, and WebKit browsers such as Chrome and Safari, Opera 10.5+.
Like this
DEMO
CSS
div {
width: 20px;
word-wrap:break-word;
}
Fix the size of the DIV and apply 'overflow: hidden'
so it will not effect the grid size an all na.
div{width: 40px;
overflow: hidden;}
do you need to view entire text?

CSS word-wrap: break-word not wrapping a tag unless you wrap it in div and add rule there

why does CSS rule
a {
word-wrap: break-word;
}
with
<div>
verylongurlherewithnospaces
</div>
not wrapping and causing window to show scrollbar, whereas
div {
word-wrap: break-word;
}
will do the wrapping at its anchor child's text fine?
UPDATE:
just noticed (see L3ST-instance URL field at this form when you resize the window) that I needed word-break:break-all instead of word-wrap:break-word, apart from the suggested display:inline-block, so now using:
a
{
word-break: break-all !important; /* make sure containers don't override */
display: inline-block !important;
}
which works fine
The CSS word-wrap: break-word; works only in display:block; or display:inline-block; elements so you can just use:
a {
display:inline-block;
word-wrap: break-word;
}
a {
width:100px;
word-wrap: break-word;
display:inline-block;
}
<div>
verylongurlherewithnospaces
</div>
P.S. div are display:block; as default by user-agent.

Prevent newline in div

If I were to have a div with a specified width, how would I prevent a new line when the content exceeds the parent div's width?
Here is my HTML code:
<div style="width: 200"><p>Verrrrrrry loooooooong pppppppp ellllemmmmmmmmentttt</p></div>
Any help would be appreciated.
Thanks!
You could simply use:
div {
white-space: nowrap;
overflow: hidden; /* assuming you don't want the <div> to stretch to accommodate */
} /* the width of the text */
div {
width: 300px;
white-space: nowrap;
overflow: hidden;
border: 2px solid rgba(0,0,0,0.5);
}
<div>This element has some text longer, by far, than the width of the page in which it is contained; it would be lorem ipsum but, honestly, I'd rather spend a few moments typing randomly, but continuously, than go find myself some of that lorem-goodness...</div>
References:
white-space (MDN).
white-space (W3.org, CSS 2.1).
Use white-space: nowrap:
div{
overflow: hidden; /* Prevent text from overflowing div */
white-space: nowrap; /* Prevent text from using more than one line */
}
Try this CSS:
overflow:hidden;
white-space:nowrap;
Should do the trick