如何始終居中對齊水平和垂直容器,無論內容如何?以下是我想要做的一個基本例子。無論內容如何,始終將容器居中對齊水平和垂直
此方法是否正確,或者有更簡單的方法解決我的問題?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>3 columns</title>
<style>
div.container {
height: 200px;
width: 100%;
margin-left: auto;
margin-right: auto;
margin-top: 25%;
margin-bottom: 20%;
}
div.col {
float: left;
width: 25%;
}
</style>
</head>
<body>
<div class="container">
<div class="col" align="center">This is col 1</div>
<div class="col" align="center">This is col 2</div>
<div class="col" align="center">This is col3</div>
</div>
</body>
</html>
請記住在適當的前綴WebKit瀏覽器(HTTP:/ /caniuse.com/transforms2d)。 –
當您離開您的評論時正在處理該過程。 ;) – Benjam
如果IE8和更低版本需要支持,我會選擇[table CSS或僞影](http://css-tricks.com/centering-in-the-unknown/)方法。此外,如果元素更高,這些元素不存在將元素推出視口頂部的問題。 –