The CSS reference

"text-align" - css property (v. css 1)

  • To specify the horizontal alignment of text
  • Note that alignments are relative to the width of the element, not the canvas.
  • Initial value: a nameless value that acts as left if direction is ltr, right if direction is rtl
  • The property is inherited
  • HTML usage: block-level elements, table cells and inline blocks
  • Browsers that support the text-align - property :
    Internet Explorer Mozilla Firefox Opera Google Chrome Apple Safari

Possible text-align values:

value Comments CSS
left Aligns the text in the box to the left. 1
right Aligns the in the box text to the right 1
center Centers the text in the box. 1
justify Stretches the lines so that each line has equal width in the box. 1
inherit Will inherit the value from parent specification. This property value is new with CSS 2. 2

Property "text-align" example:

<!DOCTYPE HTML >
<html  >
  <head>
    <title>Test</title>
    <style type="text/css">
      div {border: blue 1px solid; color:black;
           padding: 1em 1em 1em 1em; width: 400px;}
      .right { text-align: right;  }
      .center { text-align: center;  }
      .justify { text-align: justify;  }
    </style>
  </head>
  <body>
    <br>text-align: right;<br>
    <div class="right">To specify the horizontal alignment of text.
      Alignments are relative to the width of the element,
      not the canvas.
    </div>
    <br>text-align: center;<br>
    <div class="center">To specify the horizontal alignment of text.
      Alignments are relative to the width of the element,
      not the canvas..
    </div>
    <br>text-align: justify;<br>
    <div class="justify">To specify the horizontal alignment of text.
      Alignments are relative to the width of the element,
      not the canvas.
    </div>
  </body>
</html>

Javascript access:

// To SET values ([o] is the target object)
[o].style.textAlign="text-align value"
// To GET values you must first get the computed style object
// To get that object in IE or Opera:
var cStyle=[o].currentStyle;
// To get that object in Firefox, Chrome or Safari (w3c-type):
var cStyle=window.getComputedStyle([o],null)
// To GET the property value:;
var value=cStyle.textAlign;
/* Don not expect that alle browsers returns the right initial value when a text-align values is NOT set.*/
/* If a text-align value was SET you got that value in return*/
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.