使用变大小的Font Awesome图标时,列表项的对齐方式

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

Alignment of list items when using variable sized font awesome icons

问题

我正在尝试列出公司的所有联系方式。
我正在使用来自Font Awesome的不同图标。
每个图标的宽度不同。
如何正确地左对齐所有文本?
我成功地使用flex布局使文本跨多行流动。
显示区域限制在230px内。

body {
  font-family: Arial, Helvetica, sans-serif;
}

ul.ContactUs {
  list-style-type: none;
  display: flex;
  flex-direction: column;
  align-items: left;
}

ul.ContactUs li {
  display: flex;
  padding: 4px 0;
  color: black;
}

ul.ContactUs li:before {
  font-family: 'FontAwesome';
  content: '\f067';
  margin: 0 11px 0 -25px;
}

ul.ContactUs li.address:before {
  content: '\f60e';
}

ul.ContactUs li.phone:before {
  content: '\f879';
}

ul.ContactUs li.mobile:before {
  content: '\f3cd';
}

ul.ContactUs li.email:before {
  content: '\f0e0';
  margin-right: 0.5em;
}
<!--Font Awesome  -->
<script src="https://kit.fontawesome.com/d2ee99dc46.js" crossorigin="anonymous"></script>
<div style="width: 230px; border: 1px solid black;">
  <h4>Contact Us</h4>
  <ul class="ContactUs">
    <li class="address">63 Street name,
      Azaadville,
      Johannesburg,
      South Africa,
      1754</li>
    <li class="phone">011 768 3415</li>
    <li class="mobile">082 554 0050</li>
    <li class="email">Hello(at) Companyname.co.za</li>
  </ul>
</div>
英文:

I am trying to list all the contact details of a company.
I am using different icons from font awesome.
Each icon does not have the same width.
How to I properly left allight all the text?
I managed to algin the text flowing over multiple lines using flex layout.
The display area is limited to 230px

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  font-family: Arial, Helvetica, sans-serif;
}


/*
        https://fontawesome.com/v5/search
        */

ul.ContactUs {
  list-style-type: none;
  display: flex;
  /* Use flex layout for the list */
  flex-direction: column;
  /* Stack the list items vertically */
  align-items: left;
  /* Align the list items vertically at the center */
}

ul.ContactUs li {
  display: flex;
  padding: 4px 0;
  color: black;
}

ul.ContactUs li:before {
  font-family: &#39;FontAwesome&#39;;
  content: &#39;\f067&#39;;
  margin: 0 11px 0 -25px;
}

ul.ContactUs li.address:before {
  content: &#39;\f60e&#39;;
}

ul.ContactUs li.phone:before {
  content: &#39;\f879&#39;;
}

ul.ContactUs li.mobile:before {
  content: &#39;\f3cd&#39;;
}

ul.ContactUs li.email:before {
  content: &#39;\f0e0&#39;;
  margin-right: 0.5em;
  /* Some space between the icon and the text */
}

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

&lt;!--Fontawsome  --&gt;
&lt;script src=&quot;https://kit.fontawesome.com/d2ee99dc46.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  &lt;div style=&quot;width: 230px; border: 1px solid black;&quot;&gt;&lt;h4&gt;Contact Us&lt;/h4&gt;&lt;ul class=&quot;ContactUs&quot;&gt;&lt;li class=&quot;address&quot;&gt;63 Street name,
  Azaadville,
  Johannesburg,
  South Africa,
  1754&lt;/li&gt;&lt;li class=&quot;phone&quot;&gt;011 768 3415&lt;/li&gt;&lt;li class=&quot;mobile&quot;&gt;082 554 0050&lt;/li&gt;&lt;li class=&quot;email&quot;&gt;Hello(at) Companyname.co.za&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

我编辑了你的示例,我在li中添加了gap: 5px;来控制弹性项之间的间距,我还移除了电子邮件limargin-right,以使电子邮件文本与预览文本对齐,然后像mok_ku说的那样,我在ul.ContactUs li:before中添加了flex: 0 0 20px;以使宽度固定为20px:

body {
  font-family: Arial, Helvetica, sans-serif;
}

/*
  https://fontawesome.com/v5/search
*/

ul.ContactUs {
  list-style-type: none;
  display: flex;
  /* 使用弹性布局 */
  flex-direction: column;
  /* 垂直堆叠列表项 */
  align-items: left;
  /* 垂直居中对齐列表项 */
}

ul.ContactUs li {
  display: flex;
  padding: 4px 0;
  color: black;
  gap: 5px;
}

ul.ContactUs li:before {
  font-family: 'FontAwesome';
  content: '\f067';
  margin: 0 11px 0 -25px;
  flex: 0 0 20px;
}

ul.ContactUs li.address:before {
  content: '\f60e';
}

ul.ContactUs li.phone:before {
  content: '\f879';
}

ul.ContactUs li.mobile:before {
  content: '\f3cd';
}

ul.ContactUs li.email:before {
  content: '\f0e0';
  /* 图标和文本之间的一些间距 */
}
<!--Fontawsome  -->
<script src="https://kit.fontawesome.com/d2ee99dc46.js" crossorigin="anonymous"></script>
<div style="width: 230px; border: 1px solid black;">
  <h4>Contact Us</h4>
  <ul class="ContactUs">
    <li class="address">
      63 Street name,
      Azaadville,
      Johannesburg,
      South Africa,
      1754
    </li>
    <li class="phone">011 768 3415</li>
    <li class="mobile">082 554 0050</li>
    <li class="email">Hello(at) Companyname.co.za</li>
  </ul>
