轉載未知大小的圖片在一個已知大小容器中的水平和垂直置中(二)_經驗交流

來源:互聯網
上載者:User

這是一個未知大小的圖片在一個已知大小容器中的水平和垂直置中的例子,原文標題:“Centering an image of unknown size in an outer container of known size”, 作者:Stu Nicholls form:http://www.cssplay.co.uk/menu/centered.html ,發表日期:15th October 2006。

此文由forestgan於2006-10-17翻譯發表,更多文章。

這個水平和垂直置中的例子能正常工作在IE5.x, IE6, IE7, Firefox, Opera, Mozilla, Netscape,為IE和非IE分別寫了二套簡單的樣式。

IE使用inline-blocks,非IE使用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;}}
<!--[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]-->

xhtml

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

作者的例子是單個的,我擴充了下,一頁6張圖片,可適用於像冊和產品展示等地方,示範地址。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.