如何使用 CSS 移除無序列表項的縮排?


為了使用 CSS 移除無序列表項的縮排,我們將瞭解各種方法。縮排是常用的功能,用於為列表項提供視覺層次結構。在本文中,我們將介紹五種不同的方法來使用 CSS 移除無序列表項的縮排。

我們有一個包含不同程式語言的無序列表,我們的任務是使用CSS移除無序列表的縮排。

移除無序列表項縮排的方法

以下是本文將討論的使用 CSS 移除無序列表項縮排的方法,我們將逐步解釋並提供完整的示例程式碼。

使用填充移除縮排

在這種移除無序列表項縮排的方法中,我們使用了 CSS 的padding 屬性。我們也可以使用 CSS 的padding-left 屬性來移除縮排。

  • 首先,我們使用ul標籤建立了一個無序列表,並使用li標籤添加了列表項。
  • 我們使用了 "list-style-type: none;" 屬性來移除無序列表的預設標記。
  • 移除預設標記後,我們使用了 "padding: 0;",它會移除瀏覽器對 ul 元素應用的預設填充。
  • 我們也可以使用 "padding-left:0;" 屬性,它與 padding:0; 的效果相同。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 padding 屬性移除無序列表項的縮排。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        Remove indentation from an unordered list item
        using CSS
    </title>
    <style>
        ul {
            list-style-type: none;
            padding: 0;
        }
    </style>
</head>
<body>
    <h3>
        Remove Indentation from an Unordered List Item
        Using CSS
    </h3>
    <p>
        In this example we have used CSS <strong>padding 
        </strong> property to remove indentation from an 
        unordered list item using CSS.
    </p>
    <ul>
        <strong>Programming Languages:</strong>
        <li>C++</li>
        <li>Java</li>
        <li>Python</li>
        <li>C</li>
    </ul>
</body>
</html>

使用 display 屬性移除縮排

在這種方法中,我們使用了display 屬性,它使用 contents 值使 ul 元素消失。

  • 我們使用了 "list-style-type: none;" 屬性來移除無序列表的預設標記。
  • 移除預設標記後,我們使用了 "display: contents;" 屬性,它使 ul 元素消失,但列表項保留為 HTML 文件的子元素,從而移除縮排。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 display 屬性移除無序列表項的縮排。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        Remove indentation from an unordered list item
        using CSS
    </title>
    <style>
        ul {
            display: contents; 
            list-style-type: none; 
        }
    </style>
</head>
<body>
    <h3>
        Remove Indentation from an Unordered List Item
        Using CSS
    </h3>
    <p>
        In this example we have used CSS <strong>display:
        contents</strong> property to remove indentation 
        from an unordered list item using CSS.
    </p>
    <ul>
        <strong>Programming Languages:</strong>
        <li>C++</li>
        <li>Java</li>
        <li>Python</li>
        <li>C</li>
    </ul>
</body>
</html>

使用 translate() 函式移除縮排

在這種方法中,我們使用了translate() 函式,它是transform 屬性的一部分,可用於透過指定的值將任何元素移動到不同的位置。

  • 我們使用了 "list-style-type: none;" 屬性來移除無序列表的預設標記。
  • 我們使用了 "transform: translate(-40px);" 屬性,它將列表項向左移動 40px。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 translate() 方法移除無序列表項的縮排。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        Remove indentation from an unordered list item
        using CSS
    </title>
    <style>
        ul {
            list-style-type: none; 
            transform: translate(-40px); 
        }
        li {
            display: block; 
        }
    </style>
</head>
<body>
    <h3>
        Remove Indentation from an Unordered List Item
        Using CSS
    </h3>
    <p>
        In this example we have used CSS <strong>translate()
        </strong> function to remove indentation from
        an unordered list item using CSS.
    </p>
    <ul>
        <strong>Programming Languages:</strong>
        <li>C++</li>
        <li>Java</li>
        <li>Python</li>
        <li>C</li>
    </ul>
</body>
</html>

使用 margin-left 屬性移除縮排

在這種移除無序列表項縮排的方法中,我們使用了 CSS 的margin-left 屬性,它設定元素的左側邊距空間。

  • 我們使用了 "list-style-type: none;" 屬性來移除無序列表的預設標記。
  • 移除預設標記後,我們使用了 "margin-left:-40px;",它將列表項向左移動 40px,從而移除縮排。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 CSS 的 margin-left 屬性移除無序列表項的縮排。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        Remove indentation from an unordered list item
        using CSS
    </title>
    <style>
        ul {
            list-style-type: none; 
            margin-left:-40px;
        }
    </style>
</head>
<body>
    <h3>
        Remove Indentation from an Unordered List Item
        Using CSS
    </h3>
    <p>
        In this example we have used CSS <strong>margin-
        left</strong> property to remove indentation from
        an unordered list item using CSS.
    </p>
    <ul>
        <strong>Programming Languages:</strong>
        <li>C++</li>
        <li>Java</li>
        <li>Python</li>
        <li>C</li>
    </ul>
</body>
</html>

使用 left 屬性移除縮排

在這種方法中,我們使用了 CSS 的left 屬性,它指定了已定位元素相對於其包含元素的水平位置。這裡我們將其position 設定為相對。

  • 我們使用了 "list-style-type: none;" 屬性來移除無序列表的預設標記。
  • 移除預設標記後,我們使用了 "left:-40px;",它將列表項向左移動 40px,從而移除縮排。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 CSS 的 left 屬性移除無序列表項的縮排。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        Remove indentation from an unordered list item
        using CSS
    </title>
    <style>
        ul {
            list-style-type: none;
        }
        li {
            position: relative;
            left: -40px;
        }
    </style>
</head>
<body>
    <h3>
        Remove Indentation from an Unordered List Item
        Using CSS
    </h3>
    <p>
        In this example we have used CSS <strong>left
        </strong> property to remove indentation from
        an unordered list item using CSS.
    </p>
    <strong>Programming Languages:</strong>
    <ul>
        <li>C++</li>
        <li>Java</li>
        <li>Python</li>
        <li>C</li>
    </ul>
</body>
</html>

結論

在本文中,為了使用 CSS 移除無序列表項的縮排,我們使用了五種不同的方法,分別是:使用 padding 屬性、display 屬性、transform 屬性的 translate() 方法、margin-left 屬性和 left 屬性。任何方法都可以使用,但 padding 是最常用的方法。

更新於: 2024-08-13

6K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告