Center of the page element _ (1) element is centered horizontally

Source: Internet
Author: User

Inline element Horizontal centering scheme
    • The inline element is wrapped in an attribute display as the parent element of the block, and the parent block is text-align:center implemented
Code implementation

Index.html

<! DOCTYPE html><html lang="zh">  <head>    <meta charset="UTF-8">    <title>Inline elements are centered horizontally</title>    <style type="Text/css">      P {  display: ineline-block; }      . Testdiv {  text-align: center;         width: px;         height: px;       background: rgb ( 243, 145); }      / * Applicable elements: text, links, and other inline or inline-* type elements (inline-block,inline-table,inline-flex) * /    </style>  </head>  <body>    <div class="Testdiv">      <span>I am the Test text</span>      <b>| me too</b>      <p>I was testing, too.</P>    </div>  </body></html>
A block element horizontally centered scheme

Set the margin of the block for auto, up and down margin can be set according to demand{margin:0 auto}

Code implementation

Index.html

<! DOCTYPE html><html lang="zh">  <head>    <meta charset="UTF-8">    <title>Horizontal center of one block element</title>    <style type="Text/css"> #container {  width: + px;         Height: + px;         background: RGB (129, $) ;       margin: 0 auto; }          </style>  </head>  <body>    <div id="container">    </div>  </body></html>
Multi-block element horizontal centering scheme
    • Refer to the first implementation, internal Div set to inline block, wrap block settingstext-align:center
Code implementation

Index.html

<! DOCTYPE html><html lang="zh">  <head>    <meta charset="UTF-8">    <title>Multiple block elements are centered horizontally</title>    <style type="Text/css">      #container {  text-align: center;         background: rgb (+, 188, );         width: px;       height: px; }      #testDiv1 {  display: inline-block;         width: + px;         Height: + px;       background: #03232A; }      #testDiv2 {  display: inline-block;         width: + px;         Height: + px;       background: #122A03; }      #testDiv3 {  display: inline-block;         width: + px;         Height: + px;       background: #189FBD; }      #testDiv4 {  display: inline-block;         width: + px;         Height: + px;       background: #E20FAD; }    </style>  </head>  <body>    <div id="container">      <div id="TestDiv1"></div>      <div id="TestDiv2"></div>      <div id="TestDiv3"></div>      <div id="TestDiv4"></div>    </div>  </body></html>

Center of the page element _ (1) element is centered horizontally

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.