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
廣告