为了账号安全,请及时绑定邮箱和手机立即绑定

为什么没有水平居中显示?

<!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>

http://img1.sycdn.imooc.com//5875e2c20001a89c19201080.jpg

正在回答

1 回答

你设置的margin:10px auto;代码代表的是块状元素的居中(div 边框 截图中红色边框确实实现了居中)

文字属于行内元素,因此居中应该设置为 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>

0 回复 有任何疑惑可以回复我~
#1

TicoM 提问者

非常感谢!
2017-01-11 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
初识HTML(5)+CSS(3)-升级版
  • 参与学习       1222829    人
  • 解答问题       18211    个

HTML(5)+CSS(3)基础教程8小时带领大家步步深入学习标签用法和意义

进入课程

为什么没有水平居中显示?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信