Turn: Div+css How to make a small div vertically centered in another big Div

Source: Internet
Author: User

Method 1: Absolute positioning +margin:auto

 .parent {          width:800px;           height:500px;           border:2px solid  #000;           position: relative;}  .child {            width:200px;             height:200px;             margin: auto;               position: absolute;               top: 0; left: 0; bottom: 0; right: 0;              background-color: red;} 

Method 2:display:table-cell

.parent {            width:800px;             height:500px;             border:2px solid  #000;             display:table-cell;             vertical-align:middle;             text-align: center;        }         .child {            width:200px;             height:200px;             display:inline-block;             background-color: red;        } 

Method 3:display:flex

 .parent {            width:800px;             height:500px;             border:2px solid  #000;             display:flex;             justify-content:center;             align-items:center;        }         .child {            width:200px;             height:200px;             background-color: red;         } 

Method 4: Absolute positioning +margin

. parent {width:800px;            height:500px;            border:2px solid #000;        position:relative;            }. Child {width:300px;            height:200px;            Margin:auto; position:absolute;/* sets the horizontal and vertical offset of the parent element by 50%, and then moves the child element left half the size according to the actual length */
left:50%;
top:50%;
Margin-left: -150px;
Margin-top: -100px;
background-color:red;
}

Turn: Div+css How to make a small div vertically centered in another big Div

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.