Six ways to achieve vertical and horizontal centering of elements

Source: Internet
Author: User

First, the vertical horizontal center of img

Important style attributes to use Display,vertical-align
Vertical-align:middle This property is a function of centering the table element vertically, and if you want to use the IMG element, note the following display settings

<! DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>  <style type="Text/css">    . Main{  width: px;       height: px;       background-color: #aaa;  display: table; / * Parent Element Set Table properties * /     text-align: center; }          . Main span{  display: table-cell;  /*img Set as Table element properties */vertical-align: Middle; / * This property will work after two display settings * / }            </style></head><body>    <div class="main">      <span><img src="1.jpg" alt="/" width="150px"> </span>    </div></body></html>
    • Note: Display:table-cell, which works for similar text elements, is included in the span tag
      * For Text Center also h1,span,p and other similar text tags can be set to center
Second, the DIV vertical horizontal Center

The same approach applies to IMG, just change the child to IMG, no longer need span

<! DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>  <style type="Text/css">    . Main{  width: px;       height: px;       background-color: #aaa;     position: relative; }    . Child{ width: px ; height: px ; background-color: rgb (.)  ; position: Absolute ; margin: auto ;Left  : 0  ;Right  : 0  ; top: 0  ; Bottom: 0  ;}  </style></head><body>    <div class="main">      <div class="Child"></div>    </div></body></html>
Three, easy to achieve the vertical center of most elements

Center horizontally, if text (inline Element) Text-align:center,div (block-level element) margin:0 auto;, so I don't write horizontal center, don't think I'm lazy oh

    • will use the properties Display:flex and Align-items
    • I'm going to say, Display:flex, show the object as an elastic telescopic box
    • Align-items: Defines the alignment of the Flex subkey in the side axis (vertical axis) direction of the current line of the Flex container.
.main{/*给父容器设置*/display:flex;align-items:center;/*所有子元素都垂直居中了*/}
Iv. using the CSS3 attribute transform
 transform: translateY(50%);/*给子元素设置*/
    • Transform:translatex (50%) can also be horizontally centered, but above already said, can use margin:0 Auot (Block level), text-align (inline), Horizontal vertical Center Transform:translatex (50%) Translatey (50%);

The advantages of all vertical centering above: it is not necessary to know the size of the parent element and child elements, then the following method needs to know the size, but is not bad, some places may be very convenient to use, to see the situation in your project

V. Vertical centering of single-line text
    • Sets the height of the line-height== parent element of the text
<! DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Test</title>  <style type="Text/css"> . Block{  height: px;       background-color: blue;  line-height: px; /* values are the same height as the parent element */     text-align: center; }            </style></head><body>  <div class="Block">Single-line text vertically centered</div></body></html>
Six, need to know the dimensions of child elements
    • achieve horizontal and vertical centering
/*省略了尺寸的设置,侧重了重点,读者可以把部分内容加上*/    .main{/*父元素*/      position: relative;    }    .child{/*子元素*/        position: absolute;        top: 50%;        left: 50%;        margin-left: /*负的自身宽度一半*/        margin-top: /*负的自身高度一半*/    }
Vii. Summary

The above six methods of compatibility I do not have one by one tests, if the reader has a good opinion, I hope you come forward, thank you

Six ways to achieve vertical and horizontal centering of elements

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.