Achieve vertical alignment of a div alongside an image without utilizing tables or flexbox

As a beginner, I know this question has been asked numerous times, but I am struggling to find a straightforward answer that makes sense to me. I prefer not to use tables or flexbox, just plain CSS.

What I Need: I have an image and I want to vertically center multiple lines of text next to the image on the right.

My Attempts So Far: I tried placing the text inside a div with display: inline-block and styled both the image and div with vertical-align: middle.

The Issue: This approach seems to be working, but I'm puzzled about why it works. Do I really need to put the text inside a div? Is there a way to achieve this with less code? Also, keep in mind that the height of the image may vary based on the viewport width.

Check out this JS Fiddle https://jsfiddle.net/4LLruu7c/

img {
  width: 40%;
  margin-right: 30px;
  vertical-align: middle;
}
.list_text {
  display: inline-block;
  width: calc(60% - 40px);
  vertical-align: middle;
}
<ul>
  <li>
    <img src="//placehold.it/200x200">
    <div class="list_text">Client name
      <br>Project title, description, and blurb</div>
  </li>
</ul>

Answer №1

According to information provided on Mozilla Developer Network:

Values (for inline elements)

Most of the values align the element vertically in relation to its parent element:

middle

This value aligns the middle of the element with the baseline plus half the x-height of the parent.

Essentially, this means that you must have a text div to ensure two inline elements are aligned vertically at the center of the line, with respect to their parent.

Since your image is the tallest entity on the line, the text div will be centered within the line, effectively positioning it at the center of the image. That explains why your code functions correctly.

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

Position a div in the center of the page horizontally

Can someone help me with aligning the #main div in the center of the page? I have this code snippet: <div id="main" style=" margin:0 auto; "> <div style="float: left"> <a style="display: block" href="#"><img src="test.gif" ...

Utilizing AngularJs and Materialize.css to create numerous dropdown menus

I am encountering a troublesome issue that I just can't seem to resolve. My current setup involves using AngularJs to showcase a set of cards, each equipped with its own dropdown menu. Here's the snippet of code in question: <div ng-repeat=&q ...

What is the best way to utilize variable fonts with Google Fonts?

Traditional fonts typically require a separate file for each weight variant, such as 300, 400, and 500. However, with variable fonts, all weight combinations can be accessed through a single compact font file. This is extremely advantageous for web design. ...

Achieving a full-width image display in Bootstrap: A guide

I've been struggling to find a solution to stretch an image across a div using CSS and Bootstrap. I attempted to make both the div and parent container fluid, but no matter what I try, there always seems to be some white space on the left and right si ...

Issue with non-responsive images in Bootstrap 3

Here is the bootstrap configuration that I am currently using: http://www.bootply.com/118172 I'm having an issue with the responsiveness of the image. It's not shrinking to match the height of the div on the left side. Can anyone help me trouble ...

I am struggling to align elements properly with Bootstrap, and I am encountering issues with the border radius of a specific div

I'm having trouble aligning all the elements in the center of my login form created with HTML and Bootstrap. The header appears to be center-aligned, but it is not vertically aligned with the other elements. I've been using the Grid system and ne ...

"Creating the Perfect Parallax Effect: A Step-by-Step

Seeking to implement a responsive parallax effect on my website, I have the following structure: <section id="first"> <div class="text fixed" > <h1><b>aSs</b> lorem ipsum dolor sit amet</h1> <p> ...

Positioning the midcontent within a fullwidth image presents quite a challenge

Concerning the design I have, I am facing some uncertainties. Here are my thoughts on it: If desired, the individuals in the background can be portrayed using a static image. With a CMS system in place, my client will have the opportunity to upload images ...

Using the device's width and height with material-ui

Currently, I am attempting to specify the width and height for only the (Iphone 7 Plus) within my project using withStyles. import { withStyles } from "@material-ui/core"; I have tested the following code: "@media (width: 414px) and (height ...

Why was the element with id=box-2 displayed outside of its designated box? Exploring the overlapping rule of floats in CSS

The stylesheet code is #box { width:400px; height:200px; border:1px solid black; } #box-1 { width:100px; height:100px; background-color:red; float:left; } #box-2 { width:100px; height:100px; background-colo ...

The head tag specification doesn't properly include the CSS file reference

I have a question regarding my HTML5 web page and the way I am referencing a CSS file in the head tag. Currently, I have it set up like this: <head> <link rel="stylesheet" href="css/style.css" type="text/css"/> </head> However, it d ...

Wrapping text around a container element

Is there a way to make the text wrap around a div when you want the div positioned at the bottom of a container? I've been able to align the text properly with the div when it's on top, but when I attempt to move it to the bottom of the page, th ...

Can you display a Div Section by using a Toggle button?

function toggleSections(section1, section2, section3, section4) { document.getElementById(section1).style.display = "block"; document.getElementById(section2).style.display = "none"; document.getElementById(section3).style.display = "none"; documen ...

Bootstrap version 4.1 introduces a new feature that allows the collapse menu to automatically close when clicking outside of the div

I'm currently working on creating a plug and play Mega-Menu using the collapse method which has proven to be quite effective so far. However, I'm facing an issue with closing the collapsed item when clicking outside the menu. As a beginner in Ja ...

Adjustable Height Feature for JavaScript Pop-Up Windows

I'm looking to design a popup window that has a fixed width and a height that adjusts based on the user's screen size. Is this even possible? I've searched through various websites but haven't found a solution yet. I came across a simi ...

Leverage the power of Angular by configuring a custom webpack setup to

Implementing the CSS modules concept in my Angular app has been a challenge due to conflicts with existing frontend CSS. My project utilizes SCSS, and I am looking for a way for webpack to modularize the CSS generated from SCSS during the final build step. ...

Having trouble with Gulp JS and CSS minification where existing minified files cannot be replaced

I've hit a roadblock in trying to resolve my search and google issue. My current challenge involves Gulp and cssmin. I can't seem to pinpoint the cause of an error. My aim is to have both the original CSS files and their minified versions in the ...

The MaterialUI TextField isn't behaving as expected when attempting to change the background color

Currently, I am facing a challenge with setting the background color for my `TextField` components within the app I am developing. Despite adding custom RGB values using `style={{background: "rgb(232, 241, 250)"}}`, the component continues to dis ...

Can a border-bottom be made the same size as a class it is not linked to in CSS?

Can I set a border-bottom to match the size of a separate class? For example, I want a border-bottom at the bottom of each <section> tag. However, since the sections span the full width of the screen, the border-bottom inherits that width. Each < ...

Is there a way to rearrange a group of divs using flexbox?

I'm currently exploring the world of responsive web development using media queries, and I have a question - is it feasible to shift an element from one div to another without relying on a script? In the desktop view, the layout should be like this: ...