不是一般標簽居中都用margin:0 auto;嗎 文本一般用text-align:center;為什么這里a標簽不能水平居中呢?下面這句話是我百度出來的 為什么我給a標簽設置display:block了
用margin:0 auto也不能居中呢?只能加text-align:center才能居中 這是為什么?
1.text-align:center 設置文本或img標簽等一些內聯(lián)對象(或與之類似的元素)的居中。
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>"
認證0級講師
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