我想制作一个固定的导航栏.但现在菜单上方有空间.我尝试了任何我知道的解决方法,但它没有用.我在整个代码中搜索了一些创建空间的东西,但我找不到它.
#nav { position: fixed; text-align: center; font-size: 22px; background-color: #222222; margin: 0 auto; width: 100%; } #nav ul li a { color: #ccc; display: block; padding: 10px; text-decoration: none; } #nav ul { list-style-type: none; padding: 0; margin: 0; position: relative; } #nav ul li { display: inline-block; } #nav ul li:hover { background: #333333; } #nav ul li a:hover { text-shadow: 3px 2px 3px #333333; text-decoration: none; position: relative; bottom: 5px; color: #fdde00; } html,body { height: 100%; width: 100%; padding: 0; margin: 0; }
<div id="nav"> <ul> <li><a href="#">Test</a></li> <li><a href="#">Test</a></li> <li><a href="#">Test</a></li> <li><a href="#">Test</a></li> </ul> </div> <div> <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus. Donec quam felis,ultricies nec,pellentesque eu,pretium quis,sem. Nulla consequat massa quis enim. Donec pede justo,fringilla vel,aliquet nec,vulputate eget,arcu. In enim justo,rhoncus ut,imperdiet a,venenatis vitae,justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula,porttitor eu,consequat vitae,eleifend ac,enim. Aliquam lorem ante,dapibus in,viverra quis,feugiat a,tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. orem ipsum dolor sit amet,justo. Nullam</p> </div>
解决方法
此行为称为
collapsing margins.顶部的间隙是因为< p>而创建的. element具有默认边距.
3个解决方法:
>使用p {margin:0;}删除p元素的默认边距
>添加顶部:0;到.nav元素
>添加溢出:隐藏;到第二个div
有关如何防止折叠边距的更多信息,请参阅How to disable margin-collapsing?