views:

689

answers:

2

Hi All,

I ran into a strange problem. I use DIV as a container, and put an image into this DIV. I want this image to be aligned vertically to bottom. The following code works.

#banner { 
  width: 700px; 
  height: 90px; 
  top: 60px; 
  left: 178px; 
  overflow: hidden; 
  text-align: center; 
  display: table-cell; 
  vertical-align: bottom; 
  position: relative;
}

<div id="banner">
  <img src="http://www.google.de/intl/de_de/images/logo.gif"/&gt;
</div>

But if I change the css code "position: relative" to "position: absolute", the image cannot be aligned to bottom any more. Is this a bug of Firefox3? How can I solve this problem?

My current solution is:

<div id="banner">
  <table width="100%" height="100%"><tr><td valign="bottom" align="center">
  <img src="http://www.google.de/intl/de_de/images/logo.gif"/&gt;
  </td></tr></table>
</div>

But I do not like this solution.

+2  A: 

Short answer: Change

top: 60px;

to

bottom: 60px;

Long answer:

The declaration position: absolute takes your element out from wherever it is and place it relative to innermost element that is not declared static. In no longer participate in the alignment of any other element, hence it no longer serve as table-cell (the declaration has no effect). Additionally, declaration such as top: 10px means to place it that much distance from the top of that containing element.

Declaring an element as position: relative makes declaration such as top: 10px means 'move the element 10px from the top from the current position'. It is possible for elements declared relative to overlap with other elements, although you should remember that the original position still determines the arrangement of other elements.

I hope this answer your question.

RichN
Because I want to DIVs can be overlapped. If DIVs have relative position, they cannot be overlapped, can they?
stanleyxu2005
I've updated my answer.
RichN
+1  A: 

You could also try setting a position:relative; container, which makes the banner (the #banner position:relative; and the img position:absolute) then set the absolute position to be bottom:0, aligning it to the bottom of the container. If it's the whole page, just set the width and height of the container to 100%, and remove extra padding/margin on the body or on the div.

CodeJoust
Thanks, good to know "bottom:0" can make it aligned to bottom. How to make image align to middle? I have a solution to keep text in the middle: insert an inner DIV and set its "line-height: height_of_outer_div". Another thing, I want to change it at run-time. If I must write totally different tricky css code for the three positions, it is not that nice.
stanleyxu2005
Is it on a working webpage?To make it display in the middle, just set the margin:0 auto;, the element may need to have a width though, depending on its container element.To make the image align to a div... that's the trick part. There is a css table trick, as you've tried, and the line trick. You also can just pad the element if it's always the same width, but that typically isn't the case.
CodeJoust