当媒体查询启动时,为什么导航栏离开屏幕左侧?

时间:2015-04-12 22:16:01

标签: html css layout media-queries

当屏幕低于450px时,徽标会消失以显示导航空间,但导航会从屏幕左侧偏离约20px。

http://codepen.io/briligg/pen/emwXaw?editors=110

我相信这是相关的代码 - 我可能已经包含了超过必要的代码。 CSS:

@media screen and (max-width: 450px) {
    img#logo {
    display: none;
    width: 0;
    }
    nav {
    width: 100%;
    min-width: 100%;
    }
}
div#top{
        position: fixed;
        top: 0px;
        width: 100%;
        height: 130px;
        z-index: 5;

}
img#logo {
        border: 0;
        float: left;    
        width: 20%;
        margin-right: 2%;
        margin-left: 2%;
        margin-top: 5px;
        max-width: 123px;
        }


nav {       position: fixed;
            top: 0px;
            right: 0px;
            width: 70%;
            float: right;
            padding: 2%;
            height: 60px;
            max-height: 60px;
            margin: 5px 5px;
            }
nav button {
            padding: 0 4px;
            height: 28px;
            font: 16px;
}

nav button ul {
            position: relative;
            display: none;
}

nav button:hover ul, nav button:focus ul {
            display: block;
            z-index: 6;
            list-style: none;
            padding: 4px;
}

nav button:hover li, nav button:focus li {
            padding: 4px;
}
nav a {
        text-decoration: none;
        color: white;
}
nav a:hover, nav a:focus {
            color: #9dab71;
}

这是相关的HTML:

<div id="top">
<a href="default.html"><img id="logo" src="http://www.briligg.com/images/briligg-loopless-blue.png" 
alt="briligg home" /></a>
<nav>
<button><a href="futuremoon.html#begin">Purpose</a></button>
<button><a href="moonvsmars.html">Moon vs Mars</a>
   <ul>
     <li><a href="moonvsmars.html#ambiance">Ambiance</a></li>
     <li><a style="border-bottom: 1px solid #666666;" href="moonvsmars.html#communication">Communication</a></li>
     <li><a href="thereandback.html">There and Back</a></li>
</ul>
</button>
<button><a href="beingthere.html">Being There</a>
<ul>
        <li><a href="beingthere.html#domination">World Domination</a></li>
        <li><a href="beingthere.html#chickens">Chickens</a></li>
        <li><a href="beingthere.html#gravity">Down with Gravity</a></li>
        <li><a href="beingthere.html#moonstar">The Moonstar</a></li>
        </ul>
</button>
</nav>
</div>

1 个答案:

答案 0 :(得分:1)

你已经给导航元素min-width提供了100%...因为盒子模型的工作方式,添加填充和边距会强制元素比视口宽。

您可以通过向导航元素添加box-sizing: border-box;来解决此问题。这将强制任何填充或边框作为宽度包括在内。查看更多详情here

我建议您阅读w3schools框模型的工作方式,并相应地调整填充和边距。