The center of the page Element _ (4) Use the CSS3 attribute to center the element horizontally vertically

Source: Internet
Author: User

The perfect centering scheme of the flex implementation sub-blocks

Parent block using display:flex attributes, child block margin adaptive can be implemented

Code

Index.html

<! DOCTYPE html><html lang="zh"><head>  <meta charset="UTF-8">  <title>Block element vertically centered (known height)</title>  <style>*{margin: 0;  padding: 0; }      . Parent{  display: flex;           height:px;       background: #03A1FA; }      . Child{  width: + px;         Height: + px;         margin: auto;       background: #BB3713; }      / * This relies on setting the "margin" value to "auto" value to automatically get the remaining space in the telescopic container.       So setting the vertical margin value to "Auto" allows the telescopic item to be fully focused on both upper axes of the telescopic container. */  </style></head><body>      <div class="parent">            <div class="Child">Ff</div>      </div></body></html>
Horizontal vertical centering scheme with unknown width and height elements

Use the Transform:translate property to adjust

Code

Index.html

<! DOCTYPE html><html lang="zh"><head>  <meta charset="UTF-8">  <title>Block element horizontally vertically centered (unknown width height)</title>  <style>*{margin: 0;  padding: 0; }      / * Simplified version reset, working environment not recommended * /    #container{ position:Absolute ; top:% ;Left  :% ; -webkit-transform: Translate (-%,-)  ; -ms-transform: Translate (-%,-)  ; -o-transform: Translate (-%,-)  ; transform: Translate (-%,-)  ; background-color: #A4FF00  ; color:rgb ( 172, 134) ;}    / * Because we do not know the width height, we mainly use the Translate property to pull back the position on the XY axis of the Unknown element center * /    /* If the element with the unknown height is only centered, it can be implemented by using Transform:translatey (-50%) */  </style></head><body>      <div id="container">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae Obcaecati Expedita ducimus, rem laborum veniam qui quo facilis enim. Repellat Blanditiis Nemo, Magnam, Sequi illum perferendis consequatur Modi maiores quaerat?</div></body></html>
Flex implements horizontal vertical center of page scheme

Define the parent element of the center element Justify-content and Align-items properties are center, you need to set enough height, otherwise only the horizontal center effect

Code implementation

Index.html

<! DOCTYPE html><html lang="zh">  <head>    <meta charset="UTF-8">    <title>Flex achieves perfect centering of sub-blocks</title>    <style>*{  margin: 0;       padding: 0; }      . Parent {  display: flex;         justify-content: center;         align-items: center;         Height: +px;       background: #03A1FA; }      . Child {  width: + px;         Height: + px;       background: #BB3713; }    </style>  </head>  <body>    <div class="parent">      <div class="Child">La La Demanue</div>    </div>  </body></html>

The center of the page Element _ (4) Use the CSS3 attribute to center the element horizontally vertically

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.