Forum Moderators: not2easy

Message Too Old, No Replies

Navigation bar won't colour across and centre

         

Jamier101

5:49 pm on Nov 27, 2010 (gmt 0)

10+ Year Member



I've been looking at having my main navigation bar centralise, the issue I have is that if I declare a set width for the bar then I can make it centre using the 'margin:0 auto;' function however then the bar will not be coloured all the way across, it will only do it for the width of the bar. If I choose to have the bars width set to auto then I can only have the boxes float left, must I have this trade off?

CSS

#navigation {
width:800px;
height:auto;
margin:0 auto;
background-color:#999999;
}

#navigation ul {
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}

#navigation li {
float:left;
}

#navigation a:link, #navigation a:visited {
display:block;
width:120px;
font-weight:normal;
color:#FFFFFF;
background-color:#999999;
text-align:center;
padding:4px;
text-decoration:none;
text-transform:uppercase;
}

#navigation a:hover, #navigation a:active {
background-color:#333333;
}


HTML

<div id="navigation">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="search_villas.php">Search</a></li>
<li><a href="advertise_villa.html">Advertise</a></li>
<li><a href="login.php">Owners Login</a></li>
<li><a href="about.php">About Us</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>


You will notice that at present I have given the bar a set width of 800px and it is centralised on my page but the grey ends will not stretch across my page which is 1000px wide.

alt131

3:23 am on Nov 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jamier,
Have you tried removing the width from #navigation (or setting width:100%), and applying the width:800px and margin: 0 auto to #navigation ul instead?

milosevic

10:42 am on Nov 30, 2010 (gmt 0)

10+ Year Member



Try adding an outer container div to your main navigation bar that will produce the full width background, while the inner div inside is centered with auto margin.

Jamier101

12:13 pm on Nov 30, 2010 (gmt 0)

10+ Year Member



Try adding an outer container div to your main navigation bar that will produce the full width background, while the inner div inside is centered with auto margin.


This thought occurred to me last night, I think I'll give the other solution ago and if that doesn't work for me I'll just pop it inside another container.

birdbrain

12:22 pm on Nov 30, 2010 (gmt 0)



Hi there Jamier101,

try it like this...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">

<title>untitled document</title>

<style type="text/css">
#navigation {
background-color:#999;
overflow:hidden;
}
#navigation ul {
float:left;
position:relative;
left:50%;
margin:0;
padding:0;
list-style-type:none;
text-align:center;
}
#navigation li {
float:left;
display:block;
position:relative;
right:50%;
}
#navigation a:link,#navigation a:visited {
display:block;
padding:4px;
margin-right:2px;
font-weight:normal;
color:#fff;
text-decoration:none;
text-transform:uppercase;
}
#navigation a:hover,#navigation a:active {
background-color:#333;
}
</style>

</head>
<body>

<div id="navigation">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="search_villas.php">Search</a></li>
<li><a href="advertise_villa.html">Advertise</a></li>
<li><a href="login.php">Owners Login</a></li>
<li><a href="about.php">About Us</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>

</body>
</html>

birdbrain

alt131

12:48 pm on Nov 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi milsovic,

Try adding an outer container div to your main navigation bar that will produce the full width background, while the inner div inside is centered with auto margin.
It is certainly an option - but can you explain why you would choose to add an extra html element when the desired visual style should be achievable with the existing elements?

Jamier101

6:03 pm on Nov 30, 2010 (gmt 0)

10+ Year Member



Have you tried removing the width from #navigation (or setting width:100%), and applying the width:800px and margin: 0 auto to #navigation ul instead?


I just tried this and its worked a treat, thanks :)