css如何设置文字居中


本文摘自PHP中文网,作者醉折花枝作酒筹,侵删。

方法:1、使用text-align属性设置文字水平居中,语法“text-align:center”;2、使用line-height属性设置文字垂直居中,语法“line-height:数值”;3、使用CSS3的flex布局设置文字垂直居中。

本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。

1、使用text-align属性来使文字水平居中

text-align属性规定元素中的文本的水平对齐方式,通过使用center值设置文本居中。

text-align是一个基本的属性,它会影响一个元素中的文本行互相间的对齐方式。值left、right和center会导致元素中的文本分别左对齐、右对齐和居中,想要使文本居中,直接使用center即可。

该属性设置文本和img标签等一些内联对象(或与之类似的元素)的居中。

该属性有如下几个特点:

1)、text-align的center应用在一个容器上,它只针对容器里面的文字以及容器里面的display为inline或者inline-block的容器,如果里面的容器display为block,则里面的容器的内容不会居中。

2)、text-align具有向下传递性,会不断地向子元素传递。如果设置一个div,则其子div中的内容也会居中。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <title>css 水平居中</title>

        <style>

            .box {

                width: 400px;

                height: 100px;

                background: pink;

                text-align:center;

            }

        </style>

    </head>

    <body>

        <div class="box">css 水平居中了--文本文字</div>

    </body>

 

</html>

效果图:

8K4U3CP[[{5HE(F25G4K0%C.png

2、使用line-height属性来使文字垂直居中

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <title>css 垂直居中</title>

        <style>

            .box {

                width: 300px;

                height: 300px;

                background: paleturquoise;

                line-height:300px;

            }

        </style>

    </head>

    <body>

        <div class="box">css 垂直居中了--文本文字</div>

    </body>

</html>

效果图:

]XB6(B]@@CC07F8Y92AM`KX.png

3、CSS3的flex布局 使文字垂直居中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <title>css 垂直居中</title>

        <style>

            .box{

                width: 300px;

                height: 200px;

                background: #ccc;

                 /*设置为伸缩容器*/

                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>

效果:

D%PE5O{2PE3ALV(V@R0V~DS.png

推荐学习:css视频教程

以上就是css如何设置文字居中的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

jquery实现带弹窗和次数的转盘抽奖(代码)

css list-style-image属性怎么用

css的sass样式应该怎么使用

css中清除浮动有哪几种方式

css中怎么设置背景图

css字符间距怎么设置

css 布局之两端布局实现

如何利用css实现放大缩小关闭效果

html5和css 实现禁止ios长按复制粘贴功能

css怎么实现超出高度隐藏

更多相关阅读请进入《css》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...