html - 导航栏下的新内容在关闭 </nav></div> 标签后结束在导航栏内

标签 html css

我对新网站主页的导航栏、 Logo 和背景图片感到满意。我的问题是当我尝试在结束导航标签后添加新的 div、部分或图像时,新内容被插入到导航栏中。我不明白为什么导航栏会插入一个新的内容区域。

我的目标是在主页中央放置一些东西(例如:图片、H1/文本等)。

我已经尝试在关闭的导航和 div 标签下添加新的 div、部分和/或图像。从那里我尝试设计新内容的样式,但出于某种原因,我无法将内容移动到正确的位置。

* {
  box-sizing: border-box;
}

html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100vh;
}

body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100vh;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  font-weight: 400;
  background: url(images/img13.jpg);
  background-position: center;
  background-size: cover;
}

nav {
  padding: 8px;
}

.logo {
  float: left;
  padding: 8px;
  margin-left: 16px;
  margin-top: 8px;
}

.logo a {
  color: rgb(255, 255, 255);
  text-transform: uppercase;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0px;
  text-decoration: none;
}

nav ul {
  float: right;
}

nav ul li {
  display: inline-block;
  float: left;
}

nav ul li:not(:first-child) {
  margin-left: 48px;
}

nav ul li:last-child {
  margin-right: 24px;
}

nav ul li a {
  display: inline-block;
  outline: none;
  color: rgb(255, 255, 255);
  text-transform: uppercase;
  text-decoration: none;
  font-size: 14px;
  letter-spacing: 1.2px;
  font-weight: 600;
}

@media screen and (max-width: 864px) {
  .logo {
    padding: 0;
  }

  .nav-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    background: rgba(71, 8, 71, 0.507);
    opacity: 0;
    transition: all 0.2s ease;
  }

  .nav-wrapper ul {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 100%;
  }

  .nav-wrapper ul li {
    display: block;
    float: none;
    width: 100%;
    text-align: right;
    margin-bottom: 10px;
  }

  .nav-wrapper ul li:nth-child(1) a {
    transition-delay: 0.2s;
  }

  .nav-wrapper ul li:nth-child(2) a {
    transition-delay: 0.3s;
  }

  .nav-wrapper ul li:nth-child(3) a {
    transition-delay: 0.4s;
  }

  .nav-wrapper ul li:nth-child(4) a {
    transition-delay: 0.5s;
  }

  .nav-wrapper ul li:not(:first-child) {
    margin-left: 0;
  }

  .nav-wrapper ul li a {
    padding: 10px 24px;
    opacity: 0;
    color: rgb(255, 255, 255);
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 1.2px;
    transform: translateX(-20px);
    transition: all 0.2s ease;
  }

  .nav-btn {
    position: fixed;
    right: 10px;
    top: 10px;
    display: block;
    width: 48px;
    height: 48px;
    cursor: pointer;
    z-index: 9999;
    border-radius: 50%;
  }

  .nav-btn i {
    display: block;
    width: 20px;
    height: 2px;
    background: rgb(255, 255, 255);
    border-radius: 2px;
    margin-left: 14px;
  }

  .nav-btn i:nth-child(1) {
    margin-top: 25px;
  }

  .nav-btn i:nth-child(2) {
    margin-top: 4px;
    opacity: 1;
  }

  .nav-btn i:nth-child(3) {
    margin-top: 4px;
  }
}

#nav:checked + .nav-btn {
  transform: rotate(45deg);
}

#nav:checked + .nav-btn i {
  background: rgb(255, 255, 255);
  transition: transform 0.2s ease;
}

#nav:checked + .nav-btn i:nth-child(1) {
  transform: translateY(6px) rotate(180deg);
}

#nav:checked + .nav-btn i:nth-child(2) {
  opacity: 0;
}

#nav:checked + .nav-btn i:nth-child(3) {
  transform: translateY(-6px) rotate(90deg);
}

#nav:checked ~ .nav-wrapper {
  z-index: 9990;
  opacity: 1;
}

#nav:checked ~ .nav-wrapper ul li a {
  opacity: 1;
  transform: translateX(0);
}

.hidden {
  display: none;
}
<div class="container">
  <nav>
    <input type="checkbox" id="nav" class="hidden">
    <label for="nav" class="nav-btn">
      <i></i>
      <i></i>
      <i></i>
    </label>
    <div class="logo">
      <a href="#"><img src="images/BMlogo0011.png"></a>
    </div>
    <div class="nav-wrapper">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Events</a></li>
        <li><a href="#">Artists</a></li>
        <li><a href="#">Email</a></li>
      </ul>
    </div>
  </nav>
</div>

我的目标是在主页中央放置一些东西(例如:图片、H1/文本等)。

最佳答案

我已经删除了 logonav-wrapper 的 float 元素,以避免与另一个 div 重叠。我使用 flex-box 来对齐 Logo 和菜单。

并调整媒体查询响应。我希望这个解决方案对您有所帮助。

* {
    box-sizing: border-box;
}

