css怎么设置字体居中?


本文摘自PHP中文网,作者青灯夜游,侵删。

css怎么设置字体居中?下面本篇文章就来给大家介绍一下,希望对大家有所帮助。

一、CSS设置字体水平居中

在CSS中可以使用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: #ddd;

                text-align:center;

            }

        </style>

    </head>

    <body>

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

    </body>

 

</html>

效果图:

1.jpg

二、CSS设置字体垂直居中

1、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: #ddd;

                line-height:300px;

            }

        </style>

    </head>

    <body>

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

    </body>

</html>

效果图:

9.8.2.jpg

这样就能让div中的文字水平垂直居中了

2、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

30

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

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

        <style>

            .box{

                width: 300px;

                height: 300px;

                background: #ddd;

                line-height:300px;

                 /*设置为伸缩容器*/

                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>

效果图:

9.8.2.jpg

3、vertical-align:middle +display:table-cell 使文字垂直居中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

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

        <style>

            .box {

                width: 300px;

                height: 300px;

                background: #ddd;

                vertical-align:middle;

                display:table-cell;

            }

        </style>

 

    </head>

 

    <body>

 

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

 

    </body>

 

</html>

效果图:

3.jpg

说明:vertical-align:middle +display:table-cell能够使单行文字、多行文字都居中。但是因为 table-cell 是 inline 类型,所以会导致原来的块级元素每个 div 一行移动到了同一行。如果需要分列两行,需要在外面额外添加容器对位置进行控制。

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

相关阅读 >>

css如何改变图片的背景

css如何设置一行字显示不完隐藏

css max-height属性怎么用

css margin-right属性怎么用

详解css行高line-height属性

深入了解css属性*-gradient的使用技巧

css搜索框怎么写

css中无继承性的属性有哪些

css3实现条状百分比效果

css怎么设置溢出隐藏

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




打赏

取消

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

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

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

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

评论

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