Forum Moderators: not2easy

Message Too Old, No Replies

CSS horizontal Buttons

         

juliejayne

4:24 pm on May 2, 2005 (gmt 0)

10+ Year Member



OK, here is the problem.

I have 10 buttons, created with CSS, each is 105px wide.

I want them to be centered across the page, irrespective of what resolution.

Strangely enough I have it working in IE, but I can't get it to work in FF.

I have spent days working through various horizontal nav tutorials, but they all seem to rely on float: left

Any ideas anyone.

Longhaired Genius

6:28 pm on May 2, 2005 (gmt 0)

10+ Year Member



You're right, this is trickier than it ought to be. This "solution" is not perfect: It doesn't look right in IE5, "display: inline" means you can't have a global width for the buttons and you have to adjust them individually if uniform width is important. But it's simple, and that what matters to me at the moment.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<style type="text/css">
ul {
padding: 0.4em;
text-align: center;
margin: auto;
border: 1px solid #ff00ff; /* magenta border */
}
li {
display: inline;
padding: 0.2em;
border: 1px solid #ffa500; /* orange border */
margin: 0.1em;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
<ul>
<li style="padding: 0.2em 1em;"><a href="">Snal</a></li>
<li><a href="">Nocriant</a></li>
<li><a href="">Pubiner</a></li>
<li><a href="">Iquadop</a></li>
<li><a href="">Snaquad</a></li>
<li><a href="">Ustispon</a></li>
</ul>
</body>
</html>

juliejayne

6:35 pm on May 2, 2005 (gmt 0)

10+ Year Member



LG... that looks ike it might just work.

Thanks.

Longhaired Genius

6:41 pm on May 2, 2005 (gmt 0)

10+ Year Member



Ah, good. I've taken another look at the markup and I think "line-height: 1.8em;" would be better than "padding: 0.4em;" in the ul styles (for when the buttons go into two lines on small resolution screens).

juliejayne

6:46 pm on May 2, 2005 (gmt 0)

10+ Year Member



Well that is a big step in the right direction, thanks.

However, if the window/resolution is too small and the row of buttons is pushed to a second line, then the second row of buttons, ends up being 'left-aligned' rather than centred.

I will keep working on it, and would appreciate any further suggestions.

Longhaired Genius

7:59 pm on May 2, 2005 (gmt 0)

10+ Year Member



That doesn't happen for me with the markup I posted. Maybe you've got a stray style somewhere that's interfering.

createErrorMsg

8:23 pm on May 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



they all seem to rely on float: left

You can center a horizontal navbar that uses float by wrapping it in a widthed container, and then centering that. Just don't apply any borders or backgrounds to the centering container, since they won't display unless the container is floated, which will prevent it from centering. In other words, the container's only purpose is to center the navbar. For that it needs only a width, auto margins, and text-align center (for old versions of IE).

Here's a test page...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Centering a Float-Based Horizontal Navbar</title>
<style type="text/css">
#navbar{
width:300px; /*equal to total of floated links*/
margin:0 auto; /*centers in modern browsers*/
text-align:center; /*centers in old versions of IE*/
}
#navbar ul{
list-style-type:none;
}
#navbar ul li a:link, #navbar ul li a:visited{
display:block;
float:left;
width:100px;
line-height:20px;
border-top:1px solid #000;
border-bottom:1px solid #000;
border-right:1px solid #000;
color:#fff;
background:#369;
text-decoration:none;
}
#navbar ul li a:link:hover, #navbar ul li a:visited:hover{
background:#963;
}
#first{
border-left:1px solid #000;
}
</style>
</head>
<body>
<div id="navbar">
<ul>
<li><a id="first" href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
</body>
</html>

cEM

juliejayne

8:42 pm on May 2, 2005 (gmt 0)

10+ Year Member



cEM

Again, that seems to be close to an answer. It looks good in FF but it doesn't seem to work in IE, the 3 buttons are not horizontal.

I will see if I can figure out why.

encyclo

8:48 pm on May 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To correct cEM's otherwise excellent example:

1) you can do away with the redundant enclosing

