[记录]-DIV内文字的居中

DIV中的文字水平居中显示

要使盒子内的文字水平居中,最常规的方法是直接对该盒子设置text-align:center属性达到左右居中的效果。

还有一种方法:对该div的文字套上span标签,然后设置margin属性:上下固定、左右自适应。(需要注意的是,span是行内元素,只能设置margin的左右方向,上下方向是不生效的)

<style>
    .w {
        width: 1200px;
    }

    .w span {
        margin:0 auto;
    }
</style>

<body>
    <div class="w">
        <span> this is demo! </span>
    </div>
</body>

例如上面代码,将类为w的div盒内文字设置成左右居中。

DIV中的文字垂直居中显示

要使盒子内的文字垂直居中,只需对该div设置行高等于高即可。

<style>
    .shortcut {
        height: 31px;
        background-color: #f1f1f1;
        line-height: 31px;
    }
</style>

<body>
    <div class="shortcut">this is demo!</div>
</body>

例如上面代码,将类为shortcut的div盒子高度设为31px,同时行高也设置成31px,这样就成功使文字垂直居中了。

猜你喜欢

转载自blog.csdn.net/jgsecurity/article/details/121003600
今日推荐