zoukankan      html  css  js  c++  java
  • css文字如何垂直居中?

    在我们开发前端页面的时候,为了让页面效果美观,会出现需要垂直居中效果的地方。下面本篇就让我们来了解一下用css设置文字垂直居中的方法,希望对大家有所帮助。

    方法1:使用line-height属性使文字垂直居中

    line-height属性设置行间的距离(行高);该属性不允许使用负值。

    line-height属性会影响行框的布局。在应用到一个块级元素时,它定义了该元素中基线之间的最小距离而不是最大距离。

    line-height 与 font-size 的计算值之差(在 CSS 中成为“行间距”)分为两半,分别加到一个文本行内容的顶部和底部。可以包含这些内容的最小框就是行框。

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>css 垂直居中</title>
            <style>
                .box{
                     300px;
                    height: 300px;
                    background: #ddd;
                    line-height:300px;
                }
            </style>
        </head>
        <body>
            <div class="box">css 垂直居中了--文本文字</div>
        </body>
    </html>

    效果图:

    1.jpg

    方法2:将外部块格式化为表格单元格

    表格单元格的内容可以垂直居中,将外部块格式化为表格单元格就可垂直居中文本。

    示例:将段落置于具有特定给定高度的块内

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>css 垂直居中</title>
            <style>
                .box{
                     400px;
                    height: 200px;
                    background: #ddd;
    		display: table-cell;
    		vertical-align: middle;
                }
            </style>
        </head>
        <body>
            <div class="box">css 垂直居中了--文本文字</div>
        </body>
    </html>

    效果图:

    2.jpg

    方法3:使用CSS3的flex布局 使文字垂直居中

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>css 垂直居中</title>
            <style>
                .box{
                     300px;
                    height: 200px;
                    background: #ddd;
                     /*设置为伸缩容器*/
                    display: -webkit-box;
                    display: -moz-box;
                    display: -ms-flexbox;
                    display: -webkit-flex;
                    display: flex;
                    /*垂直居中*/
                    -webkit-box-align: center;/*旧版本*/
                    -moz-box-align: center;/*旧版本*/
                    -ms-flex-align: center;/*混合版本*/
                    -webkit-align-items: center;/*新版本*/
                    align-items: center;/*新版本*/
                }
            </style>
        </head>
        <body>
            <div class="box">css 垂直居中--文本文字(弹性布局)</div>
        </body>
    </html>

    效果图:

    3.jpg

    以上就是css文字如何垂直居中?的详细内容,更多请关注html中文网其它相关文章!

    https://www.html.cn/qa/css3/12440.html

  • 相关阅读:
    项目中遇到的IE8浏览器访问页面过慢问题
    linux下安装Oracle时交换空间不足的解决方法
    linux下为目录和文件设置权限
    启动TDS LDAP 服务器遇到的问题总结
    sql1032n sql6048n db2start启动不了 db2修改hostname
    IBM CE 错误集之(FNRCS0005E)
    Oracle同一个用户下启动多个数据库实例
    几种任务调度的 Java 实现方法与比较
    EXP-00056: ORACLE error 12154 encountered
    oracle删除数据库中的所有表
  • 原文地址:https://www.cnblogs.com/findumars/p/13949784.html
Copyright © 2011-2022 走看看