1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to Center a UL menu with unknown width?

Discussion in 'CSS' started by wd_2k6, Aug 31, 2009.

  1. #1

    OK when we know the width it's as simple as adding margin: 0 auto;

    But i've come accross a problem where i'm just adding padding, therefore the overall size of the menu is unknown.

    So how do we go about centering it. For example I was doing the following:

    Code (Text):
    2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    3. <html xmlns="http://www.w3.org/1999/xhtml">
    4. <head>
    5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    6. <title>Untitled Document</title>
    7. <style type="text/css" media="screen">
    8. #menuWrap{
    9.     background:pink;
    10.     text-align:center; /*Centers the Menu*/
    11. }
    12. ul {
    13.     margin:0; padding:0; /*Reset Defaults*/
    14.     display:inline-block; /*Centering Method*/
    15. }
    16. li {
    17.     margin:0; padding:0; /*Reset Defaults*/
    18.     display:inline; /*Also Removes Bullets*/
    19. }
    20. li a {
    21.     float:left; /*Triggers Display Block*/
    22.     padding: 14px;
    23.     border:2px solid #000;
    24.     background:#ccc;
    25. }
    26. </style>
    27. </head>
    29. <body>
    30. <div id="menuWrap">
    31.     <ul>
    32.         <li><a href="#">Link 1</a></li>
    33.         <li><a href="#">Link 2</a></li>
    34.         <li><a href="#">Link 3</a></li>
    35.     </ul>
    36. </div>
    37. </body>
    38. </html>
    This works fine in IE8 and the latest FF, but it doesn't center it in IE 7 or less? Any ideas?
    wd_2k6, Aug 31, 2009 IP
  2. Stomme poes

    Stomme poes Peon

    Likes Received:
    Best Answers:
    Trophy Points:
    Also won't work in old FF (2 or K-meleon) but screw those nasty old browsers.

    IE likely needs you to add a special line for it:

    ul {
    display: inline;

    because it doesn't support inline-block on native blocks without that statement. inline-block on native inlines is no problem. Since a ul is a block, you'll need that, and I usually do it like this

    Code (Text):
    2. #menuWrap{
    3.     background:pink;
    4.     text-align:center; /*Centers the Menu*/
    5. }
    6. ul {
    7.     margin:0; padding:0; /*Reset Defaults*/
    8.     display:inline-block; /*Centering Method*/
    9. }
    10. * html ul {display: inline;}
    11. *+html ul {display: inline;}
    should work. You can also do it in an IE-only stylesheet if you don't like pollution in your main stylesheet.
    Stomme poes, Sep 1, 2009 IP
  3. wd_2k6

    wd_2k6 Peon

    Likes Received:
    Best Answers:
    Trophy Points:

    Thanks for the reply.

    It does indeed work, i'm guessing *html targets pre IE7, and *+html targets IE7?

    Would you use this method for centering a the menu? The only current problem is that it doesn't work in FF2 and some other old browsers as you mentioned.
    wd_2k6, Sep 3, 2009 IP
  4. Stomme poes

    Stomme poes Peon

    Likes Received:
    Best Answers:
    Trophy Points:
    I have used it. http://stommepoes.nl/Brandweer/brandweer3.html top menu. CSS is same address but brandweer3.css (cause there's a 1-5 in there I think lawlz)

    * html with the space between the * and html is the familiar IE6 hack. I know you've seen and used it before!

    the IE7 one is no spaces: *+html though on the googles there's another one floating around with like *first-child: html or some weird thing. Too much to type : )

    You can also just use an IE Conditional Comment with IE styles or whatever. I like to keep my hacks in my single CSS file myself.

    You can get FF2/K-Meleon to work with
    display: -moz-inline-box;
    not block, but "box". There's also a -moz-inline-block but they are different from each other and neither of them truly work like the real inline-block : (

    Also because you are using inline-block know that like other inlines, whitespace in your code will make real whitespace on the page, just like with text. So you can either make like a negative margin like Gary does or you can wrap your closing tags in the HTML:

    <li>blah blhja blha blha <li
    ><li>blah blahb blahjb <li
    ><li>blah blahb blah<li
    Stomme poes, Sep 4, 2009 IP
  5. deathshadow

    deathshadow Prominent Member

    Likes Received:
    Best Answers:
    Trophy Points:
    Gah, overthinking this much? Lose the unneccessary wrapping div, the UL is already block level so you don't need that. Display:inline on li does NOT hide bullets on all browsers, so turn them off PROPERLY, though we want the LI inline... and instead of making the UL inline-block make the ANCHORS inline-block.

    Oh, and as to the inline-block issue - for all browsers EXCEPT webkit, word-spacing can be used to get rid of it, then you can guesstimate with letter-spacing for webkit. (make sure you plan for if they overlap in source order on that)

    Code (Text):
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    3. <html
    4.     xmlns="http://www.w3.org/1999/xhtml"
    5.     lang="en"
    6.     xml:lang="en"
    7. ><head>
    9. <meta
    10.     http-equiv="Content-Type"
    11.     content="text/html; charset=utf-8"
    12. />
    14. <title>
    15.     Centered Menu
    16. </title>
    18. <style type="text/css" media="screen,tv,projection">
    19. /* null margins and padding to give good cross-browser baseline */
    20. html,body,address,blockquote,div,
    21. form,fieldset,caption,
    22. h1,h2,h3,h4,h5,h6,
    23. hr,ul,li,ol,ul,
    24. table,tr,td,th,p,img {
    25.     margin:0;
    26.     padding:0;
    27. }
    29. img,fieldset {
    30.     border:none;
    31. }
    33. #menu {
    34.     list-style:none;
    35.     word-spacing:-2em;
    36.     letter-spacing:-0.4em;
    37.     text-align:center;
    38.     zoom:1; /* actually need zoom as we're tripping MORE than haslayout! */
    39.     background:pink;
    40. }
    41. #menu li {
    42.     display:inline;
    43.     zoom:1;
    44.     word-spacing:0;
    45.     letter-spacing:0;
    46. }
    48. #menu a {
    49.     display:-moz-inline-block;
    50.     display:-moz-inline-box;
    51.     display:inline-block;
    52.     padding:14px;
    53.     border:solid #000;
    54.     border-width:2px 0 2px 2px;
    55.     background:#CCC;
    56. }
    58. #menu .last a {
    59.     border-width:2px;
    60. }
    61. </style>
    63. </head><body>
    65. <ul id="menu">
    66.     <li><a href="#">Link 1</a></li>
    67.     <li><a href="#">Link 2</a></li>
    68.     <li class="last"><a href="#">Link 3</a></li>
    69. </ul>
    71. </body></html>
    Works 100% perfect in IE 6, 7 & 8, FF 2, 3 and 3.5, Opera 9.64 and 10, and is 'close enough' in webkit so as not to matter - in webkit (safari/chrome) they may overlap by a pixel or two depending on the font, so just remember the depth sorting order (hence the left border on all items hiding any overlap, with the right border only on the last item).

    Where I say you have to use zoom:1; - you HAVE to use zoom:1; - it seems to trip some sort of 'other' fix in the rendering apart from the haslayout that heights or widths do. The 'spaces' between elements re-appear if you try to use the holly hack where I used zoom - oh well.
    Last edited: Sep 4, 2009
    deathshadow, Sep 4, 2009 IP
  6. Stomme poes

    Stomme poes Peon

    Likes Received:
    Best Answers:
    Trophy Points:
    Code (Text):
    2. #menu a {
    3.     display:-moz-inline-block;
    4.     display:-moz-inline-box;
    When I was in a position to see the differences between these two, the second one simply overrides the first one. So the -moz-inline-block is never seen by a moz browser (unless there's some strange version of someone like seamonkey who doesn't override??).

    I gave up on the word-spacing thing because of Saffy-Chrome. We shouldn't have to mutilate HTML to remove spacing but it let's us avoid zoom and stupid safari problems.
    Stomme poes, Sep 4, 2009 IP
  7. deathshadow

    deathshadow Prominent Member

    Likes Received:
    Best Answers:
    Trophy Points:
    Strangely enough, 2.0 and many linux 'offshoots' only see the first one and don't know the second. It was 'renamed' inline-box for 2.0.14

    Though setting the first one also throws some sort of flag that prevents absolute positioning errors in it's children, even though it's overrided by -moz-inline-box. It doesn't come up as a problem THAT often, but I use the full version because it's less than 20 characters just to be 'sure' it works.

    I've also found omitting the first can cause regular inline-block not to work right in FF 3.5 - so that's probably a regression - NOT that it's inline-block ACTUALLY works right in the first place... Though at least it's behavior is better than Safari's ignoring word-spacing and paying attention to letter-spacing...
    deathshadow, Sep 4, 2009 IP
  8. Stomme poes

    Stomme poes Peon

    Likes Received:
    Best Answers:
    Trophy Points:
    Yeah that's where I saw the differences:
    I needed someone to act like run-in (but as that doesn't exist anywhere I used inline-block instead : (
    but also it needed to do image replacement. -moz-inline-box worked correctly as far as, let me set a height and width and otherwise act like an inline, but it could not be the rel-po'd positioned ancestor of an abso-po'd child (instead the child went to the viewport/body).
    -moz-inline-block didn't know how to make the element have height and width like a block, but DID know how to be a positioned ancestor. Basically making the whole thing worthless to do in K-Meleon and FF2. Oh well, sucks for them. I ended up staying with -box so at least we had styled run-in text without logos sitting over them... if "alt" could be styled richly the whole image-replacement bs could have been scrapped.

    But anyway that's where I noticed I could have one or the other. Both FF2 and K-Meleon ignored whoever came first in my tests, and so since then I've just used -box in things like menus (seems to work ok on my page I linked to above but no I didn't check in freaky browsers like old seamonkeys and whatever... just K-Meleon and an old copy of FF2).
    Stomme poes, Sep 7, 2009 IP