Mobile app version of vmapp.org
Login or Join
Dunderdale272

: Responsive images require excluding the width/height attributes? One of the tenets of responsive web design is responsive images: sizing images using percentages rather than concrete values. This

@Dunderdale272

Posted in: #Images #ResponsiveWebdesign

One of the tenets of responsive web design is responsive images: sizing images using percentages rather than concrete values. This is done via CSS.

The problem I have is the standard practice of including the width and height attributes on the img tag. This seems to be precluded by setting image sizes in CSS. For one, it's long been required to include the dimension attributes in the image tag. Also, these values were used to create space in the page before the image loaded.

As far as the sizing goes, I suppose it's possible to put the image into another element (e.g. figure) and set the image's width in CSS to be 100%. But that then requires sizing the container, possibly as a percentage of the parent element, and so on.

Am I not thinking straight on this, or is setting image size in CSS at odds with using the width and height attributes?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Dunderdale272

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

The width and height attributes of the img element are not required under any DOCTYPE, if that is what were implying. There is no difference between Strict, Transitional and HTML5 in this respect.

As you suggest, these attributes were only 'required' to reserve the space on the page and prevent the page moving around as it loads - which is important. This can be achieved using CSS instead providing the CSS loads quickly enough - it is likely to load before the images anyway, so all should be good.

It is also possible (and valid) to specify just one attribute, width or height and the browser will calculate the omitted value in order to maintain the correct aspect ratio.

You can specify percent values in the attributes if required. You don't need to use CSS for this, if that is what you are implying. EDIT: However, I believe this has changed slightly under HTML5. Under HTML5 the width and height can only take a pixel value, in other words a valid non-negative integer.

Whether you use the width and height attributes can depend on your design. If you have lots of differently sized images, do you want to lump all the dimensions in the CSS or include them with the img?

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme