For example, to create an adaptive image, use the method below.

  1. Img is a line element, how can the height property be applied to it?
  2. If it is valid, and auto is the default value, then what is it for?

.container { width: 800px; max-width: 90%; } img { max-width: 100%; height: auto; } 

  • one
    for sure - webDev_
  • And why not just apply background-size with the same cover or contain . And there will be no hassle. with width and height. - And

1 answer 1

There are no block and inline elements in html for a long time. Here is a good article on this topic.

To understand how ridiculous it is to consider img as a string element, let's recall that according to the ancient standard, the string elements do not have width. Where is the logic at all?

In the code you cited, the width of the image is set to 100% of the parent block, and the height indication in auto is needed in order to preserve its aspect ratio when the width is changed.