Centering content vertically inside div columns

Have you ever wondered if it's possible to vertically align content in a row with two columns, one containing an image and the other text using CSS?

I've spent days trying different approaches but nothing seems to work.

Here is the basic structure I am currently working with, focusing on responsive design with sections set to a minimum height of 100%.

CSS

html, body {
    width: 100%;
    height: 100%;
}

section {
    width: 100%;
    min-height: 100%;
    display:table;
    height:inherit;
}

.row {
    width: 100%;
    height: 100%;
    display:table-cell;
}

.col-left, .col-right {
    float: left;
    width: 50%;
    height: 100%;
}

/*--this should be vertically centred--*/

.content {
}

HTML

<section>

        <div class="row">

            <div class="col-left">
                <div class="content">
                    <h1>SOME TEXT</h1>
                </div>
            </div>

            <div class="col-right">
                <div class="content">
                    <img src="SOME IMAGE URL">
                </div>
            </div>

        </div>

</section>

JSFiddle

Answer №1

.section {
    ...
    display:table-section;
}
.div-left, .div-right {
    ...
    display: table-box;
    vertical-align: center;
}

View Demo

Answer №2

You made great progress with the structure using display:table and display:table-cell, but there were some errors in your implementation. Avoid floating elements that should be table cells and ensure correct usage of table-row and table-cell properties. Here is a revised version:

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
section {
    width: 100%;
    min-height: 100%;
    display:table;
    height:inherit;
}
.row {
    width: 100%;
    height: 100%;
    display:table-row;
}
.col-left, .col-right {
    display:table-cell;
    width: 50%;
    height: 100%;
    vertical-align:middle;
}
.col-left {
    background: MidnightBlue
}
.col-right {
    background: ForestGreen;
    text-align: center;
}
.content {
    border: 1px dashed red;
}
h1 {
    font-family: sans-serif;
    color: white;
}
<section>
    <div class="row">
        <div class="col-left">
            <div class="content">
                 <h1>I should be vertically aligned in the middle...<br><br>And so should the image on the right...
                </h1>

            </div>
        </div>
        <div class="col-right">
            <div class="content">
                <img src="https://cdn4.iconfinder.com/data/icons/miu-flat-social/60/stackoverflow-128.png">
            </div>
        </div>
    </div>
</section>

Answer №3

Here is a suggestion to try:

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
section {
    width: 100%;
    min-height: 100%;
    display:table;
    height:inherit;
}
.row {
    width: 100%;
    height: 100%;
    display:table;
}
.col-left, .col-right {
    float: left;
    display:table;
    vertical-align:middle;
    width: 50%;
    height: 100%;
}
.col-left {
    background: MidnightBlue
}
.col-right {
    background: ForestGreen;
    text-align: center;
}
.content {
    display:table-cell;
    vertical-align:middle;
    height:100%;
    border: 1px dashed red;
}
h1 {
    font-family: sans-serif;
    color: white;
}
<section>
    <div class="row">
        <div class="col-left">
            <div class="content">
                 <h1>I should be vertically aligned in the middle...<br><br>And so should the image on the right...
                </h1>

            </div>
        </div>
        <div class="col-right">
            <div class="content">
                <img src="https://cdn4.iconfinder.com/data/icons/miu-flat-social/60/stackoverflow-128.png">
            </div>
        </div>
    </div>
</section>

Answer №4

Exploring the topic further, I found myself pondering the best method for building columns in web development: Floating or Display property. As a self-taught developer, I often encounter challenges when using float elements to structure columns, leading to a sense of inconsistency and unreliability in my designs.

Surprisingly, there exists a simple and elegant solution known as Display Table that provides a solid foundation for layout structure without the need for hacks. This method offers versatility and ease of use, eliminating common struggles like vertical alignment issues.

The key lies in a straightforward structure:

.container {
  display: table;
}
.row {
  display: table-row;
}
.col {
  display: table-cell;
}

Despite its effectiveness, the term "table" may deter some from embracing this technique. However, it delivers reliable results and simplifies the design process significantly.

Interestingly, resources advocating for this approach may not be easily accessible through conventional searches on CSS columns.

Additional articles worth reading:

Give Floats the Flick in CSS Layouts

Farewell Floats: The Future of CSS Layout

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

Looking to retrieve a specific portion of HTML data returned from an AJAX request

Looking to extract specific HTML content from the response received from an ajax call made to the controller. This task will involve utilizing an ID reference. I am encountering difficulty in employing the find function directly on the data as it is curre ...

Customize the focus color of mat-label within a mat-form-field in Angular

Within my mat-form-field, I have a mat-chip-list set up similar to the example provided in this link. When I click inside the field, the mat-label and what appears to be the border-bottom (not entirely sure) change color when focused. How can I customize t ...

