CSS解決未知高度垂直置中_CSS/HTML

來源:互聯網
上載者:User
儘管有CSS的vertical-align特性,但是並不能有效解決未知高度的垂直置中問題(在一個DIV標籤裡有未知高度的文本或圖片的情況下)。

標準瀏覽器如Mozilla, Opera, Safari等.,可將父級元素顯示方式設定為TABLE(display: table;) ,內部子項目定為table-cell (display: table-cell),通過vertical-align特性使其垂直置中,但非標準瀏覽器是不支援的。

非標準瀏覽器只能在子項目裡設距頂部50%,裡面再套個元素距頂部-50% 來抵消。
複製代碼 代碼如下:

body {padding: 0; margin: 0;}
body,html{height: 100%;}
#outer {height: 100%; overflow: hidden; position: relative;width: 100%; background:ivory;}
#outer[id] {display: table; position: static;}
#middle {position: absolute; top: 50%;} /* for explorer only*/
#middle[id] {display: table-cell; vertical-align: middle; position: static;}
#inner {position: relative; top: -50%;width: 400px;margin: 0 auto;} /* for explorer only */
div.greenBorder {border: 1px solid green; background-color: ivory;}

xhtml
複製代碼 代碼如下:

<div id="outer">
  <div id="middle">
      <div id="inner" class="greenBorder">
      </div>
  </div>
</div>

以上CSS代碼的優點是沒有hacks,採用了IE不支援的CSS2選取器#value[id]。

CSS2選取器#value[id]相當於選取器#value,但是Internet Explorer不支援這種類型的選取器。同樣地.value[class],相當於.value,這些只有標準瀏覽器能讀懂。

測試:Firefox1.5、Opera9.0、IE6.0、IE5.0通過。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Vertical centering in valid CSS</title> <style type="text/css"> body {padding: 0; margin: 0; font-size: 75%; line-height: 140%; font-family:Arial, Helvetica, sans-serif;} body,html{height: 100%; } a{color: #333;} a:hover{color: green;} #outer {height: 100%; overflow: hidden; position: relative;width: 100%; background:ivory; } #outer[id] {display: table; position: static;} #middle {position: absolute; top: 50%;text-align:center;} /* for explorer only*/ #middle[id] {display: table-cell; vertical-align: middle; position: static;} #inner {position: relative; top: -50%;width: 600px;margin: 0 auto;text-align:left;}/*for explorer only */ div.greenBorder {border: 1px solid green; background-color: #FFF;} p{margin: 1em;} </style> <script type="text/javascript"> // <![CDATA[ function toggleContent(name,n) { var i,t='',el = document.getElementById(name); if (!el.origCont) el.origCont = el.innerHTML; for (i=0;i<n;i++) t += el.origCont; el.innerHTML = t; } // ]]> </script> </head> <body> <div id="outer"> <div id="middle"> <div id="inner" class="greenBorder"> <p>預設長度  加長頁面</p> <p> 1.開啟illustrator,建立一個檔案,畫個矩形,比你要匯入的圖片大一些,白色填充。 2.選中矩形,菜單:Effect > Distort & Transform > Zig Zag,設定如下圖。 3.菜單:Effect > Stylize > Drop Shadow,設定如下圖。 1.開啟illustrator,建立一個檔案,畫個矩形,比你要匯入的圖片大一些,白色填充。 2.選中矩形,菜單:Effect > Distort & Transform > Zig Zag,設定如下圖。 3.菜單:Effect > Stylize > Drop Shadow,設定如下圖。</p> <address > Design by Forestgan 本示範採用創作共用授權--署名和非商業用途。 </address> </div> </div> </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.