简体   繁体   English

如何将图标水平居中放置在文本上方(无 flexbox/grid)?

[英]How to center icons horizontally above text (no flexbox/grid)?

I am working on psd conversion and I need to center the font awesome icons horizontally above the text without using flexbox and grid.我正在处理 psd 转换,我需要在不使用 flexbox 和网格的情况下将字体真棒图标水平居中放置在文本上方。 The ul that wraps them will be stacking vertically on mobile.包裹它们的 ul 将在移动设备上垂直堆叠。

I need to accomplish this by using only css.我需要仅使用 css 来完成此操作。 Any help is appreciated.任何帮助表示赞赏。

 <div class="info-section">
      <!-- <div class="wrapper clearfix"> -->
        <section class="info-section-container">
          <ul>
            <li><a href="#" class="info-link"><i class="fas fa-location-arrow contact-icons" aria-hidden="true"></i>Locations</a></li>
            <li><a href="#" class="info-link"><i class="fas fa-ticket-alt contact-icons" aria-hidden="true"></i>Tickets</a></li>
           <li><a href="#" class=info-link><i class="fas fa-phone contact-icons" aria-hidden="true"></i>Reach Out</a></li>
        </ul>
      <!-- </div> -->
      </div>
/* this centers the entire container  */
 .info-section-container {
    /* width: 50%; */
    text-align: center;
    margin: 0 25%;

  }

  /* to position the li horizontally */
  .info-section-container ul li {
    float: left;
  }

  .info-link  {
  font-family: 'Montserrat', sans-serif;
  font-size: 3rem;
  font-weight: 700;
  text-transform: uppercase;
  text-align: center;
  /* for accesability */
  padding: 10px;
  /* width: 30%;
  display: inline-block; */
 }

  .contact-icons {
    display: block;
    text-align: center;
    padding-bottom: 20px;
  }

add some flex container on your ul在你的 ul 上添加一些 flex 容器

<ul class="flex-container">
    <li><a href="#" class="info-link"><i class="fas fa-location-arrow contact-icons" aria-hidden="true"></i>Locations</a></li>
    <li><a href="#" class="info-link"><i class="fas fa-ticket-alt contact-icons" aria-hidden="true"></i>Tickets</a></li>
    <li><a href="#" class=info-link><i class="fas fa-phone contact-icons" aria-hidden="true"></i>Reach Out</a></li>
</ul>

and a couple of new flex classes和几个新的 flex 类

.flex-container {
    display: flex;
}
.flex-container li {
    display: flex;
    flex-direction: column;
}

should make the icon above the text应该使图标位于文本上方

I edited your code as:我将您的代码编辑为:

 .info-section-container { align-items: center; justify-content: center; text-align: center; position: absolute; left: 25%; } .info-link { width: 50vw; height: 30vh; display: flex; flex-direction: column; margin-top: 5%; margin-bottom: 5%; font-family: 'Montserrat', sans-serif; font-size: 3rem; font-weight: 700; text-transform: uppercase; }
 <div class="info-section"> <div class="info-section-container"> <a href="#" class="info-link"><i class="fas fa-location-arrow contact-icons" aria-hidden="true"></i>Locations</a> <a href="#" class="info-link"><i class="fas fa-ticket-alt contact-icons" aria-hidden="true"></i>Tickets</a> <a href="#" class=info-link><i class="fas fa-phone contact-icons" aria-hidden="true"></i>Reach Out</a> </div> </div>

First, I adjusted width of info-link as 50vw.首先,我将信息链接的宽度调整为 50vw。 It covers the half of the width of screen.它覆盖了屏幕宽度的一半。 Therefore, it has 25% margin from both left and right.因此,它的左侧和右侧都有 25% 的余量。 You want to center them, so I gave extra left which is 25% in absolute position and added justify-conter: center in .info-section-container to center the three texts horizontally.你想让它们居中,所以我给了额外的左边,它是绝对位置的 25% 并添加 justify-conter: center in .info-section-container 以水平居中三个文本。

I chose display as flex to have column flex direction.我选择显示为 flex 以具有列的 flex 方向。

Then, in .info-section-container, I added align-items: center to center vertically these three items.然后,在 .info-section-container 中,我添加了 align-items: center 以垂直居中这三个项目。

Hope I helped.希望我有所帮助。

If all you want to do is centre all the <li> items and the icons, then you need very few changes.如果您只想将所有<li>项目和图标居中,那么您只需进行很少的更改。 All you really need to do is add the following rules:您真正需要做的就是添加以下规则:

.info-section-container {  margin: 0 auto; }
.info-section-container ul { list-style: none; padding: 0; }
.info-link {  text-align: center; display: inline-block; /* and your own styles here */ }
.fas.contact-icons { display:block; }

How these work:这些是如何工作的:

  • The biggest problem is the left and right margins of 25% on info-section-container - this is too big on mobile and the text doesn't fit, it juts out to the right.最大的问题是info-section-container左右边距为 25% - 这在移动设备上太大了,文本不适合,它向右突出。
    To centre the whole container use margin: 0 auto;要使整个容器居中,请使用margin: 0 auto; . . If you want to limit its size, you can use max-width如果你想限制它的大小,你可以使用max-width
  • <ul> elements have padding on the left which will affect the centre alignment, so you need to set it to 0 <ul>元素在左边有填充会影响居中对齐,所以你需要把它设置为 0
  • Set your info-link elements to be inline-block and text-align center将您的info-link元素设置为 inline-block 和 text-align center
  • You need to include .fas in the .contact-icons selector to override the default CSS您需要在.contact-icons选择器中包含.fas以覆盖默认 CSS

You can see this working below - note I have added a media query for 650px so you can see it stacked here and horizontal in you view the snippet in expanded.你可以在下面看到这个工作- 请注意,我已经添加了一个 650px 的媒体查询,所以你可以看到它在这里堆叠,并在你查看展开的片段时水平。

 /* this centers the entire container */ .info-section-container { max-width: 1000px; text-align: center; margin: 0 auto; } .info-section-container ul { list-style: none; display: inline-block; padding: 0; } .info-section-container ul li { text-align: center; margin: 20px 0; display: inline-block; } .info-link { font-family: 'Montserrat', sans-serif; font-size:3em; font-weight: 700; text-transform: uppercase; text-align: center; text-decoration: none; display: inline-block; /* for accesability */ padding: 10px; } .fa.contact-icons, /* for this demo only */ .fas.contact-icons { display: block; padding-bottom:10px; } @media screen and (max-width: 650px){ .info-section-container ul li { display: block;} }
 <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" /> <div class="info-section"> <section class="info-section-container"> <ul> <li><a href="#" class="info-link"><i class="fa fa-location-arrow contact-icons" aria-hidden="true"></i>Locations</a></li> <li><a href="#" class="info-link"><i class="fa fa-ticket contact-icons" aria-hidden="true"></i>Tickets</a></li> <li><a href="#" class=info-link><i class="fa fa-phone contact-icons" aria-hidden="true"></i>Reach Out</a></li> </ul> </section> </div>

FYI I am using Font Awesone 4.7 above because 5 isn't available on a CDN.仅供参考,我使用的是上面的 Font Awesone 4.7,因为 5 在 CDN 上不可用。 The main difference is the class is .fa instead of .fas主要区别是类是.fa而不是.fas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM