英文:
How to add an icon next to a list item in HTML
问题
如何使用Font Awesome或其他方式向列表项右侧添加图标,如所示?
我有4个文本部分在右侧,我想将它们转换为可展开的下拉列表。我尝试添加一个指向下方的箭头(<i class="fa-light fa-arrow-down"></i>
)来自Font Awesome,但是我无法让箭头显示出来。
.container-fluid {
display: flex;
align-items: center;
justify-content: space-between;
height: 30vw;
background-image: url(pictures/output-onlinepngtools.png);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
word-break: keep-all;
border: 2px solid rgb(68, 115, 216);
padding-left: 20px;
padding-right: 100px;
}
.container-fluid h1 {
margin: 0;
line-height: calc(30vw / 3);
}
.sec2drop li {
list-style: none;
border-top: 1px solid rgb(161, 159, 159);
line-height: 50px;
}
<div class="container-fluid">
<div class="sec3">
<ul class="sec2drop">
<li>
<i class="fa-light fa-arrow-down"></i> 准备参加语言考试
</li>
<li>儿童匈牙利语</li>
<li>迎接挑战</li>
<li>翻译服务</li>
</ul>
</div>
</div>
英文:
How do I add an icon using Font Awesome, or other means, to a list item on the right, as shown?
I have 4 sections of text to the right, and I would like to convert them into an expandable dropdown. I am trying to add an arrow that points down (<i class="fa-light fa-arrow-down"></i>
) from Font Awesome, however, I cannot get the arrow to appear.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.container-fluid {
display: flex;
align-items: center;
justify-content: space-between;
height: 30vw;
background-image: url(pictures/output-onlinepngtools.png);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
word-break: keep-all;
border: 2px solid rgb(68, 115, 216);
padding-left: 20px;
padding-right: 100px;
}
.container-fluid h1 {
margin: 0;
line-height: calc(30vw / 3);
}
.sec2drop li {
list-style: none;
border-top: 1px solid rgb(161, 159, 159);
line-height: 50px;
}
<!-- language: lang-html -->
<div class="container-fluid">
<div class="sec3">
<ul class="sec2drop">
<li>
<i class="fa-light fa-arrow-down"></i> Prepare for a language exam
</li>
<li>Hungarian for kids</li>
<li>Take on a challenge</li>
<li>Translation services</li>
</ul>
</div>
</div>
<!-- end snippet -->
答案1
得分: 1
请检查您的Font Awesome CSS文件路径以及在CSS文件中链接的字体样式。
然后复制并粘贴以下代码:
<div class="container-fluid">
<div class="sec3">
<ul class="sec2drop">
<li>
准备语言考试 <i class="fa fa-caret-down"></i>
</li>
<li>儿童匈牙利语</li>
<li>迎接挑战</li>
<li>翻译服务</li>
</ul>
</div>
</div>
请注意,代码部分不会进行翻译。
英文:
Please check your font-awesome css file path and font-faces that linked in css file.
Then copy and paste this code:
<div class="container-fluid">
<div class="sec3">
<ul class="sec2drop">
<li>
Prepare for a language exam <i class="fa fa-caret-down"></i>
</li>
<li>Hungarian for kids</li>
<li>Take on a challenge</li>
<li>Translation services</li>
</ul>
</div>
</div>
答案2
得分: 0
以下是代码的翻译部分:
我的第一个建议是避免使用图标字体,而是简单地使用原生的HTML元素,然后使用CSS进行适当的样式设置,如下所示:
ol, ul, li {
/* 从元素中移除默认的列表标记: */
list-style-type: none;
}
<div class="container-fluid">
<div class="sec3">
<ul class="sec2drop">
<li>
<details>
<summary>准备语言考试</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>儿童匈牙利语</summary>
<p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>迎接挑战</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>翻译服务</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
</ul>
</div>
</div>
这当然可以进一步进行样式设置:
ol, ul, li {
/* 移除默认的列表样式: */
list-style-type: none;
}
/* 样式化所有不是其父元素的:first-child的<li>元素,添加在其块轴的起始边缘上的边框: */
li:not(:first-child) {
border-block-start: 1px solid #ccc;
}
details::marker {
/* 移除::marker伪元素的默认内容: */
content: '';
}
summary {
/* 使用光标来暗示可交互性: */
cursor: pointer;
/* 使用弹性布局来在<summary>元素的文本和其::before伪元素之间添加间距: */
display: flex;
gap: 0.25em;
}
details summary::before {
/* 使用向下的三角形作为内容: */
content: 'BE';
background-color: transparent;
}
details[open] summary::before {
/* 当父<details>处于打开状态时,即具有"open"属性时,使用向上的三角形作为内容: */
content: 'B4';
}
希望这对你有帮助!
英文:
My first suggestion would be to avoid the use of icon fonts and simply use native HTML elements where they exist, styled appropriately with CSS, as shown below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
ol,
ul,
li {
/* removing default list-markers from the elements: */
list-style-type: none;
}
<!-- language: lang-html -->
<div class="container-fluid">
<div class="sec3">
<ul class="sec2drop">
<li>
<details>
<summary>Prepare for a language exam</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>Hungarian for kids</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>Take on a challenge</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>Translation services</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
</ul>
</div>
</div>
<!-- end snippet -->
This can, of course, be further styled:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
ol,
ul,
li {
/* removing default list-styles: */
list-style-type: none;
}
/* styling all <li> elements which are not the
:first-child of their parent with a border
on the starting-edge of their block-axis: */
li:not(:first-child) {
border-block-start: 1px solid #ccc;
}
details ::marker {
/* removing default content from the ::marker
pseudo-elements: */
content: '';
}
summary {
/* using the cursor to imply interaction is possible: */
cursor: pointer;
/* using flex layout to place a gap between the <summary>
element's text and its ::before pseudo-element: */
display: flex;
gap: 0.25em;
}
details summary::before {
/* using the downward-pointing triangle as the content: */
content: 'BE';
background-color: transparent;
}
details[open] summary::before {
/* using the upward-pointing triangle as the content
when the parent <details> is in its open state,
and therefore has the "open" attribute: */
content: 'B4';
}
<!-- language: lang-html -->
<div class="container-fluid">
<div class="sec3">
<ul class="sec2drop">
<li>
<details>
<summary>Prepare for a language exam</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>Hungarian for kids</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>Take on a challenge</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
<li>
<details>
<summary>Translation services</summary>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique, necessitatibus voluptas nisi sequi perferendis distinctio maiores magnam eos odit assumenda id minima fugiat cum explicabo adipisci vero culpa eligendi hic!</p>
</details>
</li>
</ul>
</div>
</div>
<!-- end snippet -->
References:
- CSS:
- HTML:
Bibliography:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论