简体   繁体   English

如何使导航栏覆盖整个页面?

[英]How do I get my navigation bar to cover the whole page?

I am fairly new to this kind of stuff. 我对这种东西还很陌生。 I just want my nav bar to cover the page from side to side instead of having the white spaces on both sides and the top. 我只希望导航栏从一侧到另一侧覆盖页面,而不是在两侧和顶部都留有空白。

Here is my CSS 这是我的CSS

nav ul {list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #3E3F43;}

nav li {float: left;}

nav li a {display: block;
       color: white;
   text-align: center;
   padding: 14px 16px;
   text-decoration: none;}

li a:hover{background-color: #7559A6;}


Here is my HTML

<nav>

    <ul>
        <li><a href="home.html" class="active">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="resume.html">Resume</a></li>
        <li><a href="blog.html">Blog</a></li>
        <li><a href="contact.html">Contact</a></li>
    </ul>

</nav>

give your nav an id 给您的导航ID

   <nav id=nav">

And set it's width to 100% 并将其宽度设置为100%

    #nav{
    width:100%;
    }

Firstly, remove the default margin from the body 首先,从body删除默认边距

 body {
      margin: 0;
    }

Then: 然后:

CSS tables CSS表

 body { margin: 0; } nav ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #3E3F43; display: table; width: 100%; } nav li { display: table-cell } nav li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { background-color: #7559A6; } 
 <nav> <ul> <li><a href="home.html" class="active">Home</a> </li> <li><a href="about.html">About</a> </li> <li><a href="resume.html">Resume</a> </li> <li><a href="blog.html">Blog</a> </li> <li><a href="contact.html">Contact</a> </li> </ul> </nav> 

Flexbox Flexbox的

 body { margin: 0; } nav ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #3E3F43; display: flex; } nav li { flex:1; } nav li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { background-color: #7559A6; } 
 <nav> <ul> <li><a href="home.html" class="active">Home</a></li> <li><a href="about.html">About</a></li> <li><a href="resume.html">Resume</a></li> <li><a href="blog.html">Blog</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> 

添加你的CSS

nav{ position: absolute; width: 100%;left: 0;top: 0;}

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

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