Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE CSS display: inline-block Rendering issue

I'm having an annoying rendering issue with IE my code is

CSS :

div {
    display: inline-block;
    margin-right:40px;
    border: 1px solid;
}

HTML :

<div>element</div>
<div>element</div>
<div>element</div>
<div>element</div>
<div>element</div>

This is how it looks in firefox/chrome (the expected display)

enter image description here

This is how it looks in IE

enter image description here

I googled if IE supports display: inline-block, and apparently it does.

like image 318
AlanFoster Avatar asked Jan 01 '12 05:01

AlanFoster


People also ask

What is the deal with display inline-block?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

How do you prevent inline-block elements from wrapping?

Add white-space: nowrap; to your . layout style declaration. This will do exactly what you need: preventing the divs from wrapping. Save this answer.

Can default display value with CSS from block to inline?

We can change the default value of how will the HTML element be rendered using the CSS Display property. NOTE − The <div> element has a default CSS display set to block and <span> element has a default CSS display set to inline.

What is the difference between display block and display inline?

Compared to display: inline , the major difference is that inline-block allows to set a width and height on the element. Also, with display: inline , top and bottom margins & paddings are not respected, and with display: inline-block they are.


2 Answers

Working Solution

The following appears to work correctly in:

  • Quirks mode
  • IE 7 Standards
  • IE 8 Standards
  • IE 9 Standards
  • IE 9 Compatibility Mode

<!DOCTYPE html>
<html>
<head>
    <style>

    DIV {
        display: inline-block;
        *display: inline; /* leading asterisk IS correct */
        margin-right:40px;
        border: 1px solid;
        zoom: 1; /* seems to fix drawing bug on border in IE 7 */
    }

    </style>

</head>
<body>
    <div>element</div>
    <div>element</div>
    <div>element</div>
    <div>element</div>
    <div>element</div>
</body>
</html>

Answer History

http://jsfiddle.net/3sK4S/

Works fine for me in IE9 Standards Mode. Does not display correctly (as you described) in quirks mode.

Testing with IE9:

  • Quirks mode: block (incorrect)
  • IE 7 Standards: block (incorrect)
  • IE 8 Standards: inline (correct)
  • IE 9 Standards: inline (correct)
  • IE 9 Compatibility Mode: inline (correct)

To trick IE7:

div {
    display: inline-block;
    *display: inline; /* leading asterisk IS correct */
    margin-right:40px;
    border: 1px solid;
}

Taken from this article. Works for me in IE 7 emulation mode.

Per @SKS comment about doctype, I have added a complete solution with a doctype specified.

like image 88
Tim M. Avatar answered Oct 06 '22 19:10

Tim M.


Add DOCTYPE to your html,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

It works for me after I added this.

Note: in jsFiddle, DOCTYPE was automatically generated so it will work there.

Edit 1: Check this for more information,

Edit 2: You can read more about inline-block styling here

like image 40
Selvakumar Arumugam Avatar answered Oct 06 '22 21:10

Selvakumar Arumugam