23

I am using Bootstrap. Let us say I have text that looks like this:

"Stackoverflow"

Can I somehow automatically make sure that it breaks like this if the text does not fit the container:

"Stack
-overflow"

instead of:

"Stackov
-erflow"

with HTML/CSS?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Obsivus
  • 8,231
  • 13
  • 52
  • 97
  • i dont think theres any option for this – Keyur Sakaria Apr 22 '16 at 11:18
  • 5
    Doesn't it have to be `Stack-` and `overflow`? In Dutch that's the correct way, don't know in English btw... – Jeroen Bellemans Apr 22 '16 at 11:21
  • 9
    Might be worth noting that if you're writing the name of SO in a document, it is actually named `Stack Overflow` (with a space), not `Stackoverflow` ;) – Kai Apr 22 '16 at 13:06
  • possible duplicate of [In HTML, how to word-break on a dash?](http://stackoverflow.com/q/904/1048572) – Bergi Apr 22 '16 at 13:35
  • For a start, the site is called "Stack Overflow", not "Stackoverflow", so you would simply break at the space. – Lightness Races in Orbit Apr 22 '16 at 14:57
  • 6
    Can we just assume that this is an example of a compound word, not the actual text, instead of telling OP that the problem is they're spelling Stack Overflow wrong? As a general question, I can see this being helpful to others. – Darrick Herwehe Apr 22 '16 at 19:49

5 Answers5

46

You can insert ­ (soft hyphen)

#box {
  font-size:14px;
  font-family:courier;
  width:90px;
  background:yellow;
 }
<div id="box">Stack&shy;overflow</div>
jayms
  • 3,828
  • 2
  • 11
  • 18
  • 16
    If you can't use `­` because you're not using some variant of (X)HTML (XML, Markdown, plain text…) you can use the equivalent Unicode character U+00AD (`­` in XML). – David Foerster Apr 22 '16 at 14:25
21

You can use the <wbr> tag:

p {
  width: 60px;
  border: 1px solid black;
}
<p>Stack<wbr>overflow</p>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
9

No, for a document "Stackoverflow" is a single word. If you are using a CSS property to break the word then it will break according to the space available.

You have to define it if you want to break it from a particular point.

You can use Stack<wbr>overflow or Stack-overflow.

If use Stack<wbr>overflow, then it will be broken at <wbr>.

p.test1 {
    width: 60px;
    border: 1px solid #000000;
}
<p class="test1">Stack<wbr>overflow</p>

OR, if using Stack-overflow and using CSS property word-break: keep-all; then it will be broken at hyphens:

p.test1 {
    width: 60px;
    border: 1px solid #000000;
    word-break: keep-all;
}
<p class="test1">Stack-overflow</p>

But you have to define one of them.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gaurav Aggarwal
  • 9,809
  • 6
  • 36
  • 74
4

If you want to break a specific word, then the answers given will work - insert an invisible break of some kind. It does, as pointed out, harm search results, but not so bad as you'd think.

If you want to break all compound terms that need wrapping, without manually going through and checking, then there is no way to do this, because you need context-sensitive English language parsing, which means you need a human brain doing it.

Consider how you would break the following strings:

lowrateslender.com
musictoyourears.com
heresearch.com
nakedsnow.com
picturespain.com
catspaying.net
attackshoes.com
williamsharp.net
andreward.net
goalsnow.net
hesstruck.net
artistrap.com
buildingskill.com
bearspray.com
atticsweep.com
matthewhale.com
singershaven.com
shoesworn.com
sinuscatscan.com
cometart.com
monkeyslap.com
scriptshack.com
nasalbum.com
moviesick.com
arizonasparesort.com
privateshot.com
nationsprinting.com
importcarshow.com

(copy-pasted from a google search for "ambiguous domain names")

...even given a dictionary to identify whole words within these terms, and inserting breaking spaces at those boundaries, it'd be a toss-up whether you would split these words as, say, "import-car-show" or "import-cars-how".

One of the main skills for programming is identifying algorithms which require natural language processing, and opting just for a "good enough" algorithm rather than wasting time on it. This is one such case.

Dewi Morgan
  • 1,143
  • 20
  • 31
2

You can use zero-width space, &#8203;. Just type Stack&#8203;overflow in HTML.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yuriy Yakym
  • 3,616
  • 17
  • 30