Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word-break only when necessary

Tags:

javascript

css

I need to achieve the following task through CSS:
I have a div with a fix width.
In this div I need to display first name, second name and eventually others name in the following way.


example 1

---------------
First-Name 
Secondo-Name
---------------

examaple 2

---------------
Very-Long-First
-Name 
Second-Name
---------------

If I try to use the word-break: break-all; it will work for the example 2 but not for the example 1 which will be:

example 1

---------------
First-Name Seco
nd-Name
---------------

What is the best way to fix this problem just using CSS?
If it will be not possible, what is the best way to fix it by using javascript?

Here is the jsfiddle.net link

like image 447
Lorraine Bernard Avatar asked Aug 09 '12 14:08

Lorraine Bernard


2 Answers

http://jsfiddle.net/uNcCR/2/

I'm using word-wrap: break-word to allow for the names to be broken if the width of the word exceeds the width of the div, otherwise it will break in between the names. I think that's what you're looking for anyway.

like image 51
Tyler Biscoe Avatar answered Sep 21 '22 16:09

Tyler Biscoe


For very long name I don't think is a good idea to break the word displaying it on the line below.
I suggest you to use the css property -moz-hyphens: auto.
Here is the reference css-hyphenation.

like image 22
antonjs Avatar answered Sep 22 '22 16:09

antonjs