Center css elements and center css Elements

Source: Internet
Author: User

Center css elements and center css Elements
Horizontal Center

If it is a row element, use text-align: center for its parent element;

If it is a block element (not floating), there are two ways to horizontally center it, one is margin: 0 auto; the other is to calculate the function calc through css;

Vertical center

If it is a row element, line-height: {height} is generally used for its parent element. If it is an image element, vertical-align: middle must be used for this element;

In addition, you can use vertical-align: middle to vertically center the elements in table element td, and use display: table-cell; vertical-align: you can also vertically center an element. This method can be used regardless of the element in the row or block element;

If it is a block element (the row element will eventually become a block element when this method is used), you can use the position and other attributes to vertically center the element. The basic steps are as follows: 1. set the parent element position: relative; 2. set the element position: absolute; top: 50%; left: 50%; 3. set the element margin-top:-{height}/2; margin-left:-{width}/2;

 

Center of floating Element

If it is a floating block element (such as float: left), you need to use the position and other attributes to realize the horizontal vertical center of the element. The basic steps are as follows: 1. the outer layer of the element sets a div and sets its style: float: left; overflow: hidden; display: inlineblock; 2. set the position attribute of the outer div to relative, and set the top and left values to 50%. 3. set the element style: transform: translate (-50%,-50% );

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.