无法使固定位置的Flexbox居中:$ auto

时间:2015-08-05 06:37:35

标签: css css-position margin flexbox centering

我在使用margin: 0 auto;定位固定位置的flexbox时遇到了问题。我可以将位置更改为相对位置以使其看起来像我想要的,但是固定是我希望的。

请在JSBIN output上查看,如果您想see the code

HTML

<header class="header l-container">
    <div class="site-title"> <a href="#" class="link">Home</a></div>
    <nav class="nav-box">
        <ul class="menu-box">
            <li><a href="about.html" class="about link">About</a></li>
            <li><a href="/blog/" class="blog link">Blog</a></li>
            <li><a href="contact.html" class="contact link">Contact</a></li>
        </ul>
    </nav>
</header>

CSS

/* change position into relative or fixed */

.header {
  position: fixed;
    z-index: 100;
}


/* some other settings below */

.l-container { 
    margin: 0 auto;
    padding: 0 6rem;
    max-width: 108rem;
}

.header,
.menu-box {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
}

.header {
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
      -ms-flex-pack: justify;
          justify-content: space-between;
}

  .site-title .link {
      float: left;
  }

  .menu-box .link {
      float: right;
  }




ul {list-style: none;}
li {margin-left: 20px;}
.link {font-size: 20px;}

注意:我想将整个.header容器居中,并使用flexbox将.site-title.nav-box分开.header的两端。

2 个答案:

答案 0 :(得分:2)

如何将left: 0right: 0添加到固定.header?仍会应用max-widthmarginpadding,但它会从左侧和右侧定位。

JSBin

.header {
  position: fixed;
  z-index: 100;
  left: 0;
  right: 0;
}

答案 1 :(得分:0)

它会起作用

{{1}}
相关问题