如何使用li元素建立專案符號?


<li> 元素用於在有序列表 (<ol>) 或無序列表 (<ul>) 中定義列表項。<li> 元素代表“列表項”。專案符號通常僅用於無序列表。在 HTML 中,無序列表使用 <ul> 元素建立,每個列表項都使用 <li> 元素定義。

語法

ul {
   list-style-type: disc;
}
<ul>
   <li>Items…</li>
</ul>

list-style-type 屬性允許我們指定將與每個 <li> 元素一起顯示的標記型別,例如專案符號、方塊、圓圈、數字或字母。

無序列表 (</ul>) 的預設專案符號樣式為 Disc(實心圓點)。

示例 1

在此示例中,我們將為無序列表建立預設樣式的實心圓點專案符號,並演示使用 <li> 元素建立巢狀列表的專案符號。

演算法

  • 上傳包含頁面 CSS 樣式的樣式部分。

  • 根據需要設定列表和列表項的樣式,包括將 list-style-type 屬性設定為“disc”以建立專案符號。

  • 使用 (<li>) 元素新增一個無序列表,其中包含一些代表不同水果的列表項。

  • 建立一個巢狀列表,其中包含水果、蔬菜和飲料等食物專案,使用 <li> 元素和預設專案符號樣式 (disc)。

<!DOCTYPE html>
<html>
<head>
   <title>(Example)Create Bullets List using li elements</title>
   <!-- CSS styling for the document -->
   <style>
      body {
         background-color: #f2f2f2;
         font-family: Arial, sans-serif;
      }
      h2 {
         color: #333;
         font-size: 24px;
         margin-bottom: 10px;
      }
      ul {
         list-style-type: disc; /* specifies disc as the list item marker */
         margin-left: 20px;
         margin-bottom: 20px;
      }
      li {
         color: #666;
         font-size: 18px;
         margin-bottom: 5px;
      }
   </style>
</head>
<body>
   <h2>Fruits List</h2>
   <!-- Unordered List of Fruits -->
   <ul>
      <li>Apple</li>
      <li>Banana</li>
      <li>Orange</li>
   </ul>
   <h2>Nested List</h2>
   <!-- Nested List Example -->
   <ul>
      <!-- First level item -->
      <li>Food</li>
      <!-- Second level with Fruits and Vegetables -->
      <ul>
         <!-- Third level unordered list of Fruits -->
         <li>Fruits</li>
         <ul>
            <li>Apple</li>
            <li>Banana</li>
            <li>Orange</li>
         </ul>
         <!-- Third level unordered list of Vegetables -->
         <li>Vegetables</li>
         <ul>
            <li>Carrot</li>
            <li>Broccoli</li>
            <li>Spinach</li>
         </ul>
      </ul>
      <!-- Second level unordered list of Drinks -->
      <li>Drinks</li>
      <ul>
         <li>Water</li>
         <li>Juice</li>
         <li>Soda</li>
      </ul>
   </ul>
</body>
</html>

示例 2

在這裡,我們將使用 list-style-type 屬性構建多個專案符號樣式,並將其分配給我們的專案列表。

演算法

  • 使用 CSS 為無序列表定義三種獨特的樣式。將 list-style-type 設定為“circle”(實心圓)、“square”(實心方塊)或“disc”(實心圓點),然後為每個樣式指定顏色。

  • 使用 ul 選擇器元素建立一個無序列表。

  • 根據所需樣式,將每個列表的 class 屬性設定為“circle”、“square”或“disc”。

  • 在每個 ul 元素中使用 li 元素建立多個列表項。每個 li 元素代表一個列表項。

<!DOCTYPE html>
<html>
<head>
   <title>Creating Bullets Using li Elements</title>
   <style>
      /* Set left margin of all unordered lists to 30px */
      ul {
         margin-left: 30px; 
      }
      /* Use circle to create circle bullets and set the colour to red */
      .circle {
         list-style-type: circle; 
         color: red; 
      }
      /* Use square to create square bullets and set the colour to green */
      .square {
         list-style-type: square; 
         color: green; 
      }
      /* Use disc to create disc bullets and set the colour to blue */
      .disc {
         list-style-type: disc; 
         color: blue; 
      }
   </style>
</head>
<body>
   <!-- Create a heading for the page -->
   <h1>Creating Bullets Using li Elements</h1>
   <!-- Create an unordered list with circle bullets and items related to beverages -->
   <h2>List of beverages</h2>
   <ul class="circle">
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Orange Juice</li>
      <li>Water</li>
   </ul>
   <!-- Create an unordered list with square bullets and items related to programming languages -->
   <h2>Different Programming Languages</h2>
   <ul class="square">
      <li>JavaScript</li>
      <li>Python</li>
      <li>Java</li>
      <li>C++</li>
      <li>PHP</li>
   </ul>
   <!-- Create an unordered list with disc bullets and items related to cities -->
   <h2>Popular cities</h2>
   <ul class="disc">
      <li>New York</li>
      <li>London</li>
      <li>Paris</li>
      <li>Tokyo</li>
      <li>Sydney</li>
   </ul>
</body>
</html>

結論

專案符號是一種靈活且強大的方式,可以以易於學習和理解的方式呈現資訊。專案符號的一些常見用例包括:列出功能、概述步驟、呈現選項、總結關鍵要素和建立列表。

Unicode 字元:這是一種字元編碼形式,允許使用大量特殊字元和符號。

CSS 偽元素:它允許我們建立可以新增到 HTML 元素的裝飾性元素,我們使用 ‘::before’ 或 ‘::after’ 偽元素向列表新增自定義專案符號。

JavaScript 庫:有許多 JavaScript 庫可用於建立自定義專案符號和其他視覺效果。一些流行的庫包括 jQuery、D3.js 和 Snap.svg。

更新於:2023年8月31日

瀏覽量:160

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告