Center the image to always align with the text

I am looking to display an image alongside some text, where the number of text lines can vary. It could be one, two, or three lines of text. Here's how it should be handled:

If there is only one line of text:

The image should be centered with the text

If there are two lines of text:

The image should be centered between the two lines

If there are three lines of text:

The image should be centered, possibly aligning with the middle line

Important: The text container must have a specified height requirement.

Here is the CSS I have so far:

.icon-several-lines {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    vertical-align: middle;
}

.info {
     display: inline-block;
     vertical-align: middle;
     height: 120px;
}

HTML:

<div class="icon-several-lines">
    <img class="image" src="http://lorempixum.com/25/25/abstract" alt="" />
    <span class="info">
     <p><span>test test test test test test</span></p>
     <p><span>test test test test test test</span></p>
     <p><span>test test test test test test</span></p>
    </span>
</div>

<div class="icon-several-lines">
    <img class="image" src="http://lorempixum.com/25/25/abstract" alt="" />
    <span class="info">
     <p><span>test test test test test test</span></p>
     <p><span>test test test test test test</span></p>
    </span>
</div>

<div class="icon-several-lines">
    <img class="image" src="http://lorempixum.com/25/25/abstract" alt="" />
    <span class="info">
     <p><span>test test test test test test</span></p>

    </span>
</div>

View the fiddle here: http://jsfiddle.net/2bNsC/726/

Thank you.

Answer №1

To easily align content vertically, consider using a table layout. Your current markup is already set up well for this, so minimal changes are needed.

img { display: inline-block; vertical-align: middle; }
.info { display: inline-table; vertical-align: middle; height: 120px; }
.info > p { display: table-row; vertical-align: middle; }
.info > p > span { display: table-cell; vertical-align: middle; }

Check out the updated Fiddle here: http://jsfiddle.net/abhitalks/2bNsC/727/

Here's a snippet from the code:

.icon-several-lines {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    vertical-align: middle;
}
img { display: inline-block; vertical-align: middle; }
.info {
    display: inline-table;
    vertical-align: middle;
    height: 120px;
    border: 1px solid #ddd;
}
.info > p { display: table-row; vertical-align: middle;}
.info > p > span { display: table-cell; vertical-align: middle;}
<div class="icon-several-lines">
    <img class="image" src="http://lorempixum.com/25/25/abstract" alt="" />
    <span class="info">
     <p><span>test test test test test test</span></p>
     <p><span>test test test test test test</span></p>
     <p><span>test test test test test test</span></p>
    </span>
</div>

<div class="icon-several-lines">
    <img class="image" src="http://lorempixum.com/25/25/abstract" alt="" />
    <span class="info">
     <p><span>test test test test test test</span></p>
     <p><span>test test test test test test</span></p>
    </span>
</div>

<div class="icon-several-lines">
    <img class="image" src="http://lorempixum.com/25/25/abstract" alt="" />
    <span class="info">
     <p><span>test test test test test test</span></p>
     
    </span>
</div>

Answer №2

If you're looking for a straightforward and efficient solution, consider using flexbox:

Styling with CSS

.container {
    display: flex;
    align-items: center;
}
.title {
    flex: 1 0 auto;
}

Check out the demo here

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

Is it possible to sanitize user input without relying on !important?

Utilizing wp_editor, I have implemented a front-end form on my WordPress website for users to post without accessing the admin section. Although it is functional, there is a concern that users may copy and paste content with inline styles that could disrup ...

"Is there a way to extract text enclosed within HTML tags, such as <div>text</div>, with Selenium using C#

How can I extract the text 'True' from within the HTML tags <div>text</div> using Selenium and C#? <div type='data' id='OfferInCart' style='display:none;'>True</div> I have attempted to retr ...

What is the best way to run a function after 10 seconds of inactivity using jquery?

Can anyone help me figure out how to run a function every 10 seconds when the system is idle? Here's an example: setInterval(function () { test(); },10000); //for every 10 Sec I really need assistance in getting this function to ...

The text next to images is being clipped (is it CSS?)

Encountering a problem with CSS on my Wordpress websites. Using WP's editor to float images to the right or left of text, I noticed an issue when viewed on mobile: https://i.sstatic.net/gVFRq.jpg The text gets cut off. How can I ensure that no tex ...

