CSS Horizontal vertical centering those things

Source: Internet
Author: User

This article is in the reference to the great God article based on the collation of a number of common programs, in addition to some of the personal speculation, if there is inappropriate, please do not hesitate to point out
Below the beginning of the topic, in order to facilitate the verification + display, the following case I will directly attach the personal verification of the source +

1.

<!doctype html>}</style>

(All instances run, similar to the right, the following is not a sticky map (the author is too lazy ....) ))

This case mainly uses two Div,container and innercnt composition, sub-div set, fixed width high (not auto), absolute positioning (absolute), top/right/bottom/ Left is set to 0 to achieve a horizontal vertical centering effect:

Here is my guess: in absolute positioning, if you do not set the coordinate value (the distance on the right and bottom left), the default is to snap to the upper left of the parent container, but at the same time set the upper right and bottom left the distance is equivalent to four people at the same time in the four direction together in the pull rope, when the same force is Allows the container to be horizontally and vertically centered ... 233 I'm too good to be sure of that.

2.

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Document</title>6 <styletype= "Text/css">7 . Container{8 Display:Table-cell;//Layout in tabular form9 width:500px;Ten Height:500px; One vertical-align:Middle;//Vertical Center A text-align:Center;//Horizontal Center - Border:1px solid #000000; - } the  - . innercnt{ - Display:Inline-block; - width:200px; + Height:200px; - Border:1px solid #FF0000; + } A </style> at </Head> - <Body> - <Divclass= "Container"> - <Divclass= "innercnt"></Div> - </Div> - </Body> in </HTML>

This example mainly shows the application of Table-cell

Table-cell This element is displayed as a table cell (similar to <td> and <th>)

Make the whole chunk as a cell, control the vertical center by vitical-align, Text-align control the horizontal center (note that the child element should be set to Inline-block as the inline chunk).

3.

<!DOCTYPE HTML><HTMLLang= "en"><Head><MetaCharSet= "UTF-8"><title>Document</title><styletype= "Text/css">. Container{text-align:Center;width:500px;Height:500px;Border:1px solid Blue;}. Empty{width:100%;Height:50%;Margin-bottom:-100px;//calculated; =INNERCNT.HEIGHT/2}. innercnt{Display:Inline-block;width:200px;Height:200px;Border:1px solid Red;}</style></Head><Body><Divclass= "Container">    <Divclass= "Empty"><!--Empty Content -</Div>    <Divclass= "innercnt">Hello</Div></Div></Body></HTML>

Use an empty div to occupy a bit, and then take a placeholder through the margin-bottom to make the elements below the level (Text-align:center) vertically centered.

CSS Horizontal vertical centering those things

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.