减少包含图像的 Bootstrap 导航栏项目的宽度

Reduce the width of Bootstrap navbar item containing image

我正在设计带有 YouTube 徽标的 bootstrap 导航栏。它没有向右对齐,而是笨拙地放置在中间。看起来锚的宽度大于图像的大小,因为图像被推到右边。我怎样才能把它移到右边。

.navbar-brand{
  font-family: Lilita One;
}

.navbar-top{
  padding: 0 0;
}

.navbar{
  padding: 0 5% 0 2%;
}

.yt-img{
  width: 6%;
  position: relative;
}

.yt-img-a{
  width: 100%;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Education Side</title>
  <!-- Tab Icon -->
  <link rel="icon" href="images\Icon.ico">
  <!-- Google Fonts -->
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Lilita+One&family=Montserrat:wght@500&display=swap" rel="stylesheet">
  <!-- Bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
  <!-- CSS -->
  <link rel="stylesheet" href="css/styles.css">
</head>

<body>
  <section id="title">
    <div class="container-fluid navbar-top">
      <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Education<br>Side</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="navbar-collapse collapse" id="navbarTogglerDemo02">
          <ul class="navbar-nav ms-auto">
            <li class="nav-item">
              <a class="nav-link yt-img-a" href="https://www.youtube.com/"><img class="yt-img" src="images\youtube.png" alt=""></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">Counselling</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">Recruitment</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">Contact</a>
            </li>
          </ul>
        </div>
      </nav>
    </div>
  </section>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
</body>

</html>

enter image description here

ms-auto class 让你的 ul 放在右边。

<ul class="navbar-nav ms-auto"> // ms-auto removed

Codepen Link: https://codepen.io/emmeiWhite/pen/JjRMxQw

已修复该问题并在需要的地方添加了 flex-grow:1:)

完整代码:

.navbar-brand{
  font-family: Lilita One;
}

.navbar-top{
  padding: 0 0;
}

.navbar{
  padding: 0 5% 0 2%;
}

.yt-img{
  width: 6%;
  position: relative;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Education Side</title>
  <!-- Tab Icon -->
  <link rel="icon" href="images\Icon.ico">
  <!-- Google Fonts -->
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Lilita+One&family=Montserrat:wght@500&display=swap" rel="stylesheet">
  <!-- Bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
  <!-- CSS -->
  <link rel="stylesheet" href="css/styles.css">
</head>

<body>
  <section id="title">
    <div class="container-fluid navbar-top">
      <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Education<br>Side</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="navbar-collapse collapse" id="navbarTogglerDemo02">
          <ul class="navbar-nav flex-grow-1">
            <li class="nav-item flex-grow-1">
              <a class="nav-link yt-img-a" href="https://www.youtube.com/"><img class="yt-img" src="images\youtube.png" alt="Image Here"></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">Counselling</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">Recruitment</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">Contact</a>
            </li>
          </ul>
        </div>
      </nav>
    </div>
  </section>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
</body>

</html>

不要将图像宽度设置为 6%,而是使用 px 值,例如 30px

.yt-img{
  width: 30px;
  position: relative;
}

Demo