块级元素内容和自身水平居中

css中,text-align属性可以让文字、图片等在块级元素中居中,如果要让块级元素本身居中(水平居中)可以用margin属性的auto值 <html lang="en">
text-align 只能规定水平对齐方式

<head>
    <meta charset="UTF-8" />
    <title>abc</title>
    <style type="text/css">
    div {
        100px;//规定宽度,块级元素居中才有意义
        margin: auto;
    }


    </style>
</head>


<body>
    
    <div class="margin">这个段落</div>
    
</body>


</html>

如果不规定宽度,默认宽度为父级元素宽度,默认文字左对齐,看到的效果还是文字在左侧

原文地址:https://www.cnblogs.com/cowboybusy/p/9067058.html