CSS解決未知高度垂直置中

來源:互聯網
上載者: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通過。 xmlns="http://www.w3.org/1999/xhtml">


預設長度 加長頁面

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,設定如。

Design by Forestgan 本示範採用創作共用授權--署名和非商業用途。

相關文章

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.