如何在HTML中在列表项旁边添加图标

huangapple go评论77阅读模式
英文:

How to add an icon next to a list item in HTML

问题

如何在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>
英文:

如何在HTML中在列表项旁边添加图标

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 (&lt;i class=&quot;fa-light fa-arrow-down&quot;&gt;&lt;/i&gt;) 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 -->

&lt;div class=&quot;container-fluid&quot;&gt;
  &lt;div class=&quot;sec3&quot;&gt;
    &lt;ul class=&quot;sec2drop&quot;&gt;
      &lt;li&gt;
        &lt;i class=&quot;fa-light fa-arrow-down&quot;&gt;&lt;/i&gt; Prepare for a language exam
      &lt;/li&gt;
      &lt;li&gt;Hungarian for kids&lt;/li&gt;
      &lt;li&gt;Take on a challenge&lt;/li&gt;
      &lt;li&gt;Translation services&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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:

&lt;div class=&quot;container-fluid&quot;&gt;
    &lt;div class=&quot;sec3&quot;&gt;
      &lt;ul class=&quot;sec2drop&quot;&gt;
        &lt;li&gt;
           Prepare for a language exam &lt;i class=&quot;fa fa-caret-down&quot;&gt;&lt;/i&gt;
        &lt;/li&gt;
        &lt;li&gt;Hungarian for kids&lt;/li&gt;
        &lt;li&gt;Take on a challenge&lt;/li&gt;
        &lt;li&gt;Translation services&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/div&gt;

答案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 -->

&lt;div class=&quot;container-fluid&quot;&gt;
  &lt;div class=&quot;sec3&quot;&gt;
    &lt;ul class=&quot;sec2drop&quot;&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Prepare for a language exam&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Hungarian for kids&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Take on a challenge&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Translation services&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

JS Fiddle demo.

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 &lt;li&gt; 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: &#39;&#39;;
}

summary {
  /* using the cursor to imply interaction is possible: */
  cursor: pointer;
  /* using flex layout to place a gap between the &lt;summary&gt;
     element&#39;s text and its ::before pseudo-element: */
  display: flex;
  gap: 0.25em;
}

details summary::before {
  /* using the downward-pointing triangle as the content: */
  content: &#39;BE&#39;;
  background-color: transparent;
}

details[open] summary::before {
  /* using the upward-pointing triangle as the content
     when the parent &lt;details&gt; is in its open state,
     and therefore has the &quot;open&quot; attribute: */
  content: &#39;B4&#39;;
}

<!-- language: lang-html -->

&lt;div class=&quot;container-fluid&quot;&gt;
  &lt;div class=&quot;sec3&quot;&gt;
    &lt;ul class=&quot;sec2drop&quot;&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Prepare for a language exam&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Hungarian for kids&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Take on a challenge&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;details&gt;
          &lt;summary&gt;Translation services&lt;/summary&gt;
          &lt;p&gt;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!&lt;/p&gt;
        &lt;/details&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

JS Fiddle demo.

References:

Bibliography:

huangapple
  • 本文由 发表于 2023年3月15日 19:54:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定