/

HTML / CSS / JavaScript Tutorial

CSS Property: text-align

[this page | pdf | back links]

The CSS (CSS1) text-align property indicates how text in an element should be aligned.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

center

Centres the text

justify

Justifies text, i.e. stretches lines to encompass whole width. The precise way justification then works is set by the text-justify property.

left

Aligns text to left

right

Aligns text to right

 

Default Value:

left if direction is ltr (left-to-right), right if direction is rtl (right-to-left)

JavaScript syntax:

e.g. object.style.textAlign="justify"

Inherited:

Yes

Animatable:

No

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
div {width: 300px; border: 1px solid black;}
div.x1 {text-align: center;}
</style>
</head>
<body>
Note: not all browsers seem to support this property effectively for some element types<br><br>
1. Element with default property<br>
<div>Some text that is longer than one line, so has multiple lines that illustrate different text alignments</div><br><br>
2. Element set using in-file HTML style
<div class="x1">Some text that is longer than one line, so has multiple lines that illustrate different text alignments</div><br><br>
3. Element set using JavaScript
<div id="x2">Some text that is longer than one line, so has multiple lines that illustrate different text alignments</div>

<script>
document.getElementById("x2").style.columnSpan = "all";
document.getElementById("x2").style.textAlign = "center";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyTextAlign() {
  var x = document.createElement("DIV"); x.style.textAlign = "center"; return (window.getComputedStyle(x, null).textAlign == "center");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile