Results 1 to 6 of 6

Thread: Vertically Centering an Inline List in a Div

  1. #1
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Vertically Centering an Inline List in a Div

    I'm having trouble understanding how to vertically center an inline list in a div. Can anyone recommend some reading?

    The list is used for a horizontal nav bar with a background. I've set the height of the div to 20px because that's the height of the bg image.

    Also: Should the font specs be where they are (#nav ul) or maybe in the #nav section?

    Here's the code:

    Code:
    #nav {
    background: url(../images/nav_bg2.gif);
    text-align: center;
    overflow: hidden;
    height: 20px;
    }
    #nav ul{
    margin:0;
    padding:0;
    list-style:none;
    }
    #nav li{
    font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
    font-size: 0.75em;
    display:inline;
    margin:0;
    padding:0;
    }

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    are you talking about vertical-align: ?

  3. #3
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by boogyman View Post
    are you talking about vertical-align: ?
    No, that doesn't work with lists (at least it didn't for me). I think it's for images.

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    what exactly are you trying to align? because that will align text vertically in the list

    if you are attempting to align the whole list to the "middle" of the div then you could use the same method that you horiztonally align
    Code:
    margin: auto 0;
    and as you see their just switched. if you are attempting to center it both vertically and horizontally however, I would stress against this as it is breaking convention and if you are only doing it for a cosmetic look, it is likely to confuse your users. That is why block level elements are defaulted to vertically align to the top of their parent element.

    if you just want to create a little bit of whitespace at the top / bottom of the list, you could always just create some padding around the list and it will provide a little bit of a buffer and not look as conjested
    Code:
    #nav ul { padding: 1em; }  /* or preferred size */

  5. #5
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    The list is in-line, used in a nav bar. The nav bar has a specific hight because it has a bg image. The in-line list should be equal distance from top and bottom, but at least not sitting on the bottom.

  6. #6
    Join Date
    Sep 2007
    Location
    Shanghai of China
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It is ver easy. Because it have one row so you can be so do "height:20px;line-height:20px;"

    It's ok????

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •