On my website I have images that have a width of 720 pixels. I want images to be either 95% of the width of the page (max-width: 95%), or 720px if the page width is too large. So, on larger screens, I don't want to blow them up. Is there a way to achieve this in CSS?
css: max-width or max-size
121 views Asked by xpoes At
2
You can specify a
width
and amax-width
. The element will respect the width, or shrink if max-width is smaller.This behavior is described on the MDN.
Note as indicated in the other answer, you could set
width
to a percentage andmax-width
to pixels. The end result is the same: the calculated width of the image becomes the smallest of the two.To me, specifying the width in pixels makes more sense semantically. After all, you want to have an image of so many pixels wide (related to the image itself), and want to have a constraint on the percentage for smaller screens. But if you write it the other way around, I won't hold it against you. ;-)