0

http://jsfiddle.net/JDwjJ/

Please take a look at this code

The problems are followings:

  • #nav must be at the top of #main, not side by side;
  • How can I center #nav inside #container if I don't exact to set exact width for it? Tried margin: 0 auto; no success?
3
  • 1
    margin:0 auto; will only work if the #nav element has a specified width. Commented Feb 16, 2012 at 7:19
  • @TiesonT. width of nav is unknown. website is multilanguage, so width changes. How can I center nav without defining exact width or is there any other way for variable width? Commented Feb 16, 2012 at 7:21
  • Without digging into your specific ruleset, I can't say, but it's a hard and fast rule that a browser cannot actually apply an "auto" margin unless the dimensions of the element are known. All "auto" really does is say "split what's left evenly on both sides." Commented Feb 16, 2012 at 7:27

2 Answers 2

2

Remove unnecessary float form your #nav & ul. Then define width to your #nav for example 600px

#nav{
 width:600px;
}
#nav ul
{
    padding:0px;
    margin:0px;
    list-style:none;
    overflow: hidden;
}

Check this http://jsfiddle.net/JDwjJ/1/

UPDATED If the width is unknown then you can use display:inline-block property for this:

    #nav {
        display:inline-block;
        *display:inline/* For IE7*/
        *zoom:1/* For IE7*/
    }
    #container{
        text-align:center;
    }  
   #main{
        text-align:left;
   }

Check this http://jsfiddle.net/JDwjJ/8/

Sign up to request clarification or add additional context in comments.

1 Comment

I already told that, width of nav is unknown. website is multilanguage, so width changes. How can I center nav without defining exact width or is there any other way for variable width?
2

For the

1.nav must be at the top of #main, not side by side;

just apply

clear:both

on the main div

2.How can I center #nav inside #container if I don't exact to set exact width for it? Tried margin: 0 auto; no success?

you have to set certain width for this content container by this margin 0 auto will work

update : please take a look if you need the same

http://jsfiddle.net/JDwjJ/9/

4 Comments

width of nav is unknown. website is multilanguage, so width changes. How can I center nav without defining exact width or is there any other way for variable width?
@epic_syntax define widht as 100% that wont hurt you in any case, check my updated answer if it's what you need :)
lol.. that will force resive navigation bar to fit parent div width, even if text of navigation menu is not that long
so I am out now no more guesses :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.