What is the best way to align text next to an image within a div container?

Is there a way to position long text next to an image within a container? I've tried using float left/right, but it didn't work. When I use margin-top, the layout gets messy because the images and text are different heights/lengths.

.containerpost{
  margin-left: 25px;
  width: 40%;
  background: white;
  margin-left: 5px;
  margin-right: auto;
  border-radius: 4px;
  border: 1px solid black;
  padding: 10px;
  margin-top: 20px;
  float: left;
  clear: both;
  overflow: hidden;
  clear: both;
}

.description{
  float: left;
  clear: both;
  word-wrap: break-word;
}
.avatar{
  float: right;
  height: 200px;
  clear: both;
  margin-top: -0%;
}

Answer №1

Utilize flexbox instead of floats for a cleaner layout. Apply display: flex; to the container to make the image and text div sit next to each other. Use align-items: center to ensure they are vertically centered.

Unnecessary styles have been removed but can be added back as needed.

.containerpost {
  background: white;
  border-radius: 4px;
  border: 1px solid black;
  padding: 10px;
  display: flex;
  align-items: center;
}

.avatar {
  height: 200px;
}
<div class="containerpost">
  <div class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed congue dolor id urna laoreet ullamcorper. Vestibulum vel metus quis ipsum blandit tincidunt.</div>
  <img class="avatar" src="https://picsum.photos/200" />
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

HTML Browser Notice

My panel is designed to display different elements based on the browser being used. Specifically, I want different content to appear for IE, Firefox, and all other browsers. I don't want to create unique code for every browser, but rather streamline i ...

Styling with CSS

What is the preferred method of CSS styling in the industry? Option 1: Create classes for each attribute (similar to bootstrap) and then attach the classes in HTML. For example: HTML: <p class="text-white text-bold"> John Doe </p> CSS: .te ...

Analyzing CPU Usage with PHP and Jquery Flot

How can I utilize this to its fullest potential? Is it possible to use a jQuery chart to display CPU usage? I'm considering using jQuery Flot. Is there a way to have the graphics update automatically? I would appreciate any tips you can provide. ...

Styling Radio Buttons and Links in Horizontal Layout Using Bootstrap

Encountering an issue in Bootstrap while attempting to incorporate custom radio buttons and a hyperlink in the same row. It seems that either the hyperlink functions OR the data-toggle feature for radio buttons works, but not both simultaneously, especiall ...

When combining Symfony, Twig, and Semantic UI, users may encounter an issue where the background image fails to display

Can someone assist me in identifying the issue with my code? I am using Symfony 2.8, Semantic UI, and Twig, attempting to set a background image on my site. The modification is being made to my base.html.twig file. I have attempted: Using different ima ...

Bootstrap CSS flexibly decreases the size of the input field on the form

I'm having trouble with Bootstrap-5. When I add the d-flex class to a container, my form inputs and labels shrink. If I remove d-flex, justify-center, and align-items-center from the container class, the form is no longer centered on the page. Adding ...

Tips for properly positioning a bootstrap popover within a grid column

I'm currently utilizing bootstrap 4 alpha 6. Displayed below is a grid consisting of one row and two columns. The first column contains text, while the second column displays a popover: <div class="row text-center align-items-center" style="heigh ...

Adjust the positioning of the content within each card inside the modal box

I require assistance in adjusting the CSS for different cards within the modal box. I would like all cards to have consistent column width for various content within them, creating a symmetrical appearance. Currently, the cards appear staggered. Also, plea ...

Is there a way to track the amount a user scrolls once they have reached the bottom of a page using Javascript?

It's a popular UI pattern on mobile devices to have a draggable element containing a scrollable element. Once the user reaches the end of the scrollable content, further scrolling should transition into dragging the outer element. For example, in this ...

Can one retrieve the CSS/XPath from a Capybara Element?

Is it possible to extract CSS/XPath from a Capybara Element? I have attempted to use #path but it doesn't appear to be effective. Here is the link I tried: The test is being executed on Selenium Webdriver. ...

How to position an empty Div next to images

I am currently working on a project to familiarize myself with Japanese Hiagana. I plan to incorporate more images and eventually sound into the post. To reduce the number of images used, I attempted to replace empty .pngs with a div element. However, I am ...

Displaying AJAX response on a jQuery mobile popup window

I am currently working on an application where I need to append Ajax response to a popup window using jQuery Mobile. I have some code snippets below. The goal is to append the response to the d2 id, which corresponds to a tag. Here is my Ajax code: ...

Looking to implement a condition that prevents the echoing of HTML code when a certain template ID is selected?

Struggling with inherited PHP MVC code for a website, I encounter an issue on a specific page where unwanted code is echoed out. To prevent this, I aim to skip the echoing when a specific templateID (which I set as default) is selected. However, I'm u ...

Expanding Issue with Bootstrap Navigation

Hello! I am experiencing an issue with my navbar. The menu collapses, but it does not expand. I have used an example from Bootstrap and made some tweaks, but for some reason, it still doesn't expand properly. Below is the code that I have been workin ...

Looping through images using JQuery

I'm struggling with an image animation and can't quite figure out how to make it work. <div id="img_loop"> <img src="img/img1.jpg" alt="image1" /> <img src="img/img2.jpg" alt="image2" class="hidden" /> <img src="im ...

Is there a way to align my anchor tag so that my link appears perfectly centered both vertically and horizontally on the page?

Despite my efforts to find a solution, I am stuck with this issue. Here is the code I have been working on: <a href='/Customers' class='centre'>Start</a> I attempted to wrap it in a div tag and also wanted to add a small gr ...

Avoid loading external scripts from third-party sources such as JavaScript libraries, Bing Maps API, and Lighthouse performance tool

Issue: The Bing maps API I incorporated into my assignment is loading unnecessary CSS and scripts, causing a noticeable decrease in my lighthouse scores. Is there a way to prevent third-party code (such as Bing Ads) from loading when fetching the maps for ...

Enhancing the appearance of HTML code within x-template script tags in Sublime Text 3 with syntax highlighting

I recently updated to the latest version of sublime text (Version 3.1.1 Build 3176) and have encountered a problem with syntax highlighting for HTML code inside script tags. Just to provide some context, I'm using x-template scripts to build Vue.js c ...

Exploring the world of JQuery Ajax selectors

I am attempting to create a comment system similar to the one found at . I have the AJAX code below, but I am having trouble uniquely selecting text areas for each post. The issue is that the number of posts can vary, so it's important that each post ...

Having trouble with loading background images in Angular 4?

After researching extensively on stack overflow, I am still struggling to resolve this issue. The main problem I am facing is related to adding a background image to the header tag in my code. Unfortunately, no matter what I try, the background image refu ...