HTML - DOM样式对象textIndent属性



HTML DOM 样式对象**textIndent**属性用于设置或返回文本第一行的缩进。它也接受负值,其中第一行将向左缩进。

语法

设置 textIndent 属性
object.style.textIndent= "length | percentage | initial | inherit";
获取 textIndent 属性
object.style.textIndent;

属性值

描述
长度 它以长度单位指定缩进。其默认值为 0。
百分比 它以父元素宽度的百分比指定缩进值。
initial 用于将此属性设置为其默认值。
inherit 用于继承其父元素的属性。

返回值

它返回一个字符串值,表示元素中第一行的缩进。

HTML DOM 样式对象“textIndent”属性示例

以下示例说明了应用于 div 元素的文本缩进属性。

使用“长度”和“百分比”设置文本缩进

以下示例使用长度和百分比值设置 div 元素的缩进。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object textIndent Property
    </title>
    <style>
        div {
            width: 250px;
        }
    </style>
</head>
<body>
    <p>
        Click to change text indentation.
    </p>
    <button onclick="fun()">Length</button>
    <button onclick="funTwo()">Percentage</button>
    <div id="indent">
        <p>
            JavaScript is a lightweight, interpreted
            programming language. It is commonly used
            to create dynamic and interactive elements
            in web applications.
        </p>
        <p>
            JavaScript is very easy
            to implement because it is integrated with
            HTML. It is open and cross-platform.
        </p>
        <p>
            This JavaScript tutorial has been designed
            for beginners as well as working professionals
            to help them understand the basic to advanced
            concepts and functionalities of JavaScript.
        </p>
        <p>
            It covers most of the important concepts
            related to JavaScript such as operators, control
            flow, functions, objects, OOPs, Asynchronous
            JavaScript, Events, DOM manipulation and much more.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("indent")
                .style.textIndent = "40px";
        }
        function funTwo() {
            document.getElementById("indent")
                .style.textIndent = "5%";
        }
    </script>
</body>
</html>

使用负值设置文本缩进

以下示例设置 div 元素的负缩进,使文本向左缩进。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object textIndent Property
    </title>
    <style>
        div {
            width: 250px;
        }
    </style>
</head>
<body>
    <p>
        Click to change text indentation.
    </p>
    <button onclick="fun()">Change</button>
    <div id="indent">
        <p>
            JavaScript is a lightweight, interpreted
            programming language. It is commonly used
            to create dynamic and interactive elements
            in web applications.
        </p>
        <p>
            JavaScript is very easy
            to implement because it is integrated with
            HTML. It is open and cross-platform.
        </p>
        <p>
            This JavaScript tutorial has been designed
            for beginners as well as working professionals
            to help them understand the basic to advanced
            concepts and functionalities of JavaScript.
        </p>
        <p>
            It covers most of the important concepts
            related to JavaScript such as operators, control
            flow, functions, objects, OOPs, Asynchronous
            JavaScript, Events, DOM manipulation and much more.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("indent")
                .style.textIndent = "-40px";
        }
    </script>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
textIndent 是 1 是 12 是 1 是 1 是 3.5
html_dom_style_object_reference.htm
广告