為什么我設置定高,然后之間margin: auto; 不能使它上下左右都居中呢?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定寬塊狀元素水平居中</title>
<style>
div{
? ? border:1px solid red;
? ? width:200px;
? ? height:200px;
margin:auto;
}
</style>
</head>
<body>
<div>我是定寬塊狀元素,我要水平居中顯示。</div>
</body>
</html>
2017-01-18
margin-top:?-200px;這個-200px是什么意思?
2016-10-27
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>定寬塊狀元素水平居中</title> <style> div?{ ????width:?400px; ????height:?400px; ????line-height:?400px; ????border:?1px?solid?#006699; ????position:?absolute; ????top:?50%; ????left:?50%; ????margin-top:?-200px; ????margin-left:?-200px; ????text-align:?center; } </style> </head> ? <body> <div>我是定寬塊狀元素,我要水平居中顯示。</div> ? </body> </html>不知道你還在不在,我做出來了
2016-10-27
給div加table/tr/td,別忘了把選擇器也改成table,其它什么也不用改
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>定寬塊狀元素水平居中</title> <style> table{ ????border:1px?solid?red; ????width:200px; ????height:200px; ????margin:auto; } </style> </head> <body> <table><th><td> <div>我是定寬塊狀元素,我要水平居中顯示。</div> </td></th></table> </body> </html>