div
as you can apply the width to the
ul
instead.

2) you need to take into account the border widths in your width calculation for the

ul

3) you need to specify

display:inline;
for the
li

which goves this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Centering a Float-Based Horizontal Navbar</title>
<style type="text/css">
#navbar {
list-style-type:none;
width:310px; /*equal to total of floated links*/
margin:0 auto; /*centers in modern browsers*/
text-align:center; /*centers in old versions of IE*/
}

#navbar li {
display:inline;
}

#navbar li a:link, #navbar li a:visited {
display:block;
float:left;
width:100px;
line-height:20px;
border-top:1px solid #000;
border-bottom:1px solid #000;
border-right:1px solid #000;
color:#fff;
background:#369;
text-decoration:none;
}

#navbar li a:link:hover, #navbar li a:visited:hover {
background:#963;
}

#first{
border-left:1px solid #000;
}
</style>
</head>
<body>
<ul id="navbar">
<li><a id="first" href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</body>
</html>

Note: I put 310px as the width which is a bit too much: you will need to get the exact figure, and then use the box-model hack or equivalent to serve altered values to IE5.x if you are supporting those browsers.

createErrorMsg

9:00 pm on May 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



doesn't seem to work in IE, the 3 buttons are not horizontal.

How embarassing. Sorry.

IE uses margins in lists where other browsers use padding. I always zero both out with the universal selector, but negelected to in that test page.

Then to compound the embarassment, I sized the container too small to allow for the width of the <a>nchor and it's borders.

Add margin:0;padding:0; to the #navbar ul selector (margin for IE, padding for FF). Then change the width setting in #navbar to 304px (to allow for the borders). You also need to add...

#navbar li{
display:inline;
}

...to the styles to fix the step-effect that IE creates. After that, it should work as intended.

My apologies. That's what I get for hastily posting without testing the test page, myself. (Now working in IE6/Win and FF. That's all I have available to test in at the moment.)

Thoroughly chagrined,
cEM

Too slow. Thanks, encyclo. A much cleaner version. :)

juliejayne

9:00 pm on May 3, 2005 (gmt 0)

10+ Year Member



This still does not work for me. It looks reasonable for 3 or 4 links, but as you will see from the full test below, it fails once the menu runs to a second line.

The second line is invariably 'left-aligned'.

I can get this to work in IE without using any LI or UL, but not in FF.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Centering a Float-Based Horizontal Navbar</title>
<style type="text/css">
#navbar {
list-style-type:none;
width:650px;
margin:0 auto;
text-align:center;
}
#navbar ul {margin:0;padding:0; list-style-type:none; }

#navbar li {
display:inline;
}

#navbar li a:link, #navbar li a:visited {
display:block;
float:left;
width:100px;
line-height:20px;
border-top:1px solid #000;
border-bottom:1px solid #000;
border-right:1px solid #000;
color:#fff;
background:#369;
text-decoration:none;
}

#navbar li a:link:hover, #navbar li a:visited:hover {
background:#963;
}

#first{
border-left:1px solid #000;
}
</style>
</head>
<body>
<ul id="navbar">
<li><A HREF="http://www.***********.nl/start.html" class="navbutton">Start</a></li>
<li><A HREF="http://www.***********.nl/koffie.html" class="navbutton">Koffie</a></li>
<li><A HREF="http://www.***********.nl/thee.html" class="navbutton">Thee</a></li>
<li><a href="http://www.***********.nl/chocolade.html" class="navbutton">Chocolade</a></li>
<li><a href="http://www.***********.nl/kens.html" class="navbutton">Kop & schotels</a></li>
<li><a href="http://www.***********.nl/koffiedingen.html" class="navbutton">Koffiedingen</a></li>
<li><a href="http://www.***********.nl/theedingen.html" class="navbutton">Theedingen</a></li>
<li><a href="http://www.***********.nl/diversen.html" class="navbutton">Diversen</a></li>
<li><a href="http://www.***********.nl/links.html" class="navbutton">Links</a></li>
<li><a href="http://www.***********.nl/home.html" class="navbutton">English</a></li>
</ul>
</body>
</html>