首页 > 解决方案 > 为什么不会'justify-content:space-between'使导航栏转到右侧?

问题描述

我对 html 和 css 很陌生,但似乎无法理解为什么justify-content不能正常工作。内联块似乎确保.navbar_wrapper没有 100% 的宽度,并且<a>链接元素也没有max-width,所以我不确定为什么它们根本没有被隔开或受 header flex 容器的影响。

@import url('https"//fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap');
* {
  margin: 0;
  padding: 0;
  /* box-sizing: border-box; */
  font-family: "Poppins", sans-serif;
}

.header {
  position: fixed;
  height: 70px;
}

.logo {
  align-items: center;
  display: inline-block;
  margin: 20px 70px;
}

.navbar_wrapper {
  position: absolute;
  right: 0;
  top: 0;
  height: 94px;
  margin: 0px 70px;
  display: flex;
  align-items: center;
}

.navbar_wrapper>a:not(:last-child) {
  margin-right: 40px;
  text-decoration: none;
  color: black;
  cursor: pointer;
}

.navbar_wrapper>a:nth-child(4) {
  text-decoration: none;
  color: black;
  cursor: pointer;
}

.sb_logo {
  height: 50px;
  width: 50px;
}

.section {
  position: relative;
  width: 100%;
  min-height: 100vh;
  padding: 100px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
}

.navbar_wrapper {
  display: inline-block;
}

.header {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  padding: 20px 100px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="style.css" />
  <title>Document</title>
</head>

<body>
  <section>
    <header>
      <a href="#" class="logo">
        <img src="images/logo.png" alt="" class="sb_logo" />
      </a>
      <div class="navbar_wrapper">
        <a href="#" class="home">Home</a>
        <a href="#" class="menu">Menu</a>
        <a href="#" class="what's new">What's New</a>
        <a href="#" class="contact">Contact</a>
      </div>
    </header>
  </section>
</body>

</html>

标签: htmlcssflexbox

解决方案


.logo从和中删除左右边距.navbar_wrapper将导致所需的结果 - 请参见下面的代码段。

但请注意,我的修复实际上与任何 flexbox 设置无关。您的 flexbox 容器 ( section) 只有一个子 ( <header>) - 在这种情况下, flex 仅可用于使该子对象居中(此处不是这种情况),但没有别的。

我删除侧边距只是将其移动.navbar_wrapper到最右边(根据其right: 0;作为绝对定位元素的设置)并将.logo元素(没有position设置)移动到左边。正如我所说 - 与 flexbox 设置无关,而是与.navbar_wrapper.

但回到您的 flexbos 设置:可以将justify-content: space-between;设置section移动<header>到实际有效。在这种情况下,您不需要绝对位置.navbar_wrapper(IMO 将是更好的解决方案)。

@import url('https"//fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap');
* {
  margin: 0;
  padding: 0;
  /* box-sizing: border-box; */
  font-family: "Poppins", sans-serif;
}

.header {
  position: fixed;
  height: 70px;
}

.logo {
  align-items: center;
  display: inline-block;
  margin-top: 20px;
}

.navbar_wrapper {
  position: absolute;
  right: 0;
  top: 0;
  height: 94px;
   margin-top: 20px;
 display: flex;
  align-items: center;
}

.navbar_wrapper>a:not(:last-child) {
  margin-right: 40px;
  text-decoration: none;
  color: black;
  cursor: pointer;
}

.navbar_wrapper>a:nth-child(4) {
  text-decoration: none;
  color: black;
  cursor: pointer;
}

.sb_logo {
  height: 50px;
  width: 50px;
}

.section {
  position: relative;
  width: 100%;
  min-height: 100vh;
  padding: 100px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
}

.navbar_wrapper {
  display: inline-block;
}

.header {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  padding: 20px 100px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="style.css" />
  <title>Document</title>
</head>

<body>
  <section>
    <header>
      <a href="#" class="logo">
        <img src="images/logo.png" alt="" class="sb_logo" />
      </a>
      <div class="navbar_wrapper">
        <a href="#" class="home">Home</a>
        <a href="#" class="menu">Menu</a>
        <a href="#" class="what's new">What's New</a>
        <a href="#" class="contact">Contact</a>
      </div>
    </header>
  </section>
</body>

</html>


推荐阅读