Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

css - box-sizing support in IE7

I just discovered the box-sizing: border-box CSS property which solves a bunch of cross browser layout problems for me.

The only issue I now have is that IE7 doesn't seem to support it. Is there a hack to get IE7 to support it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several ways to do this, none perfect.

As you point out:

  • Firefox / Opera / Safari / Chrome / IE8+ will recognise the box-sizing property allowing you to use border-boxes.
  • IE6 will use the old school (correct?) border-box model by default.
  • However IE7 uses the W3C padding box model when in standards mode, and will not recognise the CSS box-sizing property so there's no way to revert to the border box model. If you need to support IE7 (and you probably still do), you're stuck with one of four options:

1. Conditional Comments:

<!--[if IE 7]>
  Special instructions for IE 7 here
<![endif]-->

Use box-sizing for IE8 and 9, then make specific overrides for IE7. This option will be painful.

2. The Schepp Box Sizing Polyfill:

https://github.com/Schepp/box-sizing-polyfill

This excellent Polyfill is an HTC file which modifies the default browser behavior in IE6 and 7 so they use the W3C box model. It's fine for light use, but may cause problems of it's own if used extensively. Use with caution and TEST.

3. Old Style Nested Divs:

The old style nested div approach is still a fine way:

<div style="width:100px; border:1px solid black">
  <div style="margin:10px">
    Content
  </div>
</div>

A non-semantic nested div provides the padding indirectly, with the disadvantage that your markup becomes untidy. Obviously don't use inline styles, I'm using them here for the sake of illustration.

The old adage Never use padding on a fixed width element still stands true.

4. My Preferred Solution - A Direct Child Selector:

The other way round this is with the direct child selector. Say you have a fixed width div containing some content:

<div class="content">
  <h1>Hi</h1>
  <p>hello <em>there</em></p>
</div>

You can then write a rule to add left and right margins to all the direct children of the div:

.content {
  width:500px;
  padding:20px 0;
}
.content > * {
  margin:0 20px;
}

This will add a little margin to the h1 and p, but not to the nested em, giving the appearance of 20px padding on the content div, but without triggering the box model bug.

5. Consider Dropping IE7 support

IE7 is the last browser not to recognise the box-sizing property. If you're getting little traffic from IE7, you might consider dropping support. Your CSS will be much nicer.

As of late 2013, this is my preferred option.


2017 EDIT: It's probably long past time to drop support for IE7 now, and just use border-box.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...