Home:ALL Converter>Issues with IE stretching featured images in Wordpress theme

Issues with IE stretching featured images in Wordpress theme

Ask Time:2016-06-29T10:31:30         Author:Excell

Json Formatter

I have setup a wordpress theme that uses featured images for header images on pages (I did this to make it easy for clients to modify on their own). Because the header image container needs to be a fixed size (100% width of the page and 300px heigh), I am using the "object-fit:cover" css callout which works perfect. The resulting effect is the image spans the full width of the page, and then is cropped vertically automatically (I did this so client would not need to manually size/crop the images before uploading them).

It works perfect with the exception of IE (of course). I have tried numerous possible workarounds from the "backgroundsize.htc" fix to javascripts, to absolute positioning and using the clip css feature, but it still does not give me the desired effect. IE insists on stretching the image. At this stage I am not sure if this is even doable in IE.

Here is my css for the featured image:

.wp-post-image {
width:100%;
height:300px;
position:relative;
display:block;
top:0px;
object-fit:cover;

}

img.wp-post-image {
max-height:300px;
margin:0 auto;

}

This code works in all browsers except IE, so I am using this code to feed IE overrides for the featured images:

@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {

.wp-post-image {}
img.wp-post-image {}

}

Does anyone have advice for me as to how to force IE to "fill" the featured image container with its respective image and crop it instead of stretching it? Any help is really appreciated...

Author:Excell,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38089378/issues-with-ie-stretching-featured-images-in-wordpress-theme
Anthony Astige :

Put the image in a containing div with strategic use of overflow: hidden\n\n\r\n\r\n.wrapper {\r\n height: 100px;\r\n overflow: hidden;\r\n}\r\n<div class=\"wrapper\">\r\n <img src=\"http://sd.keepcalm-o-matic.co.uk/i-w600/keep-calm-and-wrap-it-up-3.jpg\" width=600 height=700>\r\n</div>",
2016-06-29T02:33:56
Excell :

Okay... figured it out. I hope this solution can help someone else in the future and a big THANK YOU to Anthony for his suggestion of the wrapper... something that I thought I had to avoid...\n\nIf you are wanting to implement a featured image in your Wordpress theme that automatically crops the media file a person uploads and not have IE screw it all up, do this:\n\nFor your regular featured image - add this implementation to your wordpress theme (I usually add it in the header.php file):\n\n<div class=\"wrapper\">\n<?php\n// check if the post has a Post Thumbnail assigned to it.\nif ( has_post_thumbnail() ) {\n the_post_thumbnail();\n} \n?>\n</div>\n\n\nThen add this in your css:\n\n.wp-post-image {\nwidth:100%;\nheight:300px;\nposition:relative;\ndisplay:block;\ntop:0px;\nobject-fit:cover;\n}\n\n.wrapper {\nheight:300px;\noverflow:hidden !important;\n}\n\n\nThis will work with all browsers except IE (naturally). IE (even version 11) will skew the image within the featured image div. To fix that, add this code to the bottom of your css:\n\n/*IE HACK*/\n @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {\n img.wp-post-image {\n max-width: 100%;\n height:auto;\n width:auto;\n top:50%;\n left:50%;\n transform: translate(-50%, -50%);\n }\n }\n\n\nAdding the \"max-width\" and the \"auto\" for the regular height and width callouts, is what I was missing before. \n\nThanks to Anthony's suggestion to use a wrapper for the featured image, I was able to fill the wrapper with the image and force IE to not skew it. To make the image center (vertically and horizontally) in IE, I added the \"top,left,transform\" code which centres the large image within the wrapper in IE 11... ",
2016-06-29T03:50:57
yy