不是一般标签居中都用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>"
When the block attribute label is centered, you need to set the width. You can just change it like this
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
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