Jump to content

Probably really easy : floated menu auto-width


jackpf

Recommended Posts

Hi all,

I'm thinking about putting a new menu on my site, but along the top, rather than down the side.

 

However, the only way I can get the menu items to have an auto width is either by using a table, or display: table (which isn't widely supported).

 

For example, at the moment, all of my divs just pile over to the left. I've tried stuff such as width: auto, text-align: center, and margin: 0 auto, but they still won't space out.

 

This is my current code.

 

<style type="text/css">
.nav
{
	width: 100%; /*just to make sure it's 100%*/
	text-align: center; /*futile attempt at auto width*/
	margin: 0 auto; /*futile attempt at auto width*/
	overflow: auto; /*futile attempt at auto width*/
	border: 1px solid green;
}
.nav div
{
	width: auto; /*futile attempt at auto width*/
	text-align: center; /*futile attempt at auto width*/
	margin: 0 auto; /*futile attempt at auto width*/
	float: left; /*to display vertically*/
	border: 1px solid red; /*just so I can see how wide they are, for testing purposed*/
}
</style>

<div class="nav">
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
<div>item 4</div>
<div>item 5</div>
<div>item 6</div>
</div>

 

Any help would be awesome.

 

Thanks, Jack.

Link to comment
Share on other sites

Yeah, I know how to set it to be a certain width, but I can't set it to be an auto width.

 

For example, divs usually take up 100% of the space they can. But when I float them I can't seem to set them to be a percentage width.

 

Like, if you take my code and try and set the divs to like, 20% or something, it'll have no effect. I just don't understand why...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.