CH26
CH26

Reputation: 63

How to get the navigation bar to stretch across the entire width of page HTML CSS

I'm trying to create a website and I'm having trouble trying to get the menu bar to stretch across the width of the web page. Can somebody please help? Here is my CSS/HTML for the menubar:

ul {
    list-style-type: none;
    margin: 0px;
    padding-left: 2px;
    position: fixed;
    top: 0px;
    word-spacing: 2px;
}
li { 
    float: left;
    display: inline;
    word-spacing: 2px;
    padding-left: 70px;
    background-color: #610000;
}
#menubar a{
    display: inline;
    text-align: center;
    text-decoration: none;
    font-size: 15px;
    font-family: PT sans, sans-serif;
    color: #FFDFC1;
    padding-left:0px;	
}


#menubar a:hover {
    color: #092601;
    background-color:#610000;
}
#menubar .active{
    color: #092601;
}

body{
    background-color: #ffffff;
    margin-left: 350px;  
}
<div id="menubar">
    <ul>
        <li><a href="home.html">Home</a></li>
        <li><a href="about.html">About Us</a></li>
        <li><a href="menu.html">Menu</a></li>
        <li><a href="location.html">Location</a></li>
        <li><a href="gallery.html">Gallery</a></li>
        <li><a href="contact.html">Contact Us</a></li>
    </ul>
</div>

Upvotes: 2

Views: 2935

Answers (4)

Jeff Puckett
Jeff Puckett

Reputation: 41061

li { 
    background-color: #610000;
}
#menubar a{
    text-decoration: none;
    font-size: 15px;
    font-family: PT sans, sans-serif;
    color: #FFDFC1;
}
#menubar a:hover {
    color: #092601;
    background-color:#610000;
}
#menubar .active{
    color: #092601;
}
ul{
    padding: 0;
    display: -webkit-box;
    display: -moz-box;
    display: box;
}
li{
    list-style: none;
    list-style:none;
    text-align: center;
    -webkit-box-flex: 1;
    -moz-box-flex: 1;
    box-flex: 1;
}
body {
  margin: 0px;
}
<div id="menubar">

    <ul>
                <li><a href="home.html">Home</a></li>
                <li><a href="about.html">About Us</a></li>
                <li><a href="menu.html">Menu</a></li>
                <li><a href="location.html">Location</a></li>
                <li><a href="gallery.html">Gallery</a></li>
                <li><a href="contact.html">Contact Us</a></li>
    </ul>
</div>

Upvotes: 1

Michael Eugene Yuen
Michael Eugene Yuen

Reputation: 2528

1) Give html and body width: 100%

2) Give the ul width: 100%

3) Remove the body margin

4) Give the ul background-color instead of li and you will notice the difference

html, body {
   width: 100%;
   margin: 0;
}

body {
   background-color: #ffffff;
}

ul {
   position: fixed;
   top: 0;
   left: 0;
   list-style: none;
   margin: 0px;
   padding-left: 2px;
   word-spacing: 2px;
   background-color: #610000;
}

li { 
    float: left;
    word-spacing: 2px;
    padding-left: 70px;

}

#menubar {
  display: inline-block;
  width: 100%;
}

#menubar a {
  text-align: center;
  text-decoration: none;
  font-size: 15px;
  font-family: PT sans, sans-serif;
  color: #FFDFC1;
  padding-left:0px;
}


#menubar a:hover {
  color: #092601;
  background-color: #610000;
}

#menubar .active{
  color: #092601;
}
 <ul id="menubar">
    <li><a href="home.html">Home</a></li>
    <li><a href="about.html">About Us</a></li>
    <li><a href="menu.html">Menu</a></li>
    <li><a href="location.html">Location</a></li>
    <li><a href="gallery.html">Gallery</a></li>
    <li><a href="contact.html">Contact Us</a></li>
  </ul>

https://jsfiddle.net/35c5b93q/

Upvotes: 0

rmondesilva
rmondesilva

Reputation: 1792

To make it relative to your screen size.

Use vw unit. So to fit it, make it 100vw on your ul..

Then in your li .. Remove the padding-left there..and then

set its width as width of parent ul divided by number of child li..

width: calc(100%/6);

NOTE: calc() function only works on modern browsers.. You can set it manually if you want.

ul {
        list-style-type: none;
    	margin: 0px;
    	padding-left: 2px;
    	position: fixed;
    	top: 0px;
    	word-spacing: 2px;
        width: 100vw;
    }
    li { 
    	float: left;
        width: calc(100%/6);
    	display: inline;
    	word-spacing: 2px;
    	background-color: #610000;
    }
    #menubar {
      width: 100%;
    }
    #menubar a{
    	display: inline;
    	text-align: center;
        text-decoration: none;
    	font-size: 15px;
    	font-family: PT sans, sans-serif;
        color: #FFDFC1;
        padding-left:0px;
        
    	
    }
    
    
    #menubar a:hover {
        color: #092601;
    	background-color:#610000;
    }
    #menubar .active{
    	color: #092601;
    }
    
    body{
    	background-color: #ffffff;
    }
<div id="menubar">

    <ul>
    		<li><a href="home.html">Home</a></li>
    		<li><a href="about.html">About Us</a></li>
    		<li><a href="menu.html">Menu</a></li>
    		<li><a href="location.html">Location</a></li>
    		<li><a href="gallery.html">Gallery</a></li>
    		<li><a href="contact.html">Contact Us</a></li>
    </ul>
</div>

Upvotes: 0

Nouman Bhatti
Nouman Bhatti

Reputation: 1421

why you have set the margin-left for the body in your css remove that

this might help you too

https://stackoverflow.com/a/11520529/2430556

Upvotes: 0

Related Questions