简体   繁体   English

对齐 HTML 和 CSS 中的文本和图标

[英]Aligning text and icons in HTML and CSS

点击查看图片

I want it so all the icons are where they are and the text to the right needs more space between the icon and the text so all text starts at the same place.我想要它,所以所有图标都在它们所在的位置,并且右侧的文本在图标和文本之间需要更多空间,因此所有文本都从同一个位置开始。

Like this:像这样:

ICON  TEXT
ICON  TEXT
ICON  TEXT
ICON  TEXT
ICON  TEXT
ICON  TEXT

And not:并不是:

ICON TEXT
ICON   TEXT
ICON  TEXT
ICON TEXT
ICON  TEXT


<ul>
<li><i class"fa fa-icon"></i> <a href="example.com">Example</a></li>
<li><i class"fa fa-icon"></i> <a href="example.com">Example</a></li>
<li><i class"fa fa-icon"></i> <a href="example.com">Example</a></li>
<li><i class"fa fa-icon"></i> <a href="example.com">Example</a></li>
<li><i class"fa fa-icon"></i> <a href="example.com">Example</a></li>
</ul>

Either make all the icons the same width or use the CSS grid.要么使所有图标宽度相同,要么使用 CSS 网格。

 ul { list-style: none; padding: 0; margin: 0; } ul.width i { width: 1.5em; } ul.grid { display: grid; grid-template-columns: auto 1fr; gap: 0 0.5em; } ul.grid > li { display: contents; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <ul class="width"> <li><i class="fas fa-envelope"></i> <a href="example.com">envelope</a></li> <li><i class="fas fa-graduation-cap"></i> <a href="example.com">graduation-cap</a></li> <li><i class="fas fa-school"></i> <a href="example.com">school</a></li> <li><i class="fab fa-instagram"></i> <a href="example.com">instagram</a></li> <li><i class="fab fa-linkedin-in"></i> <a href="example.com">fa-linkedin-in</a></li> </ul> <br> <ul class="grid"> <li><i class="fas fa-envelope"></i> <a href="example.com">envelope</a></li> <li><i class="fas fa-graduation-cap"></i> <a href="example.com">graduation-cap</a></li> <li><i class="fas fa-school"></i> <a href="example.com">school</a></li> <li><i class="fab fa-instagram"></i> <a href="example.com">instagram</a></li> <li><i class="fab fa-linkedin-in"></i> <a href="example.com">fa-linkedin-in</a></li> </ul>

just put those text in a tag like and add a few pixel left padding.只需将这些文本放在一个标签中,然后添加几个像素的左填充。 that'll work.那会工作的。 If you could git some sample code.如果你可以 git 一些示例代码。 Explanation would have been easier.解释会更容易。

 .text{ padding-left: 20px; }
 <div class="elements"> <span></span> <span class="text">Home</span> </div>

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

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