div居中與div內容居中,不一樣


1、div自身居中

使用margin:0 auto
上下為0,左右自適應的css樣式。

要讓div水平居中,那么除了設置css margin:0 auto外,還不能再設置float,不然將會導致div靠左(設置float:left)和div靠右(設置float:right)。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>div布局居中實例 thinkcss</title>
<style>
#jz{ margin:0 auto; width:320px; height:100px; border:1px solid #F00}
</style>
</head>
<body>
<div id="jz">設置margin:0 auto兼容各大瀏覽器讓div水平居中</div>
</body>
</html>

以上實例正是使用margin:0 auto讓div兼容各大瀏覽器的水平居中。

2、div內的內容居中

內容居中分為div內容水平居中與div內容垂直居中。

而div內容居中比較簡單,只需要設置一個內容居中css(text-align:center)、內容垂直居中(line-height)即可。

(1)div內容水平居中CSS:
text-align:center
無論是p還是div都可以對其設置此CSS實現對應對象內的內容水平居中。

(2)div內容垂直居中 行高屬性:
line-height
要讓div內只有一行的內容垂直居中,通常對div設置的height(高)與line-height(行高)相同,即可實現div內容垂直居中。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>div內容居中實例 thinkcss</title>
<style>
#juzhong{width:320px;height:100px; line-height:100px; text-align:center; border:1px solid #F00}
</style>
</head>
<body>
<div id="juzhong">text-align和line-height設置水平與垂直居中</div>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM