How to Put Text Next to an Image in HTML: Ultimate Guide 2024

Ashok Chapagai Dec 13, 2023
  1. How to Put Text Next to an Image in HTML Example
  2. How to Put Text Next to an Image in HTML Steps
  3. Faq
  4. Conclusion
How to Put Text Next to an Image in HTML: Ultimate Guide 2024

This article explains the ways to place text beside images by using HTML and CSS.

In HTML, positioning text beside an image inline element is a fundamental aspect of web design. To achieve this, the float property in CSS comes into play.

This straightforward technique allows content to wrap around the image seamlessly. But for a more modern approach, we will also explore the versatility of flexbox.

Mastering these HTML and CSS principles empowers you to craft visually engaging web pages with a professional touch, enhancing user experience and visual appeal.

How to Put Text Next to an Image in HTML Example

Placing text next to an image in HTML is a common element and crucial aspect of web design. It allows for a more dynamic and visually appealing layout, enabling content creators to present information in a structured and engaging manner.

Basic Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Text Next to Image Example</title>
  <style>
    .image-container {
      float: left;
      margin-right: 20px;
    }
  </style>
</head>
<body>

  <div class="image-container">
    <img src="https://loremflickr.com/320/240">
  </div>

  <p>
    This is a sample text!
  </p>

</body>
</html>

Output:

code output - text next to image html basic example

In this example, the image is floated to the left, allowing the text to wrap around it. margin-right provides space between the image and text.

Flexbox Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Text Next to Image Example</title>
  <style>
    .container {
      display: flex;
    }
    .image-container {
      flex: 0 0 30%;
      margin-right: 20px;
    }
  </style>
</head>
<body>

  <div class="container">
    <div class="image-container">
      <img src="https://loremflickr.com/320/240">
    </div>
    
    <p>
      This is a sample text!
    </p>
  </div>

</body>
</html>

Output:

code output - text next to image html flexbox example

In this example, flexbox is used to create a flexible container. The image container has a fixed width, and the text flexes to fill the available space.

How to Put Text Next to an Image in HTML Steps

Basic Example

Below is a detailed guide to put text next to an image in HTML.

Step 1: Basic HTML Structure

Begin by creating an html file with the basic HTML structure with a container for the image and text.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Text Next to Image</title>
 <style>
   .image-container {
	 float: left; 
	 margin-right: 20px;
   }
 </style>
</head>
<body>

Step 2: Insert Image

Add the image within the designated container. Replace https://loremflickr.com/320/240 with the actual path to the image file or URL of your image.

 <div class="image-container">
   <img src="https://loremflickr.com/320/240">
 </div>

Step 3: Add Text

Insert the block of text that you want to appear next to the image.

 <p>
   This is a sample text!
 </p>

Step 4: Close HTML Tags

Complete the HTML document by closing the tags.

</body>
</html>

Step 5: Final Result

Adjust styling, image paths, and text content based on your specific requirements. The result will be a web page where text gracefully aligns next to the image.

code output - text next to image html basic example

Flexbox Example

Here’s a detailed guide on using the flexbox to put text next to an image in HTML:

Step 1: Basic HTML Structure

Begin by creating the basic HTML structure with a container for both the image and text.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Text Next to Image (Flexbox)</title>
 <style>
   .container {
	 display: flex; 
   }
   .image-container {
	 flex: 0 0 30%;
	 margin-right: 20px; 
   }
 </style>
</head>
<body>

Step 2: Insert Image

Add the image within the designated container. Replace https://loremflickr.com/320/240 with the actual path or URL of your image.

 <div class="container">
   <div class="image-container">
	 <img src="https://loremflickr.com/320/240">
   </div>

Step 3: Add Text

Insert the text that you want to appear next to the image.

   <p>
	This is a sample text!
   </p>
 </div>

Step 4: Close HTML Tags

Complete the HTML document by closing the tags.

</body>
</html>

Step 5: Final Result

Adjust styling, image paths, and text content based on your specific requirements. The result will be a web page where text gracefully aligns next to the image using the flexbox layout.

code output - text next to image html flexbox example

Faq

Q1: Why Is It Important to Put Text Next to an Image in HTML?

A1: Placing text next to an image enhances the visual presentation of content on a webpage. It provides context, additional information, and improves the overall aesthetics, making the content more engaging and user-friendly.

Q2: What Is the Traditional Method for Positioning Text Next to an Image in HTML?

A2: The traditional method involves using the CSS float property. By setting the image to float left or right, text can wrap around it, creating a harmonious layout. Additionally, adjusting margins provides spacing between the displayed image and text.

Conclusion

In summary, mastering text-image alignment in HTML is crucial for creating visually appealing web pages. While the traditional use of the float property on elements offers a straightforward approach, modern methods like flexbox provide flexibility and responsiveness, enhancing user experience.

Whether choosing classic or contemporary techniques, maintaining a balance between text and image is key for an engaging design. Continuously explore and adapt strategies to align with evolving web design standards.

To further enhance your HTML and CSS skills, consider exploring additional layout techniques, such as HTML column layouts.

Ashok Chapagai avatar Ashok Chapagai avatar

Ashok is an avid learner and senior software engineer with a keen interest in cyber security. He loves articulating his experience with words to wider audience.

LinkedIn GitHub