Skip to content
WJunction - Webmaster Forum

CSS Margin Help

Status
Not open for further replies.
Hello,

I'm making a website with CSS & HTML and I'm a Beginner.
In my navigation bar there's some extra white space coming at the right.
I tried to eliminate it with CSS Margin property but I'm failed to do so.

Please have a look and help me.

http://gn.hostoi.com/

Index Page Source Code
Code:
<html>
<head>

<link rel="icon" 
      type="image/png" 
      href="...\images\fevicon.jpg">

<link rel="stylesheet" type="text/css" href="stylesheet1.css">
<title> Gamer's Nation -Beta </title>
<script type="text/javascript"></script><link rel='stylesheet' type='text/css' href='/B1D671CF-E532-4481-99AA-19F420D90332/netdefender/hui/ndhui.css' /></head>
<body><script type='text/javascript' language='javascript' src='/B1D671CF-E532-4481-99AA-19F420D90332/netdefender/hui/ndhui.js?0=0&amp;0=0&amp;0=0'></script>
<div id="header">
<h1>Welcome to Gamer's Nation </h1>
</div>
<div id="nav">
<li><a href="index.html">Home</a></li>
<li><a href="games.html">Games</a></li>
<li><a href="trailers.html">Trailers</a></li>
<li><a href="contact.html">Contact Us</a></li>
</div>
</body>
</html>
Stylesheet

Code:
#nav li {
	float: left; 
}

#nav {	
	margin-top:20px;
	margin-bottom:100px;
	margin-right:10px;
	margin-left:50px;	
	width: 100%;
	float: left;
	padding: 10;
	list-style: none;
	background-color: white;
	border-bottom: 1px solid #ccc; 
	border-top: 1px solid #ccc; }
#nav li a {
		display: block;
		padding: 8px 90px;
		text-decoration: none;
		font-weight: bold;
		color:grey;
		border-right: 1px solid #ccc; }
#nav li a:hover {
		color: green;
		background-color: #fff; }
 

6 comments

This is the cause:
PHP:
#nav {      
    width: 100%;
}
Since there's a margin right of 10px, 100% width + 10px makes it extend to right.
You can fix it in several ways, first of which is that you change width property of #nav to something less (-10px). Or simply remove the margin-left property Or:

Add this:
PHP:
<div id="super"> 
<div id="nav">
<li><a href="index.html">Home</a></li>
<li><a href="games.html">Games</a></li>
<li><a href="trailers.html">Trailers</a></li>
<li><a href="contact.html">Contact Us</a></li>
</div>
</div>
And in your css file, remove width from #nav and add:
PHP:
     #super {    margin: 20px 10px 100px 50px;}
 
Last edited:
Hi,
Thanks for the help, apparently width 100% was the cause and after removing it, I m able to move my navigation bar left and right by specifying different values.

However, I'm not being able to position it in center with margin-left:#value or maegin-right:#value. And when I change float:left; to float:center;
Its coming something like this
gn.hostoi.com
 
The link is not working.

---------- Post added at 04:39 PM ---------- Previous post was at 03:37 PM ----------

This is the cause:
PHP:
#nav {      
    width: 100%;
}
Since there's a margin right of 10px, 100% width + 10px makes it extend to right.
You can fix it in several ways, first of which is that you change width property of #nav to something less (-10px). Or simply remove the margin-left property Or:

Add this:
PHP:
<div id="super"> 
<div id="nav">
<li><a href="index.html">Home</a></li>
<li><a href="games.html">Games</a></li>
<li><a href="trailers.html">Trailers</a></li>
<li><a href="contact.html">Contact Us</a></li>
</div>
</div>
And in your css file, remove width from #nav and add:
PHP:
     #super {    margin: 20px 10px 100px 50px;}

Hey dude, I did the first method you told me by removing width. It worked but later I tried aligning the nav bar center and things got messy.
So I tried setting value of width to -10px and it worked.
Now the white space has gone.
Thanks..
can you tell me how to align it in center now?

---------- Post added at 04:44 PM ---------- Previous post was at 04:39 PM ----------

To align the navbar in centre, you must specify the width of it and then add margin:0 auto;

Also, your list doesn't have the <ul></ul> tag.

I've edited the files and made it to your needs.

Have a look: http://feverfile.com/files/4yAvGy1357216951.html

Hi, I did specify width: -10px ; and margin: 0 auto; but nothing changed.

And thanks for pointing out ul think, I added it now.
 
It may sound surprising, but the width property sets the width of the element. Negative values are not allowed.
Also, float: center doesn't work either, float only accepts left or right as values. Use the suggested method: set a (non-negative) width for your element and add margin: 0 auto; (which means no margin on top and bottom, and auto margin for left and right)
 
Status
Not open for further replies.

About the author

0
New Member · Joined
3
Messages
0
Reactions
1
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom