如何使用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。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP