CSS-—— Horizontal Centering settings

Source: Internet
Author: User

1. Inline element Horizontal Centering method: If the element is set as text, picture, and other inline elements, the horizontal center is implemented by setting Text-align:center to the parent element.

2. Block element--Fixed block element horizontal centering method:

(1) fixed-width block element: The width of the block element is a constant value.

(2) When width is set to a fixed value, Text-align:center does not work and is centered by setting the "left and right margin" value to "auto".

(3) such as: width:200px;/* fixed width */ margin:20px auto/* Center * /


3. Block element--horizontal centering of indeterminate block elements Method 1:

(1) Add the table label, using the length of the table label Adaptive---that does not define its length or the length of the default parent element body (table whose length depends on the length of its text), so it can be regarded as a fixed-width block element, Then use the fixed-width block-centered margin method to make it horizontally centered.

(2) Method: First step: Add a table label (including <tbody>, <tr>, <td>) to the outside of the center element you want to set.

(3) Step two: Set the "left and right margin center" for this table (this is the same as for a fixed-width block element).

(3) Example:

. wrap{
Background: #ccc;
display:table;
margin:0 Auto;
}

<div class= "Wrap" >
Set my div container to center horizontally
</div>

4. Block element--horizontal centering of indeterminate block elements Method 2:

(1) Changing the display of block-level elements to the inline type (set to the inline element display) is then used text-align:center to achieve the centering effect.

(2) such as:

. container ul{    list-style:none;    margin:0;    padding:0;    Display:inline;}
5. Block element--horizontal centering of indeterminate block elements Method 3:
(1) By setting float to the parent element and then setting position:relative and left:50% for the parent element , the child elements are set position:relative and left : -50% to achieve horizontal centering.

CSS-—— Horizontal Centering settings

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.