tags:

views:

70

answers:

1

How I can align a text/pargaph both to left and right in HTML? Like in the office programs.

+7  A: 

text-align: justify; in css

but beware: unlike in proffesional DTP applications or even in word, HTML/browser often do not play nicely with this - you might end having humongous gaps in your text, completely destroying any typogtaphy you try to achieve in your document.

Edit:

As reply to Brian, my experiences are following:

  • haven't tried % for width, because I either set width in px or not at all and let display:block; element fill whole width of parent
  • text-align: justify works best with small font-size with slightly higher line-height - line-height is for readbility and smaller font size puts more characters in one line (abstract characters-per-line width), therefore gaps between words/justified text looks better
  • regarding images instead of small non changing parts of text - I tend to use as few image replacement parts as possible - I have very good results with Cufón, if you learn to count with it's cons
  • I don't advice using justified text for sidebars and one-liners - it mostly seems ugly

Edit2: The main problem is, that browsers don't know how to part your text (i.e. unbreakable to un-breakable) - soft hyphyen, ­ should solve this (link: soft hyphen overview), but browser support is wild at best :)

Adam Kiss
In regards to the having humongous gaps in text, I have found that text-align: justify; works best when you have a set font size, and a set width. If it is a percentage width, that can lead to very unexpected results. Likewise, if the font is not specified in an exact pixel value but a relative value, similarly unexpected results for various browsers can be experienced. Another solution if there is just a small area of text that doesn't change is to use an image.
Brian
@Brian My reply in answer, since it kind of touches the subject.
Adam Kiss
@Brian: +1 And of course there is the non-breaking humongous word issue that throws this (and even right- or left-aligned text containers) into a turmoil. One tries to adhere to the "alphabet-and-a-half" (i.e., approx. 40 characters) line width, which is the goal for printed matter, but that can get really ugly on the Web.
Robusto
@Adam +1 also. I hate to be a typography nerd, but I really wish more Web developers would pay attention to niceties of typography, or at least avoid the egregious errors like this one that are so often seen.
Robusto
Adam Kiss