Utilize the content within an input field to perform a calculation and showcase the outcome in a separate element

Currently, my shopping cart displays a price, quantity, and subtotal field for each product. While the user can change the quantity field, the other fields remain static. I am looking for a method to automatically calculate the subtotal whenever the user ...

Transitioning images in JQuery by using a rollover effect

Is there a way to use Jquery for a hover effect that transitions images with a slide up animation? I've looked everywhere online, but I can't seem to find a code that works. All the examples I've found use fadeIn or fadeOut. Thank you for yo ...

Mysterious issue causing PHP $_POST to return null

Having a bit of trouble with my PHP $_POST that keeps returning a null or empty value. Although I can't seem to spot any mistakes in my code (I know they are there somewhere), I'm on a time crunch and can't take a break. So, I was hoping som ...

Is my webpage loading even as the spinning wheel is being displayed?

This is the App.js import Newhome from './COMPONENTS/Newhome'; import HashLoader from "react-spinners/HashLoader"; import { useState , useEffect } from 'react'; import "./App.css"; function App() { const [loading ...

When hovering over an object in three.js, the cursor should change on mouseover

In my scene, I have added a sphere and plane geometry. Clicking on the plane geometry will open a linked website. Now, when hovering over the plane geometry, I want the mouse cursor to change to a hand pointer icon, and revert back to its original style ...

Using jQuery's .post() method to automatically scroll to the top of

Is there a way to prevent the automatic scrolling to the top of the page when executing the buy function? Here is the code snippet involved: <a href="#" onClick="buy(id)">Buy</a> The buy function utilizes the $.post() function for processing t ...

Contact form in PHP fails to send upon submission

An issue I am currently facing involves a simple php mail server setup in my nodeJS site for managing contact messages. Strangely, when testing it from my local machine, the message does not get routed to the designated email address. I suspect that this c ...

Numeric input of 10 not recognized in the textbox

My validation for a textbox only accepts two digits, such as 11 or 12. However, it does not accept the digit 10. Check out my code snippet: <td width="60" height="20" class="case_txt" align="center"> <input type="text" onblur="CheckUnitValue ...

Enhance your FullCalendar experience with beautifully crafted tooltips

How do I implement a tooltip design on a full calendar? I need the tooltip to appear when hovering over an event in the calendar. Any assistance with this issue would be greatly appreciated. Thanks in advance! //reference <link rel="stylesheet"hr ...

Replacing variables in a function: A step-by-step guide

I have frequently used the replace function to eliminate classes in JavaScript. Currently, I am working on creating a JavaScript function that allows me to remove a specific class from an element by passing in the element and the class name. changeAddress ...

Clicking on the overlay does not close the bootstrap collapsed toggle

I'm currently facing an issue where I need to add a listener that closes the menu when clicked away. The code snippet below shows my attempt at solving this problem: $("body").click(function(e){ var $box1 = $('.navbar-toggle'); var $b ...

Delete an entry from the list

I have a fully functional Ajax call and function that fills up a datalist. This datalist is utilized to choose from a limited list and add it to a UL element on the page. Once an option is added from the datalist to the actual list, I aim to eliminate that ...

jQuery's visibility check function is not functioning properly

I am developing a web application for managing orders and finance in a restaurant. To enhance the functionality of my application, I require more screens to operate with. To facilitate this, I have implemented a small function to toggle between visibility: ...

Hide a parent div in jQuery depending on the checkbox status

I have a code snippet that I need help with. You can view the code here. $("#filters :checkbox").change(function() { $(".listing-details > div").hide(); $("#filters :checkbox:checked").each(function() { $(".listing-details ." + $(this). ...

Using display:inline-block will increase the vertical spacing

I am currently dealing with the following HTML and CSS setup: * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body { margin: 0; } .row { background-color: red; /* display: inline-block; */ } ul { ...

Discover the method for measuring the size of a dynamically generated list

I'm currently working on a chat box that displays messages as a list. One issue I'm facing is that when I click on the start chat button, the chat box opens but the length of the list always remains zero. How can I resolve this problem? $(docu ...

Jquery event handlers are not functioning properly on dynamically added iframes with constantly changing content

I have added a dynamic iframe to the document using jQuery on document ready function. $( document ).ready(function() { $("body").append(renderIframe()); });} ` The iframe is rendered through this function function renderIframe(){ return [ ...

How can I create a box-shaped outline using Three.js?

As someone new to threejs, I have been trying to figure out how to render a transparent box around a symbol in my canvas. The box should only display a border and the width of this border should be customizable. Currently, I am using wireframe to create a ...