使用CSS将图标与标题中的文本对齐
对于HTML和CSS的所有内容,相对较新,我试图弄清楚为什么我无法将Instagram图标在标题栏中获得与其他文本保持一致。
使用字体Avesase添加图标,并带有< i>
元素,而标题中的其余文本为< a>
。
任何建议将不胜感激。
@import url('https://fonts.googleapis.com/css?family=Work+Sans:400,600');
* {
font-family: 'Work Sans', sans-serif;
font-weight: 800;
text-transform: uppercase;
font-size: 14px;
}
body {
margin: 0;
font-family: 'Work Sans', sans-serif;
font-weight: 800;
font-size: 14px;
}
i p {
font-family: 'Work Sans', sans-serif;
font-weight: 800;
text-transform: uppercase;
font-size: 14px;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #000000;
position: fixed;
width: 100%;
top: 0;
z-index: 1;
text-align: left;
height: 50px;
}
header::after {
content: '';
display: table;
clear: both;
}
nav {
float: left;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 20px;
padding: 3px;
color: white;
position: relative;
}
nav a,i {
color: rgb(255, 255, 255);
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
nav a:hover,i:hover {
color: rgb(249, 225, 8);
}
nav a::before,i::before {
content: '';
display: block;
height: 5px;
background-color: rgb(249, 225, 8);
position: absolute;
top: 0;
width: 0%;
transition: all ease-in-out 250ms;
}
nav a:hover::before,i:hover::before {
width: 100%;
}
<script src="https://kit.fontawesome.com/7a49dc0093.js" crossorigin="anonymous"></script>
<header>
<div class="container">
<h1 class="logo"></h1>
<nav>
<ul>
<li><i class="fa-brands fa-instagram"><p>testing</p></i></li>
<li><a href="#">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Discover</a></li>
</ul>
</nav>
</div>
</header>
Relatively new to all things HTML and CSS and I am trying to figure out why I can't get the instagram icon in my header bar to align with the rest of the text.
The icon is added using Font Awesome with an <i>
element while the rest of the text in the header is <a>
.
Any suggestions would be greatly appreciated.
@import url('https://fonts.googleapis.com/css?family=Work+Sans:400,600');
* {
font-family: 'Work Sans', sans-serif;
font-weight: 800;
text-transform: uppercase;
font-size: 14px;
}
body {
margin: 0;
font-family: 'Work Sans', sans-serif;
font-weight: 800;
font-size: 14px;
}
i p {
font-family: 'Work Sans', sans-serif;
font-weight: 800;
text-transform: uppercase;
font-size: 14px;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #000000;
position: fixed;
width: 100%;
top: 0;
z-index: 1;
text-align: left;
height: 50px;
}
header::after {
content: '';
display: table;
clear: both;
}
nav {
float: left;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 20px;
padding: 3px;
color: white;
position: relative;
}
nav a,i {
color: rgb(255, 255, 255);
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
nav a:hover,i:hover {
color: rgb(249, 225, 8);
}
nav a::before,i::before {
content: '';
display: block;
height: 5px;
background-color: rgb(249, 225, 8);
position: absolute;
top: 0;
width: 0%;
transition: all ease-in-out 250ms;
}
nav a:hover::before,i:hover::before {
width: 100%;
}
<script src="https://kit.fontawesome.com/7a49dc0093.js" crossorigin="anonymous"></script>
<header>
<div class="container">
<h1 class="logo"></h1>
<nav>
<ul>
<li><i class="fa-brands fa-instagram"><p>testing</p></i></li>
<li><a href="#">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Discover</a></li>
</ul>
</nav>
</div>
</header>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要问题是由于使用
位置:
之前。i :: obalute
但是,我在检查您的代码时发现了其他一些问题:
&lt; p&gt;
在 inline&lt; i&gt;
tag。这是固定的代码:
希望这会有所帮助。愉快的编码〜:)
The main issue is due to using
position:absolute
fori::before
.However, I found some other problems while checking your code :
<p>
tag inside an inline<i>
tag.Here is the fixed code :
Hope this helps. Happy coding~ :)
删除
&lt; p&gt;
元素使图标与文本对齐,并在
&lt; li&gt;
element中不在&lt; i&lt; i&gt;
element中进行文本。Remove
<p>
element to make icon align with textand make the text in
<li>
element not in<i>
element