Cross-browser inline-block implementation in CSS

Source: Internet
Author: User

When we create a webpage, we sometimes want an element to define its width and height like a block, and to continue to work like a normal inline. For example, the red lines marked in the following figure:


 

We usually use this code to achieve this effect:

 

<A href = "#"> </A>

Although this can achieve the goal, the Code is not refined and flexible enough. The ideal way is to use the following code:

 

<A href = "#"> Yangliu. Name </a>

In this way, we need to specify the width height and background-image for the tag. But the default
The display attribute is inline, and width and height are invalid. If you set
Display: Block, although it can solve the width and height problem, but the element will automatically break the line, unable to achieve the effect we need. Is there any way to implement something similar to IMG?
In this way, the label can set the height and width, and will not automatically cut the line?

The answer is yes. Opera and WebKit support the display: inline-block attribute of css2. Using this attribute, we can achieve the desired effect in opera, but it won't work in other browsers.

The display property also has a less commonly used value display:
Inline-table. Using this value can also completely achieve the desired effect. This attribute is correctly supported by all browsers except IE, ...... Er, again
IE. Although all web developers hate CSS, giving up ie means giving up 70%
So we have to find other solutions.

No, so I had to Google and found the result for me. In this article, it is pointed out that if you first trigger haslayout of IE and then set its
Display: inline. This element will become inline-block! In this way, we can take advantage of the unreasonable features of IE, combined with some
CSS hacks provides CSS code compatible with various browsers:

 

. Element-class {
Display:-moz-inline-stack; // Firefox only code
Display: inline-block; // some standard browsers
Zoom: 1; // ie only
* Display: inline; // only ie know this code (CSS hack)
}

With this code, you can implement consistent inline-block in various browsers. However, this method has a small defect, that is, it cannot pass
W3C CSS verification. Of course, it is very easy to pass the verification. You can issue a style sheet for the inline-Table attribute to browsers other than IE. for IE
Separate the IE-only CSS.

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.