Javascript Reference - HTML DOM Style textIndent Property








The textIndent property sets or gets the indentation of the first line of text.

Browser Support

textIndent Yes Yes Yes Yes Yes

Syntax

Return the textIndent property:

var v = object.style.textIndent 

Set the textIndent property:

object.style.textIndent='length|%|initial|inherit

Property Values

Value Description
length Default to 0. Set the indentation in length units.
% Set the indentation in percent of the width of the container element
initial Set to default value.
inherit Inherit from its parent element.




Technical Details

Default Value: 0
Return Value: A string representing the indentation of the first line
CSS Version CSS1

Example

The following code shows how to indent the first line with 50 pixels.


<!DOCTYPE html>
<html>
<body>
<!--  w ww .  j  a  va 2s. c  om-->
<div id="myDiv">
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</div><br>

<button type="button" onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myDiv").style.textIndent = "50px";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the text indentation.


<!DOCTYPE html>
<html>
<body>
<!--  w w  w .j a  va2s  .  c o  m-->
<div id="myDiv" style="text-indent:3cm;">
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</div><br>

<button type="button" onclick="myFunction()">test</button>

<script>
function myFunction() {
    console.log(document.getElementById("myDiv").style.textIndent);
}
</script>

</body>
</html>

The code above is rendered as follows: