Home > Web Front-end > CSS Tutorial > 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
Release: 2024-11-11 06:30:03
Original
200 people have browsed it

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>
Copy after login

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;
}
Copy after login

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>
Copy after login

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%
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template