Steps to conceal a <select> element and reveal it by clicking a button

I'm currently working with Bootstrap 3 and I am attempting to replicate jQueryMobile's select option feature. This would involve replacing the standard select textbar and arrows with a single button that can toggle the opening of the select. Ins ...

Steps to center the search form in the navbar and make it expand in Bootstrap 5

I'm utilizing Bootstrap v5.1.3 and have included a navbar as shown below: navbar Here is the code for that implementation: <div class="container"> <nav class="navbar navbar-expand-lg navbar-light bg-light& ...

Angular: bypassSecurityTrustHtml sanitizer does not render the attribute (click)

I'm facing an issue where a button I rendered is not executing the alertWindow function when clicked. Can anyone help?: app.component.ts: import { Component, ElementRef, OnInit, ViewEncapsulation } from '@angular/core'; import ...

The Javascript function is triggered only upon the second click

My goal is to retrieve prescription details for a specific patient, with the results displayed in a modal window. However, I am encountering an issue where the first result is only obtained on the second click. Subsequent clicks display the results of the ...

Tips for styling links in Nuxt/Vue based on specific conditions

Struggling with conditional styling in my Nuxt app. I want to change the background color of the active link based on the current page. Using .nuxt-link-exact-active works for styling the active link, but how can I customize it for each page? The links ar ...

Unable to conceal adjacent radio buttons

My challenge is to use only HTML and CSS, without JavaScript, to show the first radio button with its label initially. When a user clicks on it, the second radio button and label should appear while the first disappears, and so on. I am struggling to ach ...

What could be causing the HTML content to not display when the code is executed?

I've recently started using Visual Studio Code for my project. I am able to build my code without any errors, but when I run it, the HTML content does not display properly - only the CSS styling for the header and footer is visible. I have tried click ...

having trouble with changing the button's background color via toggle

I've been experimenting with toggling the background color of a button, similar to how changing the margin works. For some reason, the margin toggles correctly but the button's color doesn't. <script> let myBtn = document.querySele ...

How to position child divs at the center of a parent div

I am looking to set the maximum width of the "circlecontainer" to 300px, but I also want my 4 child divs (each with a max width of 300px) to remain centered in the browser when it exceeds a width of 1200px. Currently, they are aligned to the left side. Be ...

Should pages be indexed to index.php or should the top and bottom parts of the page be created and included in all content pages?

I've been out of the webpage creation game for a while, but I've recently decided to get back into it. Like everyone else, I want to learn the best way to do this. However, I find myself facing a dilemma on how to structure my pages. My initial i ...

QuickFit, the jQuery plugin, automatically adjusts the size of text that is too large

I have incorporated the QuickFit library into my website to automatically resize text. However, I am running into an issue where the text is exceeding the boundaries of its containing div with a fixed size. This is something I need to rectify. This is ho ...

Is it possible to implement dependency injection within a .css document?

I have a C# .NET 6 application. Some of the web pages (Razor Pages) in the app use dependency injection to inject configuration into the Razor Pages (.cshtml files), allowing certain config elements to be displayed in the user interface. My query is, can ...

What are the steps for sharing and sending content using email?

I have a website and I'm looking for a way to post content via email, similar to popular social networks like Facebook and Flickr. For example, I'd like to send a message to '[email protected]'. The title would be stored in a MySQ ...

Using HTML5 Server-Sent Events (SSE) in conjunction with JSON and PHP

Struggling to implement HTML5 Server Sent Events that update a webpage with JSON data, I have scoured through numerous info sites and tutorials online without finding any suitable for beginners like myself. Even after exploring similar questions on StackE ...

Tips to detect a specific animation completion on an element?

How can I ensure that a specific animation ends when multiple animations are triggered on an element? My scenario involves an overlay song list that appears when a list icon is clicked. The challenge lies in closing the menu smoothly. I have implemented a ...

SQL Server database is showing question marks instead of HTML symbols

Recently, I observed that special HTML symbols like: ★ are appearing as question marks in my database records. I have set the type of the field as varchar and am utilizing Microsoft SQL 2008 for my database management. Is there a solution to this symbo ...