Home  >  Article  >  Web Front-end  >  How to Float an Image to the Left of Text in CSS?

How to Float an Image to the Left of Text in CSS?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-11 06:30:03133browse

How to Float an Image to the Left of Text in CSS?

Floating an Image to the Left of the Text with CSS

Problem

You're having difficulty floating an image to the left of text in an HTML element, while keeping the text on the right and preventing the image from wrapping around it.

Current Implementation

Here's your HTML code:

<div class="post-container">                
   <div class="post-thumb"><img src="thumb.jpg" /></div>
   <div class="post-title">Post title</div>
   <div class="post-content"><p>post description description description etc etc etc</p></div>
</div>

And your CSS code:

.post-container{
    margin: 20px 20px 0 0;  
    border:5px solid #333;
}

.post-thumb img {
    float: left;
    clear:left;
}

.post-content {
    float:right;
}

Solution

To achieve the desired result, you can make the following modifications:

HTML:

<div class="post-container">                
    <div class="post-thumb"><img src="http://dummyimage.com/200x200/f0f/fff" /></div>
    <div class="post-content">
        <h3 class="post-title">Post title</h3>
        <p>post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc post desc </p>
   </div>
</div>

CSS:

.post-container {
    margin: 20px 20px 0 0;  
    border: 5px solid #333;
    overflow: auto
}
.post-thumb {
    float: left
}
.post-thumb img {
    display: block
}
.post-content {
    margin-left: 210px
}
.post-title {
    font-weight: bold;
    font-size: 200%
}

Explanation

  • The overflow: auto property on the .post-container allows the text to flow around the image, preventing it from wrapping.
  • The display: block property on the image ensures that it takes up the full width of its container.
  • The margin-left property on the .post-content provides enough space for the image to float on the left.

The above is the detailed content of How to Float an Image to the Left of Text in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn