html - 减小屏幕宽度时出现导航栏问题

标签 html css flexbox width margin

我正在尝试创建带有图像 Logo 、标题、导航链接和一些社交链接的导航栏。我为每个创建了 4 个 div。每当我使用响应式减小屏幕宽度时,导航栏中会出现白色,并随着我减小屏幕宽度而向左增加。 这里还有一些照片:

你还可以看到图片顶部的宽度 enter image description here

这是我减小屏幕宽度的问题 enter image description here

/* Reset */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

@font-face {
  font-family: PollerOne;
  src: url('../../fonts/Poller_One/PollerOne-Regular.ttf');
}


/* Styling Navigation Bar */

#navbar {
  display: flex;
  position: relative;
  align-items: center;
  height: 65px;
  width: 100%;
}

#navbar::before {
  content: "";
  position: absolute;
  background-color: #262626;
  height: 65px;
  width: 100%;
  z-index: -2;
}

.navbar {
  color: white;
  margin: 7px 7px;
}


/* Styling Logo */

#logo {
  background-color: black;
  height: 45px;
  width: 45px;
  min-width: 45px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#logo img {
  filter: invert();
  width: 25px;
}


/* Styling Title */

#title {
  font-family: PollerOne;
  min-width: 160.5px;
  font-size: 1.4rem;
  margin-left: 0px;
}


/* Styling Nav links */

#nav-links {
  margin: 0 auto;
}

#nav-links ul {
  list-style-type: none;
  display: flex;
}

.nav-links {
  font-size: 20px;
  margin: 0 20px;
  /* padding: 5px 10px; */
  position: relative;
  cursor: pointer;
}


/* Animation Under Nav Links */

.nav-links::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
  bottom: -4px;
  background: linear-gradient(white 0 0) center/0% 100% no-repeat;
  display: block;
}

:hover.nav-links::after {
  animation: pulse 300ms linear;
  animation-fill-mode: forwards;
}

@keyframes pulse {
  to {
    background-size: 100% 100%;
  }
}


/* Styling Social Links */

#social-links {
  margin-left: auto;
  display: flex;
}

.social-links {
  width: 30px;
  height: 30px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 5px;
}

.social-links img {
  width: 27px;
  transition: all 200ms ease;
}

.social-links img:hover {
  transform: scale(1.5)
}


/* Utility Class */
<div id="navbar">
  <div id="logo" class="navbar">
    <img src="./img/bag.png" alt="">
  </div>
  <div id="title" class="navbar">Meals Point</div>
  <div id="nav-links" class="navbar">
    <ul>
      <li class="nav-links"><a id="link-1">Home</a></li>
      <li class="nav-links"><a id="link-2">About</a></li>
      <li class="nav-links"><a id="link-3">Services</a></li>
      <li class="nav-links"><a id="link-4">Recipes</a></li>
      <li class="nav-links"><a id="link-5">Contact</a></li>
    </ul>
  </div>
  <div id="social-links" class="navbar">
    <a class="social-links" href="https://www.twitter.com"><img src="./img/twitter.png" id="link-1"></a>
    <a class="social-links" href="https://www.facebook.com"><img src="./img/facebook.png" id="link-2"></a>
    <a class="social-links" href="https://www.instagram.com"><img src="./img/instagram.png" id="link-3"></a>
  </div>
</div>

最佳答案

尝试添加到#navbar float: left; :

/* Reset */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

@font-face {
  font-family: PollerOne;
  src: url('../../fonts/Poller_One/PollerOne-Regular.ttf');
}


/* Styling Navigation Bar */

#navbar {
  display: flex;
  position: relative;
  align-items: center;
  height: 65px;
  min-width: 100%;
  float: left;
}

#navbar::before {
  content: "";
  position: absolute;
  background-color: #262626;
  height: 65px;
  min-width: 100%;
  z-index: -2;
}

.navbar {
  color: white;
  margin: 7px 7px;
}


/* Styling Logo */

#logo {
  background-color: black;
  height: 45px;
  width: 45px;
  min-width: 45px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#logo img {
  filter: invert();
  width: 25px;
}


/* Styling Title */

#title {
  font-family: PollerOne;
  min-width: 160.5px;
  font-size: 1.4rem;
  margin-left: 0px;
}


/* Styling Nav links */

#nav-links {
  margin: 0 auto;
}

#nav-links ul {
  list-style-type: none;
  display: flex;
}

.nav-links {
  font-size: 20px;
  margin: 0 20px;
  /* padding: 5px 10px; */
  position: relative;
  cursor: pointer;
}


/* Animation Under Nav Links */

.nav-links::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
  bottom: -4px;
  background: linear-gradient(white 0 0) center/0% 100% no-repeat;
  display: block;
}

:hover.nav-links::after {
  animation: pulse 300ms linear;
  animation-fill-mode: forwards;
}

@keyframes pulse {
  to {
    background-size: 100% 100%;
  }
}


/* Styling Social Links */

#social-links {
  margin-left: auto;
  display: flex;
}

.social-links {
  width: 30px;
  height: 30px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 5px;
}

.social-links img {
  width: 27px;
  transition: all 200ms ease;
}

.social-links img:hover {
  transform: scale(1.5)
}


/* Utility Class */
<div id="navbar">
  <div id="logo" class="navbar">
    <img src="./img/bag.png" alt="">
  </div>
  <div id="title" class="navbar">Meals Point</div>
  <div id="nav-links" class="navbar">
    <ul>
      <li class="nav-links"><a id="link-1">Home</a></li>
      <li class="nav-links"><a id="link-2">About</a></li>
      <li class="nav-links"><a id="link-3">Services</a></li>
      <li class="nav-links"><a id="link-4">Recipes</a></li>
      <li class="nav-links"><a id="link-5">Contact</a></li>
    </ul>
  </div>
  <div id="social-links" class="navbar">
    <a class="social-links" href="https://www.twitter.com"><img src="./img/twitter.png" id="link-1"></a>
    <a class="social-links" href="https://www.facebook.com"><img src="./img/facebook.png" id="link-2"></a>
    <a class="social-links" href="https://www.instagram.com"><img src="./img/instagram.png" id="link-3"></a>
  </div>
</div>

关于html - 减小屏幕宽度时出现导航栏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69168402/

相关文章:

html - 在 css 标签 docx4j/Jsoup 之前添加的空间

html - 如何像我在许多 Google 网络应用程序中看到的那样在右下角实现 'add' FAB?

html - Flex wrap layout with boxes that "float left"- 使最后一行框与其余框宽度相同

html - 导航栏上列表项的奇怪放置 - BOOTSTRAP 4

html - 为客户构建静态网站的最佳方式?

移动端和桌面端的 CSS 布局不同

html - 覆盖 [class] css 声明

javascript - 为什么 ng-container 显示我的 td 标签和模板元素不显示我的 td 标签?

html - <ul> <li> 水平滚动

css - 是否有 CSS 父级选择器?