為什么沒(méi)有水平居中顯示?
<!DOCTYPE HTML>
<html>
<head>
? ?<meta charset="utf-8">
? ?<style type="text/css">
? ? div{
? ? ? ? border:2px solid red;
? ? ? ? width:500px;
? ? ? ? margin:10px auto;
? ? ? ? height:100px;
? ? ? ? line-height:100px;
? ? }
? ?</style>
? ?<title>定寬居中</title>
</head>
<body>
<div>i want to be in the middle.</div>
</body>
</html>
2017-01-11
你設(shè)置的margin:10px auto;代碼代表的是塊狀元素的居中(div 邊框 截圖中紅色邊框確實(shí)實(shí)現(xiàn)了居中)
文字屬于行內(nèi)元素,因此居中應(yīng)該設(shè)置為 text-align:center
<!DOCTYPE html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>定寬居中</title>
? ?<style type="text/css">
div{
? ? ? ? ? ?border:2px solid red;
width:500px;
margin:10px auto;
height:100px;
line-height:100px;
}
? ? ? ?.textCenter{
? ? ? ? ? ?text-align: center;
}
? ?</style>
</head>
<body>
? ?<div class="textCenter">i want to be in the middle.</div>
</body>
</html>