如何在中心显示图标和文本 [英] How to display icon and text below each other with icon in center

查看:48
本文介绍了如何在中心显示图标和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有背景图像的 i 标签,然后跟随 .基本上它是一个图标,后跟文本.我试图将图标居中对齐,然后在图标下方对齐文本.我该怎么做?

I have an i tag with background image and then follows <span>. Basically its an icon followed by text. I am trying to align the icon in center and then below the icon the text. How can I do this?

我试过垂直对齐,但没有用.

I tried with vertical align, but it did not work.

<li> 
<i class="icons" ></i>
<span>Icon text</span>
<li />

CSS:

.icons ::before {
content: url(/Style/Image1.svg);
}

推荐答案

你可以通过几种方式来做到这一点(一旦你修复了语法错误):

You can do this a few ways (once you fix the syntax errors):

1.使用弹性盒
使用 flex,您只需要使用正确的选项来设置容器元素的样式,使子元素按照我们的意愿行事 - 请参阅评论:

1. Using flexbox
With flex you only need to style the container element using the right options to make the child elements act as we wish - see the comments:

li {
  display: flex;
  align-items: center;     /* center align the elements in the container */
  flex-direction: column;  /* make the child elements appear one under another */
}

.icons:before {  content: url(https://via.placeholder.com/200x100); }

<ul>
  <li><i class="icons"></i><span>Icon text 1</span></li>
  <li><i class="icons"></i><span>Icon text 2</span></li>
</ul>

2.文本对齐的块元素:居中

/* center-align the container element */
li { text-align: center; list-style: none; }

/* make i and span  block elements so they appear one below the other */
li i, li.span { display: block; }

.icons:before { content: url(https://via.placeholder.com/200x100); }

<ul>
  <li><i class="icons"></i><span>Icon text 1</span></li>
  <li><i class="icons"></i><span>Icon text 2</span></li>
</ul>

注意 - 您还有一些语法错误:

  • 在选择器和 :before(或 ::before - 两者都有效)之间不能有空格
  • 还有 <li/> 不正确 - 您使用 </li>
  • 关闭 li 元素
  • You cannot have a space between the selector and :before (or ::before - both work)
  • Also <li /> is not correct - you close an li element using </li>

这篇关于如何在中心显示图标和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