views:

51

answers:

4

In the following example, I want the bottom of the site name text "site name" and the bottom of the menu text "menu 1 menu 2 menu 3" to align flush with the bottom of the container they're in (header). As it is now, the sitename text is some number of pixels above it's container's bottom edge, while the menu text is a different number of pixels above that same edge. I want both elements to be sitting on the same line.

It seems that using line-height can push it down through trial and error with different values, but the result isn't consistent across browsers (e.g. i can get them flush in Safari and Chrome, but then Firefox looks different). There has to be a better way?

Also, is there a better way to force the menu into that bottom-right corner other than the way I've done it?

thanks!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html>
        <head>
            <title></title>
            <style type="text/css">


        html, body, div, span, applet, object, iframe,
        h1, h2, h3, h4, h5, h6, p, blockquote, pre,
        a, abbr, acronym, address, big, cite, code,
        del, dfn, em, font, img, ins, kbd, q, s, samp,
        small, strike, strong, sub, sup, tt, var,
        b, u, i, center,
        dl, dt, dd, ol, ul, li,
        fieldset, form, label, legend,
        table, caption, tbody, tfoot, thead, tr, th, td {
            margin: 0;
            padding: 0;
            border: 0;
        }

        body {
            text-align: center;
        }

        #container {
            margin:0 auto;
            margin-top:20px;
            width:800px;
            border:1px solid red;
            text-align: left;
        }

        #header {
            float:left;
            position:relative;
            width:100%;
            border:1px solid green;
        }

        #sitename {
            float:left;
            left:0px;
            bottom:0px;
            margin:0;
            padding:0;
            border:1px solid blue;
        }

        #sitename h1 {
            font-weight: normal;
        }       

        #menu {
            position:absolute;
            right:0;
            bottom:0;
            line-height:1em;
            float:right;
            list-style: none;
            border:1px solid blue;
        }

        #menu ul li {
            list-style: none;
            float:left;
            padding: 0 0 0 15px;
        }

        #sitename h1 {
            font-size: 3em;
        }

        </style>
    </head>

    <body>

    <div id="container">

        <div id="header">
            <div id="sitename">
                <h1>site name</h1>
            </div>
            <div id="menu">
                <ul id="nav">
                    <li>menu1</li>
                    <li>menu2</li>
                    <li>menu3</li>
                </ul>
            </div>
        </div>

        <div id="content">
        <p>here is some content.</p>
        </div>

        <div id="footer">
        <p>here is the footer.</p>
        </div>

    </div> <!-- container -->
    </body>
</html>
+1  A: 

You could set a negative margin on #sitename h1. E.g.

#sitename h1 {
  font-size: 3em;
  margin-bottom: -9px;
}

The tricky part is to make it align exactly in all browser, since it differs a pixel or so between them.

Gert G
yeah, unfortunately it has the same problem of varying across browsers.
mix
You might get around it by having browser specific stylesheets for those styles. It will give you some overhead, but you'll get the layout that you strive for.
Gert G
A: 

I would suggest positioning the sitename relative to where it currently is, like so:

#sitename h1 {
   position: relative;
   top: 9px;
}

However, this isn't perfect because if different fonts are used, the spacing may be different. Thats not something that can be fixed with any CSS property (currently). You might consider an image? To maintain the same height at all times?

Ben
+1  A: 

You don't need to wrap your h1 in a div. It's already a block-level element. Try this code:

<!-- CSS -->
#header {
  float:left;
  position:relative;
  width:100%;
  border:1px solid green;
  height: 100px;
}

#sitename {
  position: absolute;
  float:left;
  left:0px;
  bottom:0px;
  margin:0;
  padding:0;
  border:1px solid blue;
  font-weight: normal;
  font-size:3em;
}     

#menu {
  position:absolute;
  right:0;
  bottom:0;
  line-height:1em;
  float:right;
  list-style: none;
  border:1px solid blue;
}

#menu ul li {
  list-style: none;
  float:left;
  padding: 0 0 0 15px;
}

<!-- HTML -->
<div id="header">

  <h1 id="sitename">site name</h1>

  <div id="menu">
    <ul id="nav">
      <li>menu1</li>
      <li>menu2</li>
      <li>menu3</li>
    </ul>
  </div>

</div>
Sam Nabi
interesting. thanks for pointing that out about the h1 being its own block level element. not sure why i was wrapping it. what's interesting is that it seems to hold up a bit better to line-height manipulations. firefox is still the browser that doesn't match the others (well, I haven't tried IE yet), but it's variation from the others is less than before. thus, i can find a middle ground where things aren't quite aligned that looks pretty good in all three browsers.still hoping there might be a way to do this, though. i'll keep looking and will report back if i find anything.
mix
If you set your heading's font size in pixels rather than em, you can give it a negative bottom margin (in pixels) that should look uniform cross-browser.
Sam Nabi
A: 

Do you have any kind of reset styles being applied? Each browser has different defaults for padding & margin on elements like headers and lists. If you reset everything at the beginning of your style sheet it'll make it much easier to line everything up!

Eric Meyer's reset css is a good place to start: http://meyerweb.com/eric/thoughts/2007/05/01/reset-reloaded/

DHuntrods