Göran Andersson said:
Impossible to say, as you only supply the code for the element, and
not the containing div.
I appreciate the help I get so I try to make my post short to minimize the
time required to read it. Sometime that doesn't work so well.
To make the code shorter so I could supply it here
I copied the code and pasted it into a page by itself.
<body>
<form id="form1" runat="server">
<div style="position: relative; border: .1em solid red; height: 300px;
line-height: 300px;
text-align: center;">
<p style="border: 5px solid green; position: relative; top: 45px; height:
200px;
background-color: pink; line-height: 200px; vertical-align: middle;
margin-left:auto; margin-right:auto;text-align:center;">
<img style="height: 100px; position: relative; top: 50px;margin-left:auto;
margin-right:auto; text-align:center;
border: 0px;"
src="
http://groups.google.se/intl/en/images/logos/groups_logo.gif"
alt='TEST' />
</p>
</div>
</form>
</body>
In FF the pink <p> box is positioned a little too low to be vertically
centered in its containing div by about 10px.
But the imag is vertically and horizontally centered in the pink box.
In IE8 the pink box is centered in the containing div
but the image is too low (by 50px)
In both browsers the div fills the browser's width
and the pink <p> fills the div's width as desired
The position is defined relative to the original position of the
element, not relative to the parent element. So, if there is a
difference between browsers, it's most likely because there is a
difference in the original position, not in the relative positioning.
If you want to position the element relative to the parent, you should
use absolute positioning instead.
If I use absolute I can't center horizontally
margin-left:auto; margin-right:auto;text-align:center; doesn't work
The width varies so I can't use Left to center.
--------
Say there an img inside a p that is inside a div
if I put margin-left:auto; margin-right:auto;text-align:center;
on the p, does it center that p within the enclosing div
or does it center the img in the p?
I've been testing and concluded:
Vertical-align:middle applies to what is inside the element.
Display:inline applies to the element it is on.
Line height applies to what is inside the element.
Make the container's Line-height equal to it's height to center one img
inside the container.
Text-align: center applies to what is inside the element.
Do you concur with the above?
Thanks a lot