【问题标题】:Navigation Bar Problem while decreasing screen width减小屏幕宽度时出现导航栏问题
【发布时间】:2021-11-09 02:00:58
【问题描述】:

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

您还可以看到图片顶部的宽度

这里的问题是我减小了屏幕宽度

/* 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>

【问题讨论】:

    标签: html css flexbox width margin


    【解决方案1】:

    尝试添加到#navbarfloat: 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>

    【讨论】:

    • 嘿,我的代码整洁干净,我的意思是如果有任何改进告诉我会做的话,其他人很容易理解
    • 嘿伙计 :) 如果你问我你的代码没问题
    • 嘿伙计,当我向下滚动时出现一些问题,灰色背景固定到顶部并且不需要固定导航栏
    • 啊,对不起,伙计,现在检查一下,我更新了答案
    猜你喜欢
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多