使用CSS将图标与标题中的文本对齐

发布于 2025-02-05 00:28:42 字数 2462 浏览 2 评论 0原文

对于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.

Navbar

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2025-02-12 00:28:42

主要问题是由于使用位置:i :: obalute之前。

但是,我在检查您的代码时发现了其他一些问题:

这是固定的代码:

@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>

希望这会有所帮助。愉快的编码〜:)

The main issue is due to using position:absolute for i::before.

However, I found some other problems while checking your code :

Here is the fixed code :

@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>

Hope this helps. Happy coding~ :)

↙厌世 2025-02-12 00:28:42

删除&lt; p&gt;元素使图标与文本对齐

,并在&lt; li&gt; element中不在&lt; i&lt; i&gt; element中进行文本。

Remove <p> element to make icon align with text

and make the text in <li> element not in <i> element

remove p element

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文