汉堡菜单未正确居中

Burger menu not centred properly

我在纯 CSS 中创建了一个汉堡菜单,但问题是由于某种原因它以左侧而不是中间为中心。我真的不明白为什么。

标记:

<section id="header">
    <a href="#menu" class="box-shadow-menu" id="navTrigger">
        <div class="navicon">
        </div>
    </a>
</section>

CSS:

#header {
    width: 100%;
    height: 45px;
    background-color: #4dc1df;
    position: fixed;
    z-index: 100;
}

.navicon {
    position: fixed;
    height: 45px;
    left: 50%;
    margin-left: -17.5px;
}

.box-shadow-menu {
    position: relative;
    display: block;
    height: 45px;
}

.box-shadow-menu:before {
    content: "";
    position: absolute;
    top: 10px;
    width: 35px;
    height: 4px;
    background: white;
    box-shadow: 0 10px 0 0 white, 0 20px 0 0 white;
}

演示:http://jsfiddle.net/jLhnr12p/1/.

如能帮助我们正确居中,我们将不胜感激!

您的代码:

.navicon {
  position: fixed;
  left: 50%;
  right: 50%;
}

你不能这样居中,你可以通过将左 属性 设置为 50% 并设置负 margin-left (导航图标的一半,即 17.5px)来实现;

.navicon {
  left: 50%;
  margin-left: -17.5px; // or transform: translateX(-50%);
}