使绝对定位的项目居中

时间:2019-06-24 19:12:49

标签: html css

我正在尝试将绝对定位的导航居中,但在大屏幕尺寸的右侧有多余的空间。

我查看了其他一些类似的StackOverflow问题,并尝试将以下内容添加到ul:

left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
*, *:before, *:after { box-sizing: border-box; margin: 0; padding: 0;
border: none;}

.section-border {
  background-color: #f8f7f3;
  min-height: 100vh;
  border: 30px solid #ffffff;
}

.header {
  max-width: 960px;
  height: 100%;
  margin-left: 1rem;
  margin-right: 1rem;
  position: relative;
}

.menu {
  display: flex;
  position: absolute;
  top: -0.83rem;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 960px;
  line-height: 1.5em;
}

li {
  display: inline-block;
  flex-grow: 1;
  font-size: 14px;
}

@media only screen and (min-width: 601px) {
  .home-link {
    order: -1;
    flex-grow: 0;
  }

  li {
    text-align: right;
  }
}

<div class="section-border">
  <header class="header">
    <nav role="navigation">
      <ul class="menu">
        <li class="home-link" aria-hidden="true"><a href="#"><img src="https://www.nicolefenton.com/_/images/dec/circle-menu.svg" height="12" width="12" alt=""></a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Writing</a></li>
        <li><a href="#">Speaking</a></li>
        <li><a href="#">Projects</a></li>
      </ul>
    </nav>
  </header>
</div>

我希望ul居中放置,而不考虑视口大小。

0 个答案:

没有答案