Javascript DOM CSS Style textIndent Property

Introduction

Indent the first line of a <div> element with 50 pixels:

document.getElementById("myDiv").style.textIndent = "50px";

View in separate window

<!DOCTYPE html>
<html>
<body>

<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()">Indent text</button>

<script>
function myFunction() {// w ww  .j  a  v a2  s . c o m
  document.getElementById("myDiv").style.textIndent = "50px";
}
</script>

</body>
</html>

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

Negative values are allowed.

The first line will be indented to the left if the value is negative.

Property Values

Value Description
length Defines the indentation in length units. Default value is 0
% Defines the indentation in % of the width of the parent element
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

The textIndent property returns a String representing the indentation of the first line of text in the element.




PreviousNext

Related