2

I have this div:

<div class="text">
    Thisismytextwontgoinanewparagraph
</div>

with this CSS:

.text
{
    background-color:red;
    width:100px;
}

As you can see in this JSFiddle, the text goes outside of the div.

Is there a way in CSS to force wrapping for a text without spaces (such as a hyperlink)?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

5

you could try this, but it's not very pretty:

.text
{
    background-color:red;
    width:100px;
    word-wrap: break-word;
}

http://jsfiddle.net/esGEn/1/

dax
  • 10,779
  • 8
  • 51
  • 86
  • The code breaks the string at arbitrary points. This is seldom acceptable, and surely not for URLs (which is what the question probably means by “such as a hyperlink”). – Jukka K. Korpela Oct 07 '13 at 14:12
  • thus, " but it's not very pretty" – dax Oct 07 '13 at 14:13
  • You can make certain elements unbreakable with `white-space:nowrap`, like in this fiddle: http://jsfiddle.net/keaukraine/JsNgQ/ – keaukraine Oct 07 '13 at 14:17
0

Why not just use ellipsis with overflow hidden? That will also look pretty.

.text {
  background-color: red;
  width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
}

enter image description here

Md Adilur Rashid
  • 700
  • 7
  • 13
-1

Here is how I did it. I added the <br> tag to the text I wanted to push to the next line. <p> Sleek custom designs and durable functionality. <br>Let your headwear work for you. </p> text multiple lines example

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '22 at 11:37