5 kinds of ideas about CSS to achieve horizontal and vertical centering

Source: Internet
Author: User
The following small series for everyone to bring an analysis of the CSS to achieve horizontal vertical and center 5 kinds of ideas. Small series feel very good, now share to everyone, also give us a reference, follow the small series come and see it

Horizontal center and vertical centering have been introduced separately, this article will introduce 5 kinds of ideas of horizontal vertical center simultaneously

idea One: text-align + line-height to achieve horizontal vertical center of single line text

<style>   . test{       Text-align:center;       line-height:100px;   }   </style>

xml/html Code copy content to clipboard


    1. < P class="Test" style="background-color:lightblue;width:200px;" > Test text </p>


idea Two: text-align + vertical-align

"1" sets Text-align and vertical-align in the parent element and sets the parent element to the Table-cell element, and the child element is set to the Inline-block element

[note] If the Ie7-browser is compatible, the structure will be changed to <table> structure to achieve the Table-cell effect; display:inline;zoom:1 to achieve inline-block effect

<style>   . parent{       Display:table-cell;       Text-align:center;       Vertical-align:middle;   }   . child{       display:inline-block;   }   </style>
<p class= "Parent" style= "Background-color:gray; width:200px; height:100px; " >  <p class= "Child" style= "Background-color:lightblue;" > Test Text </p></p>


"2" If the child element is an image, instead of using Table-cell, the parent element uses the row height instead of the height, and the font size is set to 0. The child element itself is set Vertical-align:middle

<style>   . parent{       Text-align:center;       line-height:100px;       font-size:0;   }   . child{       vertical-align:middle;   }   </style>
<p class= "Parent" style= "Background-color:gray; width:200px; ">  </p>


idea Three: margin + vertical-align

To set the vertical-align in the parent element, set it to the Table-cell element, and set it to the table element if you want the content of the block element to be horizontally centered by margin:0 Auto to open the width. The table element can be nested inside the Tabel-cell element, like a cell where a table can be nested

[note] If the Ie7-browser is compatible, the structure should be changed to <table> structure

<style>   . parent{       Display:table-cell;       Vertical-align:middle;   }   . child{       display:table;       margin:0 auto;   }   </style>


<p class= "Parent" style= "Background-color:lightgray; width:200px; height:100px; ">  <p class=" Child "style=" Background-color:lightblue; " > Test Text </p></p>


idea four: using absolute

The "1" uses the box model property of the absolutely positioned element, and sets the Margin:auto based on the offset attribute as the determined value.

<style>   . parent{       position:relative;   }   . child{       Position:absolute;       top:0;       left:0;       rightright:0;       bottombottom:0;       height:50px;       width:80px;       Margin:auto;   }   </style>


<p class= "Parent" style= "Background-color:lightgray; width:200px; height:100px; ">  <p class=" Child "style=" Background-color:lightblue; " > Test Text </p></p>


2 uses the Offset property of an absolutely positioned element and the translate () function's own offset to achieve a horizontal vertical center effect

Note ie9-Browser does not support

<style>   . parent{       position:relative;   }   . child{       Position:absolute;       top:50%;       left:50%;       Transform:translate ( -50%,-50%);   }   </style>
<p class= "Parent" style= "Background-color:lightgray; width:200px; height:100px; ">  <p class=" Child "style=" Background-color:lightblue; " > Test Text </p></p>


"3" can be used to achieve horizontal vertical center effect with margin negative value when child element height is known

<style>   . parent{       position:relative;   }   . child{       Position:absolute;       top:50%;       left:50%;       width:80px;       height:60px;       Margin-left: -40px;       Margin-top: -30px;   }   </style>
<p class= "Parent" style= "Background-color:lightgray; width:200px; height:100px; ">  <p class=" Child "style=" Background-color:lightblue; " > Test Text </p></p>


idea Five: using Flex

Note ie9-Browser does not support

"1" Use Margin:auto on telescopic projects

<style>   . parent{       Display:flex;   }   . child{       margin:auto;   }   </style>
<p class= "Parent" style= "Background-color:lightgray; width:200px; height:100px; ">  <p class=" Child "style=" Background-color:lightblue; " > Test Text </p></p>


"2" Use spindle alignment justify-content and side axis alignment on telescopic container Align-items

<style>   . parent{       Display:flex;       Justify-content:center;       Align-items:center;   }   </style>


<p class= "Parent" style= "Background-color:lightgray; width:200px; height:100px; ">  <p class=" Child "style=" Background-color:lightblue; " > Test Text </p></p>


The above analysis of the CSS to achieve horizontal vertical at the same time the center of the 5 ideas is small to share all the content of everyone, hope to give you a reference, but also hope that we support topic.alibabacloud.com.

Related Article

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.