Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

html - Use position relative inside display:table-cell in Firefox. Wrapper solution prevent vertical align : middle

I'm using this code on Chrome and ie :

<div id="infobox_mainpic" class="DetailsMainPic" align="center" valign="middle">
   <img src="pictures/3a8ca06d8d51e76243f3d4efdc710d72.jpg" onerror="this.src='css/noload.png';">
   <div class="mainPicArrowLeftDetails" onclick="return NextPic(this,-1);"></div>
   <div class="mainPicArrowRightDetails" onclick="return NextPic(this,1);"></div>
</div>

the main div (infobox_mainpic) has fixed dimension 600px*450px. My purpose is to center vertically and horizontally the img inside. Note that all image can't be wider than 600px and higher than 450px (i have a treatement).

The two div "mainpic..." are supposed to be position absolutely from the top of the main div like on this picture

enter image description here

To achieve this, I used on the infobox_mainpic

display : table-cell;
vertical-align : middle;
text-align:center;
position : relative;

And on the arrows, i use

position : absolute;
top : 130px;

This works on Chrome and IE. The problem is that position relative doesn't work on Firefox. I tried to wrap everything inside a div and apply the position : relative but now, the arrows are not starting from the top of the main div but from the top of the image. If I set width and height to 100% for the wrapper div , then the image is not vertically aligned...

Do you see a solution ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After some tests, I have found a solution.

instead of putting the div wrapper inside infobox_mainpic, I put infobox_mainpic inside a a div with a relative position. This way, the arrows are correctly positioned and the img is still inside the table-cell div so it is correctly centered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...