Horizontal center of div elements in CSS text Images

Source: Internet
Author: User

Text center, text vertical center horizontal center, picture center, picture horizontal center vertical center, block element vertical center? Css Center is a common issue when we are doing front-end development. There are many situations, and there are different solutions for different situations. The horizontal center method is relatively easy to set through css, and the vertical center is relatively tricky.

Let's talk about the different situations and Solutions of horizontal center.

1. the horizontal center of text, images, and other intra-row elements is relatively simple. You can set a text-align: centent attribute for the parent element to horizontally center the element.

2. How do I set horizontal center for block-level elements with fixed width? The setting method is not difficult. You can set the margin, margin: 0 auto; To realize horizontal center of block-level elements with fixed width.

3. There are multiple ways to set horizontal center for block-level elements with uncertain width. Here we will introduce three methods for you.

The first method is to use table as a container. This method is not recommended because meaningless labels are added. Here is a brief introduction. The Table label itself is not a block-level element. If we do not set the table width, the width in the Table is supported by the width of its internal elements. But even if we do not set the table width, we can directly set the table's margin: 0 auto; To achieve horizontal center! In this way, we can indirectly center the content in the table by setting the horizontal center of the table.

In the second method, compared with the table method, the advantage is that you do not need to add silent labels to simplify the depth of tag nesting. This idea is like this. By changing the display attribute value of the block-level element to the inline type, and then setting text-align: center to center. This method also makes it possible to change block-level elements into intra-row elements, which have fewer functions than block elements. For example, if you set the length and width, there may be some restrictions in the use of the project, you can choose your own.

The following is the third method. Set floating float for the parent element, set the position attribute of the parent element to relative and left: 50%, and set position: relative and left:-50% for the child element to realize horizontal center. This advantage is that block-level elements can still be displayed in the form of display: block, without adding meaningless labels or nested depth. It is true that position: relative; has some side effects. The following is the instance code of the third method, which can be placed in the body tag.

<Div style = "width: 400px; height: 300px; background: #333;">

<Ul style = "margin: 0 auto; float: left; position: relative; left: 50%; list-style: none; padding: 0;">

<Li style = "position: relative; left:-50%; float: left"> OKOKOKOKKOOKK </li>

</Ul>

</Div>

The three methods are widely used and all have disadvantages and advantages. The method you choose depends on the situation. This section describes the horizontal center of Css text image block elements.

This article was first launched at: frontend

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.