Arranging Multiple DIVs Next to Each Other with Middle Vertical Alignment

We are facing the challenge of aligning multiple DIVs side by side while centering the content vertically. Using FLOAT or INLINE-BLOCK makes it easy to place the DIVs alongside each other, but vertical alignment becomes a problem. Any suggestions on how to achieve this without relying on a framework would be greatly appreciated!

Some recommendations pointed to an article discussing vertical alignment within a single div element, but our scenario involves two or more divs placed next to each other. Vertical alignment of elements in a div

Option 1:

<div>
    <div style="width: 150px; height: 150px; text-align: center; vertical-align: middle; float: left; padding: 7px; border: 1px solid #000;"><img style="max-width: 100%; max-height: 150px; border: 0;" src="http://unsplash.it/150/100?random" alt="Gaslight" width="150" border="0"></div>
    <div style="width: 150px; height: 150px; text-align: center; vertical-align: middle; float: left; padding: 7px; border: 1px solid #000;"><img style="max-width: 100%; max-height: 150px; border: 0;" src="http://unsplash.it/150/100?random" alt="Gaslight" width="150" border="0"></div>
</div>

Option 2:

<div>
    <div style="width: 150px; height: 150px; text-align: center; vertical-align: middle; padding: 7px; border: 1px solid #000; display: table-cell;"><img style="max-width: 100%; max-height: 150px; border: 0;" src="http://unsplash.it/150/100?random" alt="Gaslight" width="150" border="0"></div>
    <div style="width: 150px; height: 150px; text-align: center; vertical-align: middle; padding: 7px; border: 1px solid #000; display: table-cell;"><img style="max-width: 100%; max-height: 150px; border: 0;" src="http://unsplash.it/150/100?random" alt="Gaslight" width="150" border="0"></div>
</div>

Answer №1

Give this a shot:

.content div
{width: 150px; height: 150px;  float: left; padding: 7px; border: 1px solid #000;display: flex;
align-items: center;
justify-content: center;
}

SEE IT IN ACTION HERE

Answer №2

When working with a single line of text, consider setting the line-height: 150px for your <div>

Another option is to utilize flex properties for simplicity:

display: flex;
align-items: center;
justify-content: center;

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

"Learn the steps to toggle a sub menu using an onclick event and how to hide it using another

I created a sidebar navigation that displays submenus on mouseover, but I want them to open on click and close when clicking on the same tab. Please take a look at my code on this CodePen link. Thank you. <nav class="navigation"> <ul class="mai ...

Embedding JavaScript information into a cell of an HTML table

I am currently working on creating a pop-up box that, when triggered (with a delay), will showcase a counter displaying the number of clicks along with the dates of each click! I would like to present this information in a structured table format within a ...

Can Vuetify's grid system seamlessly integrate with the Bootstrap grid system?

According to information from the Vuetify documentation: The Vuetify grid draws inspiration from the Bootstrap grid. It involves the use of containers, rows, and columns to organize and align content. If I utilize Bootstrap grid classes in a Vuetify pr ...

Background that stretches to 100% width but only fills to the right side

UPDATE: I figured out why it wasn't working - the element with a width of 100% was nested inside a div with the class "container," removing that solved the issue! (I don't have enough reputation to answer my own question yet, just wanted to updat ...

Creating a responsive canvas and image container in fabric.js involves adjusting the layout and size of the

I am working with fabric.js and canvas to dynamically add an image from a URL. I would like to make the canvas responsive based on the resolution. How can this be achieved? Here is my current code: <canvas id="panel" width="700" height="350"></ ...

Ways to position divs without causing them to collapse or override existing divs

I am currently developing a demonstration in which users can select jQuery areas to create square blocks. When a user selects an area and adds a comment with color, the entire block is displayed at a specific position. This feature is working perfectly as ...

Tips for displaying an array in a tabular layout using angularjs

API calls are my jam, and I'm trying to display the JSON results in a nice tabular format using angularjs. To achieve this, I've set up a function (myfunc) with a hardcoded array called train. Now all that's left is to print this array in a ...

Populating a select dropdown menu with options pulled from SQL table entries

Within my database, there is a table named "Registration" which is used for registering into clubs. This table consists of two columns: 'clubName' and 'registrationStatus'. The clubName column contains the names of different clubs, whil ...

Utilize a boolean value to enable the button and remove the greyed

Can anyone help me with using a password strength check to enable/disable a button in JavaScript? I have a function that checks if a password is strong enough and matching, returning a boolean value. How can I use this value to ungrey a button that is by d ...

Create an image of a static map from Google

I have incorporated the Google Static Map API into my Angularjs Application to display images of Google maps. Browse here for a glimpse Everything operates smoothly when there are fewer map coordinates. However, when I increase the number of map coordina ...

Adjusting the transparency levels of all div backgrounds simultaneously

Is there a way to uniformly add an alpha value to all the divs on my web page without specifying the exact color code in 'rgba'? I have divs with different background colors and I want to easily revert the changes if needed. Perhaps something al ...

Creative Vue.js and Tailwind CSS card layout featuring an image extending beyond the boundaries of the card

I'm attempting to design a card where an image pops out of the card. I've tried using z-index, but it doesn't seem to be working as expected. Here is the code I have: <div class="flex flex-col"> <div class=&quo ...

Setting the default value for ngModel does not receive any information from the model

I am trying to populate a form with default values using the code below: <h3>ِStart Time</h3> <div class="row" > <div class="col"> <label for="startTime">Hour(s) </label> <input type="numb ...

The contents within the <div> element are not appearing on the webpage

I've been attempting to design a card that performs a 3D rotation when the mouse hovers over it. While I've successfully incorporated the background and front image of the card, I'm facing an issue with adding text to it. Here is the incorr ...

displaying the timestamp of the most recent reaction inserted

I am trying to figure out how to display the timestamp of the most recent 'reaction' that was added. I believe there is a straightforward PHP solution for this, but I haven't been able to find it yet. https://i.sstatic.net/siKUs.png As ind ...

firefox compatibility issues with jquery ajax

Trying to establish a connection with a URL and expecting a large object in response has proven to be challenging. The process appears to function smoothly until an error occurs during the return reply, triggering the .ajax.error() function. Unfortunately, ...

Launching Android Calendar app using Intent from a web browser (Chrome)

Did you know that you can use HTML links to open Android apps from within Chrome? Take a look at this resource. If you're trying to open the calendar app from an Android app, the intent should be content://com.android.calendar/time. Learn more about ...

Positioning buttons in a grid using CSS - Tips and tricks

Help needed! Seeking professional advice. I have a few issues that I can't seem to solve on my own. Firstly, regarding the second menu - should I use padding to position it correctly? Secondly, although I managed to make the navigation bar visually s ...

Using the `ng-repeat` directive as an argument in a Javascript function

I am struggling to pass the ng-repeat element to a JavaScript function, but it's not working as expected. <tr ng-repeat="x in myArry"> <td><input type="text" value="{{x.firstname}}" id="{{x.firstname}}{{$index}}" onblu ...

A minimalist web page appearing as blank in Internet Explorer

After testing my simple web page in various browsers, I discovered that it only showed a blank white page in Internet Explorer. I combed through my CSS code and identified background = rgba(some stuff) as the unsupported line by IE. However, this particula ...