Wrap Text Around Centered Image CSS

Wrap Text Around Centered Image CSS
Code Snippet:wrap text around centered image
Author: Asif Mughal
Published: January 18, 2024
Last Updated: January 22, 2024
Downloads: 861
License: MIT
Edit Code online: View on CodePen
Read More

Are you looking to neatly align text around a centered image on your webpage? This “Wrap Text Around Centered Image CSS” code does exactly that. It utilizes CSS to create a container that centers an image, allowing text to flow around it harmoniously. This method improves the visual presentation of your content.

It’s particularly useful for blog posts, or product descriptions where you want to center images and wrap text around them, improving visual styling and readability.

How to Wrap Text Around Centered Image Using HTML & CSS

1. First, create the HTML structure for your content. You’ll need a container to hold your text and image. Here’s a sample HTML code snippet:

<div class="container">
        <div class="text left">
            <h2>Left text goes here...</h2>
                <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu, massa scelerisque ac, cursus et, sollicitudin a, orci.</p>
        </div>
        <div class="image-container">
            <img src="your-image-url.jpg" alt="Centered Image" class="centered-image">
        </div>
        <div class="text right">
            <h2>Right text goes here...</h2>
           <p>Praesent in mauris eu tortor porttitor accumsan. Duis bibendum. Morbi non erat non ipsum pharetra tempus. Du is iaculis laoreet odio. Vestibulum id ligula porta felis euismod semper. Sed euismod. Maecenas id ante. Nunc pellentesque. Sed vestibulum. Integer in ante. Nunc elementum. </p>
        </div>
    </div>

Replace "your-image-url.jpg" with the URL of your image.

2. Next, let’s apply the necessary CSS styles to achieve the desired effect. We’ll center the image and make sure the text flows around it. Here’s the CSS code:

.container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
}

.text {
    flex: 1;
    padding: 20px;
}

.image-container {
    text-align: center;
}

.image-container img {
    max-width: 320px;
    height: auto;
    display: block;
    margin: 0 auto;
}

p {
    text-align: justify;
}

Feel free to customize the CSS styles to match your website’s design and image dimensions. You can adjust the max-width property to control the maximum image size.

That’s all! hopefully, you have successfully wrapped text around the centered image. This technique is great for blog posts, articles, or product descriptions. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X