style="float: right"
image in a narrow column
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id aliquam justo, ac tristique leo. Integer venenatis, quam sit amet mollis rhoncus, mi libero tempor elit, et sodales purus erat sed velit. Vivamus nibh orci, vestibulum rhoncus viverra vitae, faucibus id est. Praesent pellentesque, augue eu imperdiet eleifend, augue tellus vehicula lorem, et eleifend metus augue id mauris. Proin sed sollicitudin turpis.
style="float: right"
image, but with CSS magic protecting it
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id aliquam justo, ac tristique leo. Integer venenatis, quam sit amet mollis rhoncus, mi libero tempor elit, et sodales purus erat sed velit. Vivamus nibh orci, vestibulum rhoncus viverra vitae, faucibus id est. Praesent pellentesque, augue eu imperdiet eleifend, augue tellus vehicula lorem, et eleifend metus augue id mauris. Proin sed sollicitudin turpis.
align="right"
attribute instead
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id aliquam justo, ac tristique leo. Integer venenatis, quam sit amet mollis rhoncus, mi libero tempor elit, et sodales purus erat sed velit. Vivamus nibh orci, vestibulum rhoncus viverra vitae, faucibus id est. Praesent pellentesque, augue eu imperdiet eleifend, augue tellus vehicula lorem, et eleifend metus augue id mauris. Proin sed sollicitudin turpis.
So your website's users can upload images into a CMS rich text editor like TinyMCE (or similar). That's cool.
Until it's not.
Giving that much control has downsides - not all users can steer the controls well enough to get a resulting design that doesn't suck. That's especially true in the multi-screen world, where a floated image can look great on a laptop, but squeeze the text horribly on a mobile. So how can we as web developers help protect against floated image problems like this, without taking away toys from users?
This CSS preserves your sanity and the layout of your site, without taking away too much control from your users. Win-win!
align="right"
style=" ... float:right ..."
style=" ... float: right ..."
9em
wide column of text around the floated image, preventing cramped flows.:
max-width: calc(100% - 30px - 9em);
- remember the 100% is the width of the container including padding (in most modern box models), so it's actually (100% container width) minus (padding-left + padding-right) minus (reserved text width)
min-width: 50px;
img[align='right'] {margin-left: 15px;}
margin-bottom: 8px;
max-width: 100%;
, and because we may hijack the image width, we need to stop any predetermined height squishing the image out of it's natural aspect ratio
height: auto;