Handling Responsive Images from Editors Without Breaking Aspect Ratios
Images created with rich-text editors like TinyMCE often include width and height attributes. These values are useful because they describe how the image should appear. However, many layouts use global CSS rules like width: 100%, which silently override those attributes. The result is stretched images or broken proportions.
A better approach is to let both systems work together:
- If an image has a defined width, respect it.
- If it does not, make it responsive.
- Always keep the original aspect ratio.
The key idea is simple. CSS should only force full width on images that do not define their own size. At the same time, height should never be fixed. Let the browser calculate it automatically.
This approach is safe, predictable, and works well for content feeds, articles, and mixed image sizes.
Example HTML
Example CSS
Why this works
- Images with
widthandheightkeep their intended size - Images without dimensions become responsive
height: autopreserves the natural aspect ratio- No cropping or distortion occurs
Avoid using object-fit: cover for normal content images, as it may cut off important parts. This solution keeps layouts clean, readable, and friendly for all screen sizes.
Comments