Width and Height interfering with sizes attribute
-
I can’t get custom media queries that I set in the “sizes” attribute to take effect on images inserted into posts unless I manually remove the height and width attributes on the image tag, or set height and width to “auto” in CSS.
Ex.
<div class=”one-fourth”>
<?php
$args = array(
‘sizes’ => array(
array(
‘size_value’ => ’25vw’,
‘mq_value’ => ’48em’,
‘mq_name’ => ‘min-width’
),
array(
‘size_value’ => ’20vw’
),
)
);
$sizes = tevkori_get_sizes( get_post_meta( get_the_ID(), ‘about_section_image_id’, 1), ‘medium’, $args );echo wp_get_attachment_image( get_post_meta( get_the_ID(), ‘about_section_image_id’, 1 ), ‘medium’, false, array(‘sizes’ => $sizes) ); ?>
</div>Image tag generated:
<img width=”294″ height=”300″ src=”https://test.dev/wp-content/uploads/2015/05/test-image-294×300.png” class=”attachment-medium” alt=”test” sizes=”(min-width: 48em) 25vw, 20vw” srcset=”https://test.dev/wp-content/uploads/2015/05/test-image-294×300.png 294w, https://test.dev/wp-content/uploads/2015/05/test-image.png 450w”>In this example, I am specifying to only show the image at 20% width of the viewport by default, and then 25% at screen widths of 48ems and up. The image tag generated looks fine, but the image is much bigger than 20% of the viewport at narrower widths, unless I remove the width attribute or set it to “auto” in CSS. Any ideas? Works fine outside of WordPress on a barebones site that I have using Picturefill.
- The topic ‘Width and Height interfering with sizes attribute’ is closed to new replies.