在不破坏 flexbox 的情况下固定导航栏

Fixed navbar without breaking flexbox

position: fixed; 添加到导航栏中断 margin-left: auto; 导航栏菜单图标:

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.navbar {
  position: fixed;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div class="nav-holder">

  </div>
  <div class="container">
    <nav class="navbar">
      <img src="/img/Mitcho Icon black yellow.png" alt="logo" class="logo"> <span class="logo-type">Mitcho</span>
      <div class="nav-menu"></div>
    </nav>
    <main class="main-con">
      <div class="hero">
        <h1 class="greeting">Hello!</h1>
        <span class="hero-name">
                    I'm Mitchell Orutu,
                </span>
        <div class="hero-anim">
          <span class="h-love">I Love</span>
          <div class="x-anim"></div>
          <span class="h-creating">Creating</span>
          <div class="dash-anim"></div>
          <span class="h-amazing">Amazing Experiences</span>
        </div>
        <p class="h-details">A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" class="know-more-btn">
          <span class="know-more-text">Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>

可以通过拍打 width: 100%:

来修复它

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.navbar {
  position: fixed;
  width: 100%;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div class="nav-holder">

  </div>
  <div class="container">
    <nav class="navbar">
      <img src="/img/Mitcho Icon black yellow.png" alt="logo" class="logo"> <span class="logo-type">Mitcho</span>
      <div class="nav-menu"></div>
    </nav>
    <main class="main-con">
      <div class="hero">
        <h1 class="greeting">Hello!</h1>
        <span class="hero-name">
                    I'm Mitchell Orutu,
                </span>
        <div class="hero-anim">
          <span class="h-love">I Love</span>
          <div class="x-anim"></div>
          <span class="h-creating">Creating</span>
          <div class="dash-anim"></div>
          <span class="h-amazing">Amazing Experiences</span>
        </div>
        <p class="h-details">A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" class="know-more-btn">
          <span class="know-more-text">Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>

1 个首选解决方案

您只需将 right:0width: 100%; 添加到您的 .navbar class。

.navbar {  
  position: fixed;  
  right:0;
  width:100%;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;  
}

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.navbar {  
  position: fixed;  
  right:0;
  width:100%;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;  
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div class="nav-holder">

  </div>
  <div class="container">
    
      <nav class="navbar">
        <img src="/img/Mitcho Icon black yellow.png" alt="logo" class="logo"> <span class="logo-type">Mitcho</span>
        <div class="nav-menu"></div>
      </nav>
    
    <main class="main-con">
      <div class="hero">
        <h1 class="greeting">Hello!</h1>
        <span class="hero-name">
                    I'm Mitchell Orutu,
                </span>
        <div class="hero-anim">
          <span class="h-love">I Love</span>
          <div class="x-anim"></div>
          <span class="h-creating">Creating</span>
          <div class="dash-anim"></div>
          <span class="h-amazing">Amazing Experiences</span>
        </div>
        <p class="h-details">A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" class="know-more-btn">
          <span class="know-more-text">Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>
      

2个解决方案

我用 div 包裹导航标签,并为这个 div 分配宽度和填充。然后我从导航元素中删除 position:fixed。

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.fixed {
  position: fixed;  
  width:100%;
  padding:0px 30px 0px 0px;
}

.navbar {  
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;  
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div class="nav-holder">

  </div>
  <div class="container">
    <div class="fixed">
      <nav class="navbar">
        <img src="/img/Mitcho Icon black yellow.png" alt="logo" class="logo"> <span class="logo-type">Mitcho</span>
        <div class="nav-menu"></div>
      </nav>
    </div>
    <main class="main-con">
      <div class="hero">
        <h1 class="greeting">Hello!</h1>
        <span class="hero-name">
                    I'm Mitchell Orutu,
                </span>
        <div class="hero-anim">
          <span class="h-love">I Love</span>
          <div class="x-anim"></div>
          <span class="h-creating">Creating</span>
          <div class="dash-anim"></div>
          <span class="h-amazing">Amazing Experiences</span>
        </div>
        <p class="h-details">A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" class="know-more-btn">
          <span class="know-more-text">Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>