English 中文(简体)
Anchor tag around fluid image in Internet Explorer 8
原标题:

I m having some trouble with a fluid layout (with images using max-width:100%) in Internet Explorer 8. The problem comes when I want to wrap an anchor tag around a flexible-width image and margins, padding, and borders get involved. This is a little harder than you d think.

So the HTML is roughly this:

<div>
    <a class="image-wrap" href="#">
        <img alt="something" src="/path_to_image.jpg">
    </a>
</div>

And the styling:

img { 
    max-width: 100%;
    display: block;    
}

.image-wrap {
    padding: 5px;
    border: 1px solid black;
    display: block;
}

So what I have is a fluid image, surrounded by an anchor with a padding and border.

The above is good unless the image is smaller than the container. In that case, the anchor s padding and border expand beyond the image (filling the full width of the container).

So let s try something to force the anchor to fit its content:

.image-wrap {
    padding: 5px;
    border: 1px solid black;
    display: inline-block;
}

OK, now the anchor box doesn t grow larger than the image, but the anchor tag is back to being not flexible in Firefox, Opera, and IE8+. But it does trigger layout in IE7 so that s taken care of. Next up:

.image-wrap {
    padding: 5px;
    border: 1px solid black;
    display: inline-block;
    max-width: 100%;
}

Now the anchor is flexible again, but it s 12px larger than the container when the image starts scaling down. The anchor is calculating the width based on the image, then adding the border and padding. For some reason, Chrome and Safari don t do this. Anyway, alternate box model to the rescue:

.image-wrap {
    padding: 5px;
    border: 1px solid black;
    display: inline-block;
    max-width: 100%;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

Now it s working as expected in Firefox, Chrome, Safari, Opera, IE7, IE9, but not IE8 for some reason. There, the anchor still extends outside the container. I m a little stumped as to what to do, as box-sizing: border-box is supposed to be supported in IE8.

I ve got a JSFiddle page that isolates the issue http://jsfiddle.net/3gkXU/22/

As a last resort I could use JavaScript to detect if the anchor is bigger than its parent element, and then switch it to display: block on the assumption that people are unlikely to resize their browser. I m not against that, but is there a CSS solution here?

最佳回答

It appears that IE8 respects box-sizing: border-box for width, but not for max-width. Solution: Forget about box-sizing and insert an extra element.

jsFiddle: http://jsfiddle.net/wdKGU/1/

The new HTML:

<div>
    <a class="image-wrap" href="#">
        <span>
            <img alt="something" src="/path_to_image.jpg">
        </span>
    </a>
</div>

The new styling:

img {
    max-width: 100% !important;
    display: block;
}

.image-wrap {
    display: inline-block;
    max-width: 100%;
}

.image-wrap span {
    padding: 5px;
    border: 1px solid black;
    display: block;
}
问题回答

暂无回答




相关问题
Image rendered with wrong colors in IE8

I have a gradient image as a PNG (no transparency) in a web-page. By taking a screenshot of Chrome showing the page and cropping the image from the page, I see the exact same colors are drawn as the ...

Determine Mobile Internet Explorer version

I need to determine the version of Mobile Internet Explorer on a Windows Mobile 6.1 device. So that I can report the same user-agent string as is being used by Mobile Internet Explorer. The user-...

CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

Internet Explorer only part loading JavaScript/CSS

I m having trouble with my local development environment where IE (6 through to 8) is only part loading JavaScript/CSS files. It throws random errors at random places in jquery.min.js every time I ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签