如何使用CSS实现文本左对齐、右对齐和居中对齐


本文摘自PHP中文网,作者不言,侵删。

文本的位置在网页布局中是必不可少的,今天的这篇文章就来给大家分享关于使用CSS实现文本左对齐、右对齐和居中对齐的方法,下面我们来看具体的内容。

要设置文本对齐(左对齐,右对齐,居中对齐)需要用到的CSS属性是text-align属性。(相关推荐:CSS学习手册)

语法格式:

1

text-align :(文本位置)

位置说明
left左对齐
right右对齐
center居中对齐
justify两端对齐

接下我们来分别看看这四种对齐方式的设置方法

文本左对齐的设置方法:

1

text-align:left;

文本右对齐设置方法:

1

text-align:right;

文本居中对齐设置方法:

1

text-align:center;

文本两端对齐设置方法:

1

text-align:justify;

我们来看具体的代码示例1:

TextAlign.html

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

31

32

33

34

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <link rel="stylesheet" href="TextAlign.css" />

    <title></title>

    </head>

    <body>

  <div class="TextLeft">

    php中文网左对齐<br />

    php中文网<br />

    php中文网<br />

    php

  </div>

  <div class="TextRight">

    php中文网右对齐<br />

   php中文网<br />

    php中文网<br />

    php

  </div>

  <div class="TextCenter">

    php中文网居中对齐<br />

    php中文网<br />

    php中文网<br />

    php

  </div>

  <div class="TextJustify">

    php中文网<br />

    两端对齐<br />

    php中文网<br />

    php

    </div>

    </body>

    </html>

TextAlign.css

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

.TextLeft{

    margin-top:24px;   

    margin-left:32px;   

    border:1px solid #ff6a00;   

    width:480px;   

    text-align:left;

}

.TextRight {

    margin-top: 24px;   

    margin-left: 32px;   

    border: 1px solid #ff6a00;   

    width: 480px;   

    text-align: right;

}

.TextCenter {

    margin-top: 24px;   

    margin-left: 32px;   

    border: 1px solid #ff6a00;   

    width: 480px;   

    text-align: center;

}

.TextJustify {

    margin-top: 24px;   

    margin-left: 32px;   

    border: 1px solid #ff6a00;   

    width: 480px;   

    text-align: justify;   

    text-justify: distribute-all-lines;

}

显示结果

使用Web浏览器显示上述HTML文件。将显示如下所示的效果。

360截图20181115101423160.jpg

代码示例2:

文本左对齐:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <meta charset="utf-8">

    <style type="text/css">

.leftText{

  text-align:left;

  }

    </style>

</head>

<body>

<div class="leftText">左对齐</div>

</body>

</html>

文本右对齐:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <meta charset="utf-8">

    <style type="text/css">

.rightText{

  text-align:right;

  }

    </style>

</head>

<body>

<div class="rightText">右对齐</div>

</body>

</html>

居中对齐:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <meta charset="utf-8">

    <style type="text/css">

.centerText{

  text-align:center;

  }

    </style>

</head>

<body>

<div class="centerText">居中对齐</div>

</body>

</html>

本篇文章到这里就全部结束了,更多关于文本对齐的内容大家可以关注php中文网的CSS视频教程栏目!!!

以上就是如何使用CSS实现文本左对齐、右对齐和居中对齐的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

dreamweaver怎么快速对齐代码? dw对齐代码的设置方法_dreamweaver教程_网页制作

如何使用css实现文本左对齐、右对齐和居中对齐

css怎么设置两个字和三个字对齐

css如何设置对齐

li标签实现日期,标题右对齐的方法

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




打赏

取消

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

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

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

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

评论

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