/    Sign up×
Community /Pin to ProfileBookmark

Centering a Floating List Menu

I have an unordered list…

[CODE]
<div style=”width: 500px;”>
<ul>
<li style=”float: left;”>&nbsp;</li>
<li style=”float: left;”>&nbsp;</li>
<li style=”float: left;”>&nbsp;</li>
<ul>
</div>
[/CODE]

…floating each list element left is the only way I know of that I can have a list without line breaks (for a menu). However, I am also trying to center the list within the div.

I tried putting a “margin: 0 auto;” on the div to center the list within it, as well as putting a 300px wide div within it and applying “margin: 0 auto;” to it as well – neither worked.

I want to be able to keep it dynamic enough to add more list items in the future but still keeping it centered. Does anyone have any suggestions on how I can do this?

NOTE: I’ve tried too many things to name and I couldn’t do it, so please test your suggestions before submitting them to ensure that they work as is assumed.

Thanks,
walman

to post a comment
CSS

3 Comments(s)

Copy linkTweet thisAlerts:
@WebJoelAug 31.2007 — <div style="width: 500px; [B]margin:0 auto;[/B]">

<ul>

<li style="float: left;">&nbsp;</li>

<li style="float: left;">&nbsp;</li>

<li style="float: left;">&nbsp;</li>

<ul>

</div>[/QUOTE]
[I]and[/I] a valid !doctype. Without a !doctype, IE will not 'center' this.

Try:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

....[/QUOTE]


Required: a stated width (even a percentage width), margin:0 auto; and valid !doctype.
Copy linkTweet thisAlerts:
@CentauriSep 01.2007 — If this is a menu, then it should also contain <a> elements within the <li>s. A lot depends on what you want to do with the <a> elements - if the menu items are just going to be text links, then [COLOR="Blue"]dispaly:inline[/COLOR] on the <li>s and [COLOR="Blue"]text-align:center[/COLOR] on the containing div may be all you need.
Copy linkTweet thisAlerts:
@KravvitzSep 04.2007 — [url=http://www.dynamicsitesolutions.com/css/example-menus/4/]Check out this centered menu example.[/url]
×

Success!

Help @walman05 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...