Reposting the horizontal and vertical centering of images of unknown size in a container of known size (2)_Experience exchange

WBOY
Release: 2016-05-16 12:09:24
Original
1654 people have browsed it

This is an example of horizontal and vertical centering of an image of unknown size in a container of known size. Original title: "Centering an image of unknown size in an outer container of known size", author: Stu Nicholls form: http://www.cssplay.co.uk/menu/centered.html , published on 15th October 2006.

This article was translated and published by forestgan on 2006-10-17. More articles.

This horizontal and vertical centering example can work normally in IE5.x, IE6, IE7, Firefox, Opera, Mozilla, Netscape, and two sets of simple styles are written for IE and non-IE.

IE uses inline-blocks, non-IE uses table-cell and vertical-align.

CSS

/* for non-IE browsers */
div.holder {width:750px; height:300px; background:#f8f8f8; 
border:1px solid #777; text-align:center; display:table-cell; vertical-align:middle;}
}<br><!--[if IE]>
<style type="text/css">
/* vertical align for IE */
#edge {width:0; height:100%; display:inline-block; vertical-align:middle;} 
#container {text-align:center; width:100%; display:inline-block; vertical-align:middle;}
</style>
<![endif]-->
Copy after login

xhtml

<div class="holder"><br><span id="edge"></span> <br><span id="container"><img src="graphics/homework.jpg" alt="" /></span><br></div><br>
Copy after login

The author's example is a single one. I extended it to include 6 pictures on one page, which can be used in photo albums and product displays. Demo address.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template