CSS text Picture div element centered method horizontally

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

Center text, the vertical center of the text horizontally, the picture centered, the picture horizontally centered vertically, the block elements vertically centered? When we are doing front-end development it is very common to have a CSS centered problem. There are many different situations and different ways of solving them. Horizontally centered way to solve the relatively easy to set through the CSS, vertical center is relatively tricky.

First of all, the horizontal center of the different situations and different solutions.

1 text, pictures and other inline elements in the horizontal center method is relatively simple. Setting a Text-align:centent property directly to the parent element enables you to center the element horizontally.

2 determines the width of the block-level elements in the horizontal center how do you set it? It is also easy to set the method by setting the outer margin, margin:0 auto;

3 block-level elements with uncertain widths there are several ways to center horizontally, and here are three methods for everyone.

The first method is implemented using table as a container. Of course, this method is not recommended because it adds meaningless tags. Here's a little introduction. The table label itself is not a block-level element, and when we don't set the width of the table, the width of his inside is propped up by the width of his inner element. But even if we do not set the width of the table, directly set the outer margin of the table margin:0 auto; You can center it horizontally! This allows us to center the contents by setting the table horizontally.

The second method, compared to the use of the table method, has the advantage of simplifying the label nesting depth without adding no semantic tags. The idea is, by changing the display property value of the block-level element to the inline type, and then setting the Text-align:center to center it. This method also has a certain flaw is that the block-level elements changed into inline elements, inline elements less than block elements of some functionality. For example, set a long width value, in the application of the project may have some restrictions, we can choose.

Here is the third method. Center horizontally by setting floating float for the parent element, and then setting the Position property of the parent element to relative and left:50%, and the child element setting position:relative and left:-50%. The advantage is that the block-level elements can still be displayed as display:block, without adding meaningless tags, without adding nesting depth. The point is to set the position:relative; Here is the example code for the third method, which you can put inside 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>

Three methods are widely used and have disadvantages and advantages. The specific choice of which method is the case. CSS text picture block element horizontally centered method is introduced here.

This article starts in: Finger front

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.