Flexbox vs Float: Wrap Text Around Images
In this article
Wrapping text around an image is a classic web layout task. The old-school way uses the float property, which still works everywhere. The modern way uses Flexbox, which gives you more control over spacing and alignment without clearing floats. This guide walks you through both methods step by step, so you can choose the right one for your project.
What You'll Need
- A text editor (VS Code, Sublime Text, or any code editor)
- A web browser (Chrome, Firefox, Safari, or Edge)
- Basic HTML and CSS knowledge (you should know what tags and selectors are)
- An image file (any format, e.g., JPG, PNG, or SVG)
Step-by-Step
Method 1: Using Float
- Set up your HTML structure. Create a container
<div>that holds both the image and the text. For example:<div class="wrap-float"> <img src="photo.jpg" alt="A descriptive caption"> <p>Your text goes here.</p> </div>
- Apply the float to the image. In your CSS, target the image inside that container and set
float: left;(orrightif you want the image on the right). Add some margin so the text doesn't touch the image..wrap-float img { float: left; margin-right: 15px; width: 200px; } - Add a clearfix to the container. Because the image is floated, the container's height will collapse, and any following content might overlap. Add a clearfix to the container. The simplest way is to add
overflow: auto;to the container, or use the classic clearfix class:.wrap-float::after { content: ""; display: table; clear: both; } - Adjust as needed. You can control the image size, margin, and even add a background or border to the container. The text will flow around the image naturally.
Method 2: Using Flexbox
- Set up the same HTML structure. Use a container with the image and text, but give it a class like
wrap-flex:<div class="wrap-flex"> <img src="photo.jpg" alt="A descriptive caption"> <p>Your text goes here.</p> </div>
- Make the container a flex container. In CSS, set
display: flex;on the container. This puts the image and text side by side by default..wrap-flex { display: flex; align-items: flex-start; /* Aligns items to the top */ } - Add spacing and control the image size. Use
marginon the image to create space between it and the text. You can also set a fixed width or useflex-shrinkto prevent the image from squishing..wrap-flex img { margin-right: 15px; width: 200px; flex-shrink: 0; /* Prevents the image from shrinking */ } - Understand the difference. With Flexbox, the text does not wrap around the image in a traditional senseāit sits in a column next to it. If you want the text to flow around the image like in a magazine, Flexbox alone won't do that. For true wrapping, you'd need to use
floator CSS shapes. Flexbox is better for aligning the image and text block in a row, especially when you want them to have equal heights or align vertically.
Common Mistakes
- Forgetting to clear floats. If you don't add a clearfix, the parent container will collapse, and elements after it may move up and overlap the image.
- Using Flexbox for true text wrap. Flexbox does not make text wrap around an image; it places them in a row. If you need text to flow around the image, use
floator CSSshape-outside. - Not setting a width on the image. If you don't set a width, the image may take up too much space, especially in a flex container, and push the text to a narrow column.
- Ignoring responsive design. On small screens, a floated image with a fixed width might leave too little space for text. Consider using media queries to adjust the image size or switch to a stacked layout.
Tips
- Use
shape-outsidefor fancy wrapping. If you want text to wrap around a circular or irregularly shaped image, useshape-outside: circle(50%);along withfloat. This works in modern browsers. - Combine float and flexbox. You can use a flex container for the overall layout and then float an image inside one of the flex items if you need text to wrap around it.
- Always add
alttext to images. This improves accessibility and helps with SEO. - Use relative units for image width. Instead of fixed pixels, consider using percentages or
max-width: 100%;to make the layout more fluid. - Test in multiple browsers. While float and flexbox are well-supported, older browsers may have quirks. Always test in Chrome, Firefox, and Safari.
Troubleshooting
- The container collapses (height becomes zero). This is the classic float issue. Add a clearfix to the container:
overflow: auto;or the::afterpseudo-element method. - Text is too close to the image. Increase the margin on the image. For floated images, use
margin-right(if floated left) ormargin-left(if floated right). For flexbox, usemargin-righton the image orgapon the container (if supported). - The image pushes the text to the next line. This can happen if the image is too wide. Reduce the image width, or in flexbox, set
flex-shrink: 0;and allow the text to wrap by not settingwhite-space: nowrap;. - Flexbox items are not aligning at the top. By default, flex items stretch to fill the container height. Use
align-items: flex-start;to align them to the top. - Text wraps under the image instead of around it. This usually happens with floats when the image is too large relative to the container. Make sure the image width is less than the container width, and consider using
shape-outsideif you need a specific shape.
Frequently Asked Questions
Can I use Flexbox to wrap text around an image like a magazine?
No, Flexbox does not support text wrapping around an image. It places the image and text in a row, with the text in a separate column. For true text wrapping, use float or CSS shape-outside.
What is the clearfix hack and why do I need it?
The clearfix hack is a CSS technique that forces a parent container to expand to include its floated children. Without it, the container's height collapses, causing layout problems. The most common method is adding overflow: auto; or using the ::after pseudo-element with clear: both;.
Is float deprecated in HTML5?
No, float is not deprecated. It is still a valid CSS property and is widely used for wrapping text around images. However, for overall page layout, Flexbox and Grid are now preferred because they offer more control and are easier to maintain.
How do I make the image responsive in a flex container?
Set the image's max-width: 100%; and height: auto; to ensure it scales down on smaller screens. You can also use flex-shrink: 1; (default) to allow the image to shrink, but be careful it doesn't become too small.
Can I use both float and flexbox together?
Yes, you can. For example, you can have a flex container for the overall layout, and inside one of the flex items, you can float an image to wrap text around it. This is a common pattern for cards or article previews.
What is the best way to center an image with text around it?
If you want the image centered and text wrapping on both sides, you can use float: left; and float: right; on two separate elements, but this is tricky. A simpler approach is to use CSS columns or a three-column flex layout with the image in the middle and text on both sides. For a single image with text on both sides, you might need to use shape-outside with a polygon.