x
Black Bar Banner 1
x

NEW: About Me Look — turn on a full-width dark About Me from your profile Configuration (Profile Page Upgrade). The Academy, profile feeds and Media Feed now read like the News Feed. See Subscriptions »

New: AI Blog Autopilot — free for the next 30 days. Switch it on by November 15th »

NEW: Smart Mail is live — free for every member. Send broadcasts, build lists and run autoresponders. Open Smart Mail »

New training: The Campaign Map. Watch it now »

A+ A−
Ads by Markethive - View All
The Blog Feed
The Blog Feed
» Blog Feed
Write a New Blog Post
Search Blog Status
Most Viewed
Most Recent
Most Shared
Alphabetical
Markethive Blog (default)
My Blog Posts
Friends' Blogs
Blog Categories
All
Advertising
Blockchain & Cryptocurrency
Business Development
Diet & Weight Loss
Environmental
Health and Wellness
History and Culture
Home and Garden
Marketing
Mentoring & Training
Money & Finance
Other
Political
Prayer & Religion
Programming & Technical
Real Estate
Search Engine Optimization
Social Media
Spirituality
Sports & Recreation
Transport
Travel & Events
Website Design
Blogging Tools & Assets
My Blog Info
Members Subscribed to You
Blogs You Are Subscribed To
Website Widget
Wordpress Plugin
Subscribe for Greater Services
Subscribe to one of many subscriptions, each one includes the previous ones.. Unlock powerful tools, advance features, to build a powerful reach.

How to Float an Image to the Left of Text on a Webpage

Posted by Corneliu Boghian on August 01, 2021 - 8:29am


Science, Tech, Math › Computer Science

How to Float an Image to the Left of Text on a Webpage

Use CSS to place your images with precision

By Jennifer Kyrnin

 

Updated  2021

Block-level elements in a web page appear in sequential order. To improve the page's appearance or usefulness, you can modify that order by wrapping blocks, including images, so that text flows around images.

In web design terms, this effect is known as floating the image. This is achieved with the CSS property float, which allows text to flow around the left-aligned image to its right side (or around a right-aligned image to its left side).

female web developer working on computer

Maskot/Getty Images

Start With HTML

This example adds an image at the beginning of a paragraph (before the text, but after the opening

tag). Here's the initial HTML markup:


 

The text of the paragraph goes here. In this example, we have an image of a headshot photo, so this text might describe the person in the headshot.

 

By default, the page would display with the image above the text, because images are block-level elements in HTML. This means that the browser displays line breaks before and after the image element by default. To change this default look using CSS, add a class value (left) to the image element to serve as a hook to which properties can be attached.


 

The text of the paragraph goes here. In this example, we have an image of a headshot photo, so this text might describe the person in the headshot.

 

Note that this class does nothing on its own. CSS will achieve the desired style.

Adding CSS Styles

Add this rule to the site's stylesheet:

.left {
 float: left;
 padding: 0 20px 20px 0;
}


This style floats anything with the class left to the left of the page and adds a little padding to the right and bottom of the image so that the text doesn't butt right up against it.

In a browser, the image would now be aligned to the left; the text would appear to its right with space between the two.

The class value .left used here is arbitrary. You can call it anything you choose because it does nothing on its own. However, you should also not that any value you change in the CSS should also be reflected in the HTML.

Other Ways to Achieve These Styles

You could also take the class value off of the image and style it with CSS by writing a more specific selector. In the example below, the image is inside of a division with a main-content class value.


 

 

The text of the paragraph goes here. In this example, we have an image of a headshot photo, so this text might describe the person in the headshot.


 

To style this image, write this CSS:

.main-content img { 
 float: left; 
 padding: 0 20px 20px 0;
}


In this scenario, the image is aligned to the left, with the text floating around it as before, but without the extra class value in the markup. Doing this at scale can help create a smaller HTML file, which will be easier to manage and can improve performance.

Avoid Inline Styles

Finally, you could use inline styles:


 

The text of the paragraph goes here. In this example, we have an image of a headshot photo, so this text might describe the person in the headshot.

 

This is not advisable, however, because it combines the style of an element with its structural markup. Best practices dictate that the style and structure of a page remain separate. This segregation is especially helpful when you need to change the page's layout and look for different screen sizes and devices with a responsive website.

Intertwining the style of the page with the HTML makes authoring media queries to adjust your site for different screens much more difficult.

Corneliu Boghian thanks for comment
August 2, 2021 at 7:07am
Bill Rippel If a person knows HTML that looks pretty simple. Thanks for sharing, Corneliu.
August 1, 2021 at 2:52pm
Mihai Cristian Thanks for sharing
August 1, 2021 at 2:40pm