當前位置:軟件學堂 > 資訊首頁 > 網絡編程 > DIV+CSS > div水平垂直居中設置技巧

div水平垂直居中設置技巧

2012/9/20 15:05:42作者:佚名來源:網絡

移動端

學習CSS+DIV的朋友,今天學無憂來教你們如何使用div水平垂直居中設置技巧,相信知道DIV的人對水平垂直居中并不陌生吧,但是對于那些剛學習的菜鳥來說,還是有很打幫助的!下面請看學無憂慢慢為大家進行詳細的解說。

一、DIV水平居中設置技巧

在設計代碼中使用margin-left:auto;margin-right:auto; 可以讓你的div居中對齊。 在使用CSS的時候,在style屬性中加入.style{margin-left:auto;margin-right:auto;},同時也可以使用縮寫形式為: .style{margin:0 auto;}。

解釋: 數字0,表示上下邊距是0。然后左右居中。也可以按照需要設置成不同的值。

<!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="zh-cn"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>水平垂直居中div演示效果</title>
<style type="text/css"> 
.align-center{ 
position:fixed;left:50%;top:50%;margin-left:width/2;margin-top:height/2;
} 
</style> 
</head> 
<body> 
<div class="align-center">
水平垂直居中div演示效果
</div> 
</body>
</html> 

或者不使用上面的代碼,給body使用一個body{text-align:center;}屬性值,讓其全部居中。

二、DIV垂直居中設置技巧

關于垂直居中可以使用在CSS中代碼為:position:fixed;left:50%;top:50%;margin-left:width/2;margin-top:height/2;

<!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="zh-cn"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>水平垂直居中div演示效果</title> 
<style type="text/css"> 
.align-center{ 
position:fixed;left:50%;top:50%;margin-left:width/2;margin-top:height/2;
} 
</style> 
</head> 
<body> 
<div class="align-center">水平垂直居中div演示效果</div> 
</body> 
</html>

標簽: 居中    

欧美一级一极性活片免费观看,欧美一级艳片欧美精品,欧美一级性爱大片,欧美一级囗交视频 视频,欧美一级特黄录像视频