html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100vh;
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100vh;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-weight: 400;
    background: url(https://via.placeholder.com/1920x600);
    background-position: center;
    background-size: cover;
}

.container {
    width: 100%;
    padding: 0 20px;
}

nav {
    display: flex;
}

.logo img {
    max-width: 100%;
}

.logo {
    padding: 10px 0;
    /* float: left;
      margin-left: 16px;
      margin-top: 8px; */
}

.logo a {
    color: rgb(255, 255, 255);
    text-transform: uppercase;
    font-weight: 700;
    font-size: 18px;
    letter-spacing: 0px;
    text-decoration: none;
}

.nav-wrapper {
    margin-left: auto;
}

nav ul li {
    display: inline-block;
}

nav ul li:not(:first-child) {
    margin-left: 48px;
}

nav ul li:last-child {
    margin-right: 24px;
}

nav ul li a {
    display: inline-block;
    outline: none;
    color: rgb(255, 255, 255);
    text-transform: uppercase;
    text-decoration: none;
    font-size: 14px;
    letter-spacing: 1.2px;
    font-weight: 600;
}

@media screen and (max-width: 864px) {
    .nav-wrapper {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: -1;
        background: rgba(71, 8, 71, 0.507);
        opacity: 0;
        transition: all 0.2s ease;
    }
    .nav-wrapper ul {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 100%;
        margin-top: 30px;
    }
    .nav-wrapper ul li {
        display: block;
        float: none;
        width: 100%;
        text-align: right;
        margin-bottom: 10px;
    }
    .nav-wrapper ul li:nth-child(1) a {
        transition-delay: 0.2s;
    }
    .nav-wrapper ul li:nth-child(2) a {
        transition-delay: 0.3s;
    }
    .nav-wrapper ul li:nth-child(3) a {
        transition-delay: 0.4s;
    }
    .nav-wrapper ul li:nth-child(4) a {
        transition-delay: 0.5s;
    }
    .nav-wrapper ul li:not(:first-child) {
        margin-left: 0;
    }
    .nav-wrapper ul li a {
        padding: 10px 24px;
        opacity: 0;
        color: rgb(255, 255, 255);
        font-size: 14px;
        font-weight: 600;
        letter-spacing: 1.2px;
        transform: translateX(-20px);
        transition: all 0.2s ease;
    }
    .nav-btn {
        position: fixed;
        right: 15px;
        top: 25px;
        display: block;
        width: 30px;
        height: 40px;
        cursor: pointer;
        z-index: 9999;
        border-radius: 50%;
    }
    .nav-btn i {
        position: fixed;
        display: block;
        width: 20px;
        height: 2px;
        background: rgb(255, 255, 255);
        border-radius: 2px;
        right: 15px;
    }
    .nav-btn i:nth-child(2) {
        margin-top: 6px;
        opacity: 1;
    }
    .nav-btn i:nth-child(3) {
        margin-top: 12px;
    }
}

#nav:checked + .nav-btn {
    transform: rotate(45deg);
}

#nav:checked + .nav-btn i {
    background: rgb(255, 255, 255);
    transition: transform 0.2s ease;
}

#nav:checked + .nav-btn i:nth-child(1) {
    transform: translateY(6px) rotate(180deg);
}

#nav:checked + .nav-btn i:nth-child(2) {
    opacity: 0;
}

#nav:checked + .nav-btn i:nth-child(3) {
    transform: translateY(-6px) rotate(90deg);
}

#nav:checked ~ .nav-wrapper {
    z-index: 9990;
    opacity: 1;
}

#nav:checked ~ .nav-wrapper ul li a {
    opacity: 1;
    transform: translateX(0);
}

.hidden {
    display: none;
}
<div class="container">
    <nav>
        <input type="checkbox" id="nav" class="hidden">
        <label for="nav" class="nav-btn">
            <i></i>
            <i></i>
            <i></i>
        </label>
        <div class="logo">
            <a href="#"><img src="https://placeholder.com/wp-content/uploads/2018/10/placeholder.com-logo1.png" width="140"></a>
        </div>
        <div class="nav-wrapper">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Events</a></li>
                <li><a href="#">Artists</a></li>
                <li><a href="#">Email</a></li>
            </ul>
        </div>
    </nav>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
</div>

关于html - 导航栏下的新内容在关闭 </nav></div> 标签后结束在导航栏内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55348785/

相关文章:

javascript - 在 JavaScript Canvas 中绘制 Arc 3 鼠标点

HTML 代码太长

css - 为什么 float 下降?

html - 如何更改html中单个列的背景颜色?

css - 不同浏览器(显示: -webkit-inline-box;)

javascript - HTML, CSS, JS if 语句认为 1==0

javascript - jQuery slideUp 然后在鼠标悬停时 slideDown

javascript - getElementsByTagName().length 在 Firefox/Internet Explorer 中不起作用

javascript - 使用 webpack 打包 CSS

css - 使用控制 - 和 + 时,使用 vw 的响应文本无法正确显示