使用 CSS 将图标与 header 中的文本对齐
Align icon with text in header using CSS
对于 HTML 和 CSS 的所有事物都比较陌生,我想弄清楚为什么我无法在我的 header 栏中获得 Instagram 图标以与其他图标对齐的文本。
图标是使用带有 <i>
元素的 Font Awesome 添加的,而 header 中的其余文本是 <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>
删除 <p>
元素以使图标与文本对齐
并使 <li>
元素中的文本不在 <i>
元素中
主要问题是由于 position:absolute
用于 i::before
。
但是,我在检查您的代码时发现了一些其他问题:
- 你不应该放 block level
<p>
tag inside an inline <i>
标签。
- 你应该使用 class selectors rather than type selectors for better specificity.
这是固定代码:
@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;
}
.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-menu {
margin: 0;
padding: 0;
list-style: none;
}
.nav-menu li {
display: inline-block;
margin-left: 70px;
padding-top: 20px;
padding: 3px;
color: white;
position: relative;
}
.nav-link {
color: rgb(255, 255, 255);
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
.nav-link i {
margin-right: 5px;
}
.nav-link:hover,
.nav-link:hover i {
color: rgb(249, 225, 8);
}
.nav-link::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-link:hover::before {
width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title> Document </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://kit.fontawesome.com/7a49dc0093.js" crossorigin="anonymous"></script>
</head>
<body>
<header class="header">
<div class="container">
<h1 class="logo"></h1>
<nav class="nav">
<ul class="nav-menu">
<li><a href="#" class="nav-link"><i class="fa-brands fa-instagram"></i>testing</a></li>
<li><a href="#" class="nav-link">Home</a></li>
<li><a href="#" class="nav-link">About Me</a></li>
<li><a href="#" class="nav-link">Blog</a></li>
<li><a href="#" class="nav-link">Discover</a></li>
</ul>
</nav>
</div>
</header>
</body>
</html>
希望这对您有所帮助。编码愉快~:)
对于 HTML 和 CSS 的所有事物都比较陌生,我想弄清楚为什么我无法在我的 header 栏中获得 Instagram 图标以与其他图标对齐的文本。
图标是使用带有 <i>
元素的 Font Awesome 添加的,而 header 中的其余文本是 <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>
删除 <p>
元素以使图标与文本对齐
并使 <li>
元素中的文本不在 <i>
元素中
主要问题是由于 position:absolute
用于 i::before
。
但是,我在检查您的代码时发现了一些其他问题:
- 你不应该放 block level
<p>
tag inside an inline<i>
标签。 - 你应该使用 class selectors rather than type selectors for better specificity.
这是固定代码:
@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;
}
.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-menu {
margin: 0;
padding: 0;
list-style: none;
}
.nav-menu li {
display: inline-block;
margin-left: 70px;
padding-top: 20px;
padding: 3px;
color: white;
position: relative;
}
.nav-link {
color: rgb(255, 255, 255);
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
.nav-link i {
margin-right: 5px;
}
.nav-link:hover,
.nav-link:hover i {
color: rgb(249, 225, 8);
}
.nav-link::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-link:hover::before {
width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title> Document </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://kit.fontawesome.com/7a49dc0093.js" crossorigin="anonymous"></script>
</head>
<body>
<header class="header">
<div class="container">
<h1 class="logo"></h1>
<nav class="nav">
<ul class="nav-menu">
<li><a href="#" class="nav-link"><i class="fa-brands fa-instagram"></i>testing</a></li>
<li><a href="#" class="nav-link">Home</a></li>
<li><a href="#" class="nav-link">About Me</a></li>
<li><a href="#" class="nav-link">Blog</a></li>
<li><a href="#" class="nav-link">Discover</a></li>
</ul>
</nav>
</div>
</header>
</body>
</html>
希望这对您有所帮助。编码愉快~:)