简体   繁体   English

CSS顶部菜单栏

[英]CSS top menu bar

I'm trying to create a top menu bar like in stackoverflow (but with fixed position). 我正在尝试像在stackoverflow中创建一个顶部菜单栏(但位置固定)。

JSFIDDLE Demo

There is a space on the left and at the top of the menu bar. 菜单栏的左侧和顶部都有一个空格。 How can I remove that space? 如何删除该空间?

Thank you in advance... 先感谢您...

By reseting margin & padding : http://jsfiddle.net/yw4e69qo/1/ 通过重置margin & paddinghttp : //jsfiddle.net/yw4e69qo/1/

Simply 只是

html * {
    margin: 0;
    padding: 0;
}

But more comprehensively - dive deeper into this topic: http://meyerweb.com/eric/tools/css/reset/ 但是更全面-请更深入地研究此主题: http : //meyerweb.com/eric/tools/css/reset/

It's very useful :) 这非常有用:)

body{
 margin:0;
 padding:0;
}

#nav{
margin:0
}

This will solve you problem without changing the menu postiton of menu items 这将解决您的问题,而无需更改菜单项的菜单位置

#nav {
    padding:0;
    margin:0;

}

ul elements always have a default padding on the left side because of the list-style property. 由于list-style属性, ul元素始终在左侧具有默认padding If you inspect the element in the console you can see exactly what is creating the margins you see. 如果您在控制台中检查元素,则可以确切看到正在创建的边距。 So simply putting the following will fix the problem for you: 因此,只需输入以下内容即可为您解决问题:

#nav {
    margin: 0;
    padding: 0;
}

Also you should look into using a reset.css file so you don't have to do the resetting like this yourself on every list on your page. 另外,您应该研究使用reset.css文件,这样就不必在页面上的每个列表中自己进行这种重置。 Using global selectors in your css like so: 在CSS中使用全局选择器,如下所示:

ul {
    margin: 0;
    padding: 0;
}

to reset all elements is considered bad practice since it will slow down the execution of the code since it always have to check if the selector is part of the global selector. 重置所有元素被认为是不好的做法,因为这将减慢代码的执行速度,因为它始终必须检查选择器是否是全局选择器的一部分。 Hope this answer helps. 希望这个答案有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM