css - 这个a标签为什么不能水平居中?
PHP中文网
PHP中文网 2017-04-17 13:13:43
0
1
970

不是一般标签居中都用margin:0 auto;吗 文本一般用text-align:center;为什么这里a标签不能水平居中呢?下面这句话是我百度出来的 为什么我给a标签设置display:block了
用margin:0 auto也不能居中呢?只能加text-align:center才能居中 这是为什么?

1.text-align:center 设置文本或img标签等一些内联对象(或与之类似的元素)的居中。
2.margin:0 auto 设置块元素(或与之类似的元素)的居中。

<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <title>主页</title>
   <style type="text/css">
   body,p,ul,li,a,form{margin: 0;padding: 0;}
   #p1{border: 1px solid black;width: 800px;height: 50px;}
   #p1 a{margin: 0 auto;display:block;}
   </style>
   <script type="text/javascript">
   </script>
</head>
<body>
<p id="p1">
    <a href="#">aaaaaaaaaaaaassssssssssssss</a>
  </p>
</body>
</html>"
PHP中文网
PHP中文网

认证0级讲师

reply all(1)
洪涛

When the block attribute label is centered, you need to set the width. You can just change it like this

#p1 a{width:100px;margin: 0 auto;display:block;}

The reason is actually very simple. When the width is not set, the a tag set to block will occupy the entire width of the parent element (fluid layout) then

margin:0 auto == margin:0 0

So there is no centering effect. After setting the width, you will feel that your width and the width of the parent element are calculated as marigin

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!