@nikitonsky The best way would be to set dimensions in markup for each image:
<img width height>
It helps browsers to render responsive images knowing their ratios even before they’re loaded: no jumping, better rendering performance.
As for CSS:
img { max-width: 100%; height: auto }
Unlike your solution, this one won’t limit you to a certain aspect ratio set in CSS.
@pepelsbey nice! it works, too