Example of using Inline-block to center in a detailed CSS

Source: Internet
Author: User
This article focuses on the use of CSS in the Inline-block to the center of the example, use the time to pay attention to the width of the container, the need for friends can refer to the next

Urgently needed method: The Inline-block method is centered. The basic method is to use Display:inline-block, vertical-align:middle styles and pseudo elements to center the content block in the container. My implementation uses a few new tricks that I can't see anywhere else to solve some of the problems.

The width of the content area declaration cannot be greater than the width of the container's 100% minus 0.25em. It's like an area with long text. Otherwise, the content area is pushed to the top, which is why you use: after Pseudo-class. Use: Before pseudo-class will make the element have 100% size!

If the content block needs to occupy as much horizontal space as possible, you can add max-width:99% to the large container, style, or use the Max-width:calc (100%–0.25EM) style with the browser and container width in mind.

This approach is the same as most of the Table-cell benefits, but at first I gave up this method because it was more like a hack. In spite of this, browser support is good and proven to be a popular method.

Html:

<p class= "Center-container is-inline" >  <p class= "Center-block" >    <!--CONTENT--  < /p></p>

Css:

.     center-container.is-inline {text-align:center; Overflow: <span style= "Width:auto; Height:auto; Float:none; "id=" 7_nwp "><a style=" Text-decoration:none "mpid=" 7 "target=" _blank "href=" http://cpro.baidu.com /cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0& jk=2d6719abf210fdd5&k=auto&k0=auto&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr& rb=0&rs=1&seller_id=1&sid=d5fd10f2ab19672d&ssp2=1&stid=0&t=tpclicked3_hc&tu= U1922429&u=http%3a%2f%2fwww%2eadmin10000%2ecom%2fdocument%2f3058%2ehtml&urlid=0 "id=" 7_NWL "><span style= "color: #0000ff; font-size:14px;width:auto;height:auto;float:none;"   >auto</span></a></span>; }   . Center-container.is-inline:after,. Is-inline. Center-block {display:inline-<span style= "width:auto; height:auto; float:none;" Id= "8_nwp" ><a style= "Tex T-decoration:none, "mpid=" 8 "target=" _blaNK "Href=" http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di= 128&fv=0&is_app=0&jk=2d6719abf210fdd5&k=block&k0=block&kdi0=0&luki=7&n=10& P=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=d5fd10f2ab19672d&ssp2=1&stid=0 &t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3058%2Ehtml& Urlid=0 "id=" 8_NWL "><span style=" color: #0000ff; font-size:14px;width:auto;height:auto;float:none; "     >block</span></a></span>;   Vertical-align:middle; }   .     Center-container.is-inline:after {content: ';     height:100%; Margin-left: -0.25em; /* to offset spacing. May vary by font */}. Is-inline. Center-block {max-width:99%;/* Prevents issues with long content causes the content Block to being pushed to the top * //* MAX-WIDTH:CALC (100%-0.25em)/* Only for ie9+ */}

Benefits:

Highly variable content
Content overflow can automatically open the parent element height
Browser compatibility is good, can even adjust support IE7

Also note:

Additional containers required
Dependent on Margin-left: -0.25em style, do horizontal centering, need to adjust for different font size
The width of the content area declaration cannot be greater than the width of the container 100% minus 0.25em

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.