CSS Alignment

CSS Horizontal Align (Horizontal Align)

In CSS, there are several properties for horizontal alignment of elements.


Block element alignment

The block element is an element that occupies the full width and is preceded and followed by line breaks.

Example of block element:

  • <h1>

  • <p>

  • <div>

In this chapter, we will tell you how to align block elements horizontally in layout.


Center alignment, use the margin attribute

The block element can set the left and right margins to "Auto "Align.

The margin attribute can be arbitrarily split into left and right margin settings and automatically specified. The result will be a centered element:

Example

      <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文网(php.cn)</title>
    <style>
        .center
        {
            margin:auto;
            width:70%;
            background-color:#b0e0e6;
        }
    </style>
</head>

<body>
<div class="center">
    <p>人心灵的伤痛若无知己来抚慰,便会荒芜;人的欢乐若知己来共享,再多的快乐也是悲伤。知己之于人,本是必不可缺。一个人的痛苦要找人倾诉,一个人的愁绪要有人来排解。人若没有知己,心灵便是一片怎样的荒凉。    青春都是如此,带着疼痛,却又义无反顾。世上本无事,庸人自扰之。人生在世,总是有些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。</p>
</body>
</html>

Tip: If the width is 100%, alignment has no effect.

Run the program and try it


Use the position attribute to set left and right alignment

One of the ways to align elements One is to use absolute positioning:

  <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文网(php.cn)</title>
    <style>
        .right
        {
            position:absolute;
            right:0px;
            width:300px;
            background-color: #4ce667;
        }
    </style>
</head>

<body>
<div class="right">
    <p>人心灵的伤痛若无知己来抚慰,便会荒芜;人的欢乐若知己来共享,再多的快乐也是悲伤。知己之于人,本是必不可缺。一个人的痛苦要找人倾诉,一个人的愁绪要有人来排解。人若没有知己,心灵便是一片怎样的荒凉。    青春都是如此,带着疼痛,却又义无反顾。世上本无事,庸人自扰之。人生在世,总是有些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。</p>
</div>
</body>
</html>

Note: Absolute positioning has nothing to do with document flow, so they can cover other elements on the page.

Run the program and try it


##Use the float attribute to set left and right alignment

Use the float attribute to align One of the methods of elements:

      <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文网(php.cn)</title>
    <style>
        .right
        {
            float:right;
            width:300px;
            background-color:#b0e0e6;
        }
    </style>
</head>

<body>
<div class="right">
    <p>人心灵的伤痛若无知己来抚慰,便会荒芜;人的欢乐若知己来共享,再多的快乐也是悲伤。知己之于人,本是必不可缺。一个人的痛苦要找人倾诉,一个人的愁绪要有人来排解。人若没有知己,心灵便是一片怎样的荒凉。    青春都是如此,带着疼痛,却又义无反顾。世上本无事,庸人自扰之。人生在世,总是有些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。</p>
</div>
</body>
</html>

Run the program and try it




Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .center { margin:auto; width:70%; background-color:#b0e0e6; } </style> </head> <body> <div class="center"> <p>人心灵的伤痛若无知己来抚慰,便会荒芜;人的欢乐若知己来共享,再多的快乐也是悲伤。知己之于人,本是必不可缺。一个人的痛苦要找人倾诉,一个人的愁绪要有人来排解。人若没有知己,心灵便是一片怎样的荒凉。</p> <p>青春都是如此,带着疼痛,却又义无反顾。世上本无事,庸人自扰之。人生在世,总是有些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。</p> </div> </body> </html>
submitReset Code