Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you evenly distribute the width of the li

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
My question has to do with how do you spread the items in the list (li) across the page evenly so that they have equal width.
I have been eyeballing it and increasing the width on #menu li until it looks the way I want it to. To make it look even
I created a width of 60px and padded the left 60 to make it look centered. What is the correct way to do this.

I appreciate your help
Howard

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">


<title>empty html</title>
<style>
body {font-size: .75em;font-family: arial;}
#container{width: 800px; height: 300px; margin:0 auto; border:1px solid black;}
#header{height: 60px; background-color: black;color: white;}
ul,li {margin:0;padding:0;}
#menu {width: 800px; height: 20px; background-color: orange;}
#menu ul {}
#menu li {list-style: none; float: left; width: 60px; padding-left:60px;}
#menu li a {color: white; text-decoration: none; }
#menu li a:hover {color: black;}
</style><base href="file:///C:/Users/csphard/Desktop/practice%20css/emptyhtml.html"></head><body><div id="container">
<div id="header">header</div>
<div id="menu">
<ul>
<li><a href="#">test1</a></li>
<li><a href="#">test2</a></li>
<li><a href="#">test3</a></li>
<li><a href="#">test4</a></li>
<li><a href="#">test5</a></li>
<li><a href="#">test6</a></li>
</ul>
</div>
</div>
</body></html>
 
If it is text you're centring inside, then use text-align: center; on the list items (li). This will ensure that the text inside the list item is in the centre (you will have to make sure the padding on both sides is 0 to not interfere with centring). If it is other block level elements, then use left and right margin values of auto on them and give the inner block elements a width. This will ensure that they are positioned in the centre of the li element.

If you're trying to centre the entire menu, make sure you give it a good width (which is equivalent to all li widths with all the margins) and give its left and right margins a value of auto.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Since you've got a fixed width container, you just need to do some maths:

The container is 800px wide. 800 divided by 6 is 133.3333. So each li needs to be 133px wide, with 2px left over. How you deal with that remainder is up to you - either you have a pixel either side, or you change the container width down to 798px. Remember that the total width of an element is its [tt]left-border-width + left-padding + width + right-padding + right-border-width[/tt], so if you want borders and/or padding around your element you'll need to decrease its [tt]width[/tt] accordingly.

Something like this should work:
Code:
#menu ul {list-style: none; margin: 0; padding: 0 1px;}
#menu li { float: left; text-align: center; width: 133px;}
It might take some trial and error to work around all IE6's rendering bugs when you're dealing with pixel-perfect stuff like this.

If you're trying to split a variable width container into six columns, it's more tricky. You can give each li [tt]width: 16.666%[/tt], but you'll find it sometimes wraps round to a second line due to rounding errors. If you have to do this, try to leave a few spare pixels somewhere to allow for this. Or (horrors!) use a table.



-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top