</div>
英文:

I edited a little you example, I added gap: 5px; to the li to controls the space between flex items, I also removed the margin-right for email li so the email text will be aligned with the previews texts, Then like mok_ku said I added flex: 0 0 20px; to ul.ContactUs li:before so the width is fixed to 20px:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  font-family: Arial, Helvetica, sans-serif;
}

/*
  https://fontawesome.com/v5/search
*/

ul.ContactUs {
  list-style-type: none;
  display: flex;
  /* Use flex layout for the list */
  flex-direction: column;
  /* Stack the list items vertically */
  align-items: left;
  /* Align the list items vertically at the center */
}

ul.ContactUs li {
  display: flex;
  padding: 4px 0;
  color: black;
  gap: 5px;
}

ul.ContactUs li:before {
  font-family: &#39;FontAwesome&#39;;
  content: &#39;\f067&#39;;
  margin: 0 11px 0 -25px;
  flex: 0 0 20px;
}

ul.ContactUs li.address:before {
  content: &#39;\f60e&#39;;
}

ul.ContactUs li.phone:before {
  content: &#39;\f879&#39;;
}

ul.ContactUs li.mobile:before {
  content: &#39;\f3cd&#39;;
}

ul.ContactUs li.email:before {
  content: &#39;\f0e0&#39;;
  /* Some space between the icon and the text */
}

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

&lt;!--Fontawsome  --&gt;
&lt;script src=&quot;https://kit.fontawesome.com/d2ee99dc46.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;div style=&quot;width: 230px; border: 1px solid black;&quot;&gt;
  &lt;h4&gt;Contact Us&lt;/h4&gt;
  &lt;ul class=&quot;ContactUs&quot;&gt;
    &lt;li class=&quot;address&quot;&gt;
      63 Street name,
      Azaadville,
      Johannesburg,
      South Africa,
      1754
    &lt;/li&gt;
    &lt;li class=&quot;phone&quot;&gt;011 768 3415&lt;/li&gt;
    &lt;li class=&quot;mobile&quot;&gt;082 554 0050&lt;/li&gt;
    &lt;li class=&quot;email&quot;&gt;Hello(at) Companyname.co.za&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

我会将图标直接放在HTML文件中,而不是在CSS中使用:before伪元素。

我认为ul在这里不应该使用flex属性。相反,我会将每个列表项设置为flex容器,并给包含图标的div一个固定宽度,例如15%,将其余的85%分配给文本。

英文:

I would place the icons directly in the HTML file instead of working with :before pseudo-elements in CSS.

I don't think the ul should have a display of flex here. Instead I would make each of the list items a flex box and give the div that contains the icons a certain width, for example 15%, and give the other 85% to the text.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  font-family: Arial, Helvetica, sans-serif;
}

.contact-info {
  font-style: normal;
  width: 230px;
  border: 1px solid black;
}
.contact-info h4 {
  text-align: center;
}

.contact-info__list {
  list-style-type: none;
  margin: 0 0 0 20px;
  padding: 0;
}

.contact-info__list__item {
  display: flex;
  align-items: center;
}

.contact-info__list__item__icon {
  width: 15%;
}

.contact-info__list__item__text {
  width: 85%;
}

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

    &lt;script
      src=&quot;https://kit.fontawesome.com/d2ee99dc46.js&quot;
      crossorigin=&quot;anonymous&quot;
    &gt;&lt;/script&gt;

&lt;address class=&quot;contact-info&quot;&gt;
      &lt;h4&gt;Contact Us&lt;/h4&gt;
      &lt;ul class=&quot;contact-info__list&quot;&gt;
        &lt;li class=&quot;contact-info__list__item&quot;&gt;
          &lt;div class=&quot;contact-info__list__item__icon&quot;&gt;
            &lt;i class=&quot;fa-solid fa-address-book&quot;&gt;&lt;/i&gt;
          &lt;/div&gt;
          &lt;p class=&quot;contact-info__list__item__text&quot;&gt;
            63 Street name, Azaadville, Johannesburg, South Africa, 1754
          &lt;/p&gt;
        &lt;/li&gt;
        &lt;li class=&quot;contact-info__list__item&quot;&gt;
          &lt;div class=&quot;contact-info__list__item__icon&quot;&gt;
            &lt;i class=&quot;fa-solid fa-phone-flip&quot;&gt;&lt;/i&gt;
          &lt;/div&gt;
          &lt;p class=&quot;contact-info__list__item__text&quot;&gt;011 768 3415&lt;/p&gt;
        &lt;/li&gt;
        &lt;li class=&quot;contact-info__list__item&quot;&gt;
          &lt;div class=&quot;contact-info__list__item__icon&quot;&gt;
            &lt;i class=&quot;fa-solid fa-mobile-screen-button&quot;&gt;&lt;/i&gt;
          &lt;/div&gt;
          &lt;p class=&quot;contact-info__list__item__text&quot;&gt;082 554 0050&lt;/p&gt;
        &lt;/li&gt;
        &lt;li class=&quot;contact-info__list__item&quot;&gt;
          &lt;div class=&quot;contact-info__list__item__icon&quot;&gt;
            &lt;i class=&quot;fa-solid fa-envelope&quot;&gt;&lt;/i&gt;
          &lt;/div&gt;
          &lt;p class=&quot;contact-info__list__item__text&quot;&gt;
            Hello(at) Companyname.co.za
          &lt;/p&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/address&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 16:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054495.html
匿名

发表评论

匿名网友

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

确定