CSS Flexbox有问题

发布于 2025-02-07 07:29:58 字数 2582 浏览 1 评论 0原文

我正在尝试在Navbar下方添加部分。我的问题是,如果我不添加位置:固定;顶部:50px(这是Navbar的高度),然后我部分的顶部被Navbar覆盖。

请告诉我我在做什么错。

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

body {
  background: #f5f6f7;
  color: #1b1b32;
  font-family: Helvetica;
  margin: 0;
}


/* NAVBAR */

header {
  height: 51px;
  width: 100%;
  background-color: #f6f8f9;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  position: fixed;
  top: 0;
}

h1 {
  color: #fa6980;
  font-size: 13;
}

nav {
  width: 50%;
  max-width: 300px;
  height: 50px;
}

nav>ul {
  display: flex;
  justify-content: space-evenly;
}

nav>ul>li {
  color: #343638;
  margin: 0 0.2rem;
  padding: 0.2rem;
  padding-top: 20px;
  display: block;
  font-size: 11px;
}


/* WORLD NEWS SECTION FLEXBOX */

.world-news {
  display: flex;
  height: 590px;
  width: 80%;
  margin: 0 auto;
}

.flex1 {
  width: 50%;
  background-image: url(./world\ news/image.png);
  height: 590px;
  padding-top: 15%;
  color: white;
  text-align: center;
}

.flex2 {
  width: 50%;
  height: 590px;
  background-color: white;
  padding-top: 15%;
}
<header>
  <h1 id="logo" class="bold">LOGO</h1>
  
  <nav>
    <ul>
      <li><a class="bold">HOME</a></li>
      <li><a class="bold">DISCOVERY</a></li>
      <li><a class="bold">PHOTOS</a></li>
      <li><a class="bold">CONTACT</a></li>
    </ul>
  </nav>
  
  <img src="https://via.placeholder.com/80" alt='profile pic sample' id='profile-pic' />
</header>

<section class="flex-box world-news">
  <div class="flex1">
    <div class="try">
      <h2 class="bold">WORLD NEWS</h2>
      <hr>
      <p class="bold">Lorem ipsum dolor sit amet, in eam odio amet, vix id nullam detracto vidit.</p>
      <img src="https://via.placeholder.com/60">
    </div>
  </div>

  <div class="flex2">
    <form>
      <label for="date" class="bold">Select a date</label>
      <input name='date' type="date" placeholder="Select...">
      <label for="keywords" class="bold">Enter the first keywords</label>
      <input name="keywords">
      <button class="bold">Search</button>
    </form>
  </div>
</section>

I'm trying to add a section below navbar. My problem is that if I don't add position:fixed; top: 50px (which is the height of the navbar) then the top of my section is getting covered by navbar.

Please tell me what am I doing wrong.

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

body {
  background: #f5f6f7;
  color: #1b1b32;
  font-family: Helvetica;
  margin: 0;
}


/* NAVBAR */

header {
  height: 51px;
  width: 100%;
  background-color: #f6f8f9;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  position: fixed;
  top: 0;
}

h1 {
  color: #fa6980;
  font-size: 13;
}

nav {
  width: 50%;
  max-width: 300px;
  height: 50px;
}

nav>ul {
  display: flex;
  justify-content: space-evenly;
}

nav>ul>li {
  color: #343638;
  margin: 0 0.2rem;
  padding: 0.2rem;
  padding-top: 20px;
  display: block;
  font-size: 11px;
}


/* WORLD NEWS SECTION FLEXBOX */

.world-news {
  display: flex;
  height: 590px;
  width: 80%;
  margin: 0 auto;
}

.flex1 {
  width: 50%;
  background-image: url(./world\ news/image.png);
  height: 590px;
  padding-top: 15%;
  color: white;
  text-align: center;
}

.flex2 {
  width: 50%;
  height: 590px;
  background-color: white;
  padding-top: 15%;
}
<header>
  <h1 id="logo" class="bold">LOGO</h1>
  
  <nav>
    <ul>
      <li><a class="bold">HOME</a></li>
      <li><a class="bold">DISCOVERY</a></li>
      <li><a class="bold">PHOTOS</a></li>
      <li><a class="bold">CONTACT</a></li>
    </ul>
  </nav>
  
  <img src="https://via.placeholder.com/80" alt='profile pic sample' id='profile-pic' />
</header>

<section class="flex-box world-news">
  <div class="flex1">
    <div class="try">
      <h2 class="bold">WORLD NEWS</h2>
      <hr>
      <p class="bold">Lorem ipsum dolor sit amet, in eam odio amet, vix id nullam detracto vidit.</p>
      <img src="https://via.placeholder.com/60">
    </div>
  </div>

  <div class="flex2">
    <form>
      <label for="date" class="bold">Select a date</label>
      <input name='date' type="date" placeholder="Select...">
      <label for="keywords" class="bold">Enter the first keywords</label>
      <input name="keywords">
      <button class="bold">Search</button>
    </form>
  </div>
</section>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

新雨望断虹 2025-02-14 07:29:58

由于您的Navbar具有位置:固定它始终将位于顶部,并将覆盖其下面的所有内容。我建议您做的是将页面中的所有其他内容包装在&lt; main&gt;元素中,并给出该主要元素50px的填充。这样...

<header>
  Header content here
</header>
<main>
  <section>some content</section>
  <section>some content</section>
</main>

然后在您的CSS中...

main {padding-top: 50px}

Since your navbar has position: fixed it's always going to be at the top and will cover everything underneath it. What I suggest you do is wrap everything else in the page in a <main> element and give that main element 50px of padding. Like this...

<header>
  Header content here
</header>
<main>
  <section>some content</section>
  <section>some content</section>
</main>

And then in your css...

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