Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to put text next to an image in HTML

Understanding the Basics

Positioning text next to an image in HTML is a common task in web development. It might seem daunting when you're just starting out, but with a bit of practice, it will become second nature. This blog post aims to guide you through the process, step by step. I'll try to keep it as simple as possible, and explain any technical terms (jargons) that come up.

Remember, HTML (HyperText Markup Language) is like the skeleton of a website. It gives structure to the content. We'll be adding an image and text to this skeleton today.

The Simple Way: Using the <img> and <p> Tags

The most straightforward way to put text next to an image is by using the <img> tag for the image and the <p> tag for the paragraph of text. Let's consider this as a basic Lego structure - two blocks that we want to put side by side.

Here's an example:

<img src="myImage.jpg" alt="My Image">
<p>This is my text</p>

In this code, src stands for 'source', which is the location of your image file. alt is the 'alternative' text that will be displayed if the image can't be loaded for some reason. The text within the <p> tags is your paragraph.

However, if you try this in your HTML file, you'll see that the text isn't sitting next to the image, but below it. Why is that?

Understanding Block and Inline Elements

HTML elements are divided into two categories: block-level elements and inline elements. Think of them as different types of Lego blocks. Block-level elements (like our <p> tag) form a block and take up the full width available, with a new line both before and after the element.

Inline elements (like our <img> tag), on the other hand, only take up as much width as necessary and do not force new lines.

So, in our case, the <p> tag is creating a new line after the <img> tag, causing the text to appear below the image.

Positioning Text Next to an Image: The <span> Tag

To get around this, we can use the <span> tag, which is an inline element, just like the <img> tag. Let's swap our <p> tag with the <span> tag and see what happens.

<img src="myImage.jpg" alt="My Image">
<span>This is my text</span>

Voila! The text is now next to the image, just like we wanted.

But what if you want the text and image to be treated as a single unit, a bigger Lego structure perhaps? For that, we have the <div> tag.

Grouping Elements Together: The <div> Tag

The <div> tag is a block-level element that can be used to group other elements together. It's like a big Lego plate that you can attach other Lego blocks to.

  <img src="myImage.jpg" alt="My Image">
  <span>This is my text</span>

By doing this, we're ensuring that our image and text always stick together, no matter where they're moved on the page.

Final Thoughts

And that's it! You've learned how to position text next to an image in HTML. Remember, practice makes perfect. The more you use these tags and understand how they interact, the easier it will be to structure your web pages.

So, keep experimenting, keep learning, and don't be afraid of a little trial and error. Before long, you'll be building web pages with ease. Happy coding!