Achieving optimal vertical alignment with CSS and DIVs

I am struggling to vertically center my comments. Despite trying various methods, I have not been successful. Below is the code snippet along with a screenshot showcasing the issue.

Can you identify what is causing the problem?

.comment {
    min-height: 400px;
    background-color: red;
    padding: 5px;
    float: left;
    display: table-cell;
    vertical-align: middle;
}

The corresponding HTML:

<tr>
    <td colspan="2">
        <div class="profile_picture_container">
            <img class="profile_picture" src="URL" />
        </div>
        <div class="comment">
            <b>Patrick Reck</b> Come on, would you just work already!
        </div>
    </td>
</tr>

Answer №1

Based on the guidelines provided in the CSS 2.1 specification

The cell box's height is determined by the content inside it. While the row's height can be influenced by the table cell's 'height' property, it does not directly impact the cell box's height.

To achieve the desired outcome with a fixed height value, you can use the following approach:

Try implementing this code snippet:

.comment {
    height: 400px;
    background-color: red;
    padding: 5px;
    display: table-cell;
    vertical-align: middle;
}

View the visual representation of the solution on JS Fiddle

Answer №2

Combining float and display can cause issues.

To vertically center a table cell, use the middle property.

An image as an inline-box will vertically center among text or other inline-boxes.

To achieve this layout in HTML, follow this structure:

<td>
  <img />
  <span>text</span>
</td>

And apply the following CSS:

    td,
    td img,
    td span {vertical-align:middle;}
    /* convert span into an inline box */
    td span {display:inline-block;
    /* consider setting max-width or adjusting white-space properties */
}

Answer №3

Using divs in table cells can feel like hedging your bets. Why not eliminate the table entirely and stick with divs? Check out this example for inspiration: http://codepen.io/pageaffairs/pen/JmzEe

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8>

<style media="all">
.wrap {
    display: table;
    min-height: 400px;
    padding: 5px;
    background: red;
}

.wrap div.comment {
    display:table-cell;
    vertical-align: middle;
}
</style>

</head>

<body>
<div class="wrap">
    <div class="profile_picture_container">
        <img class="profile_picture" src="URL" />
    </div>
    <div class="comment">
        <b>Patrick Reck</b> Come on, would you just work already!
    </div>
</div>
</body>
</html>

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

Having trouble with jQuery not functioning properly in IE9 when trying to add items to a List

I've created a listbox that allows users to add or remove items. To add an item, they can enter text in a textbox and click the "Add" button, which will prepend the text to the top of the list if it's not already present. I used jQuery to handle ...

Can you please provide the CSS code that would style this table in a similar manner?

I am interested in utilizing the design of this table, but how can I update it to meet CSS standards and HTML5 equivalents? <html> <head></head> <body> <table border="1" bordercolor="#000000" style="background-color:#ffffff" w ...

Manipulating contextual selectors

Have you ever tried reverting or overriding attributes from contextual selectors? .card { padding-top: 20px; ... } .card .card-header { padding: 0 20px; } .card .card-header.news { padding-top: 0px; } Do you think it's possible to elimina ...

Connecting various components together

I am attempting to establish a connection between various elements in order for a specific class element to trigger an action on another specific class element (for example, when .ph5 is clicked, .art5 should become visible) Here is what I have developed ...

Retrieve the CSS class definition using the console in the Chrome Developer Tools

Is there a way to automatedly extract CSS class definitions from Chrome Developer Tools? I want to retrieve the styles defined in a specific class name, similar to how it is displayed in the Styles tab on the right-hand side. I know about the getComputedS ...

The functionality of the angularjs class seems to be malfunctioning

I'm a new learner of angularjs and I've created a simple page below. I have a style called "item" that I'm trying to apply to a div, but it's not working. However, if I use inline style, then it works fine. Can someone please help me f ...

Switch between divs using an update panel

Consider this scenario: Numerous div controls are present on an aspx page, and update panels are being used to prevent page refresh. These divs contain various controls like buttons and textboxes: <asp:UpdatePanel ID="UpdatePanel1" runat="server"> ...

Tips for resolving alignment issues in a chat box:

I am currently working on adjusting the alignment of my chat box app. I have placed 3 div elements with the class .bubble inside a div with the class .chatlines, but they appear to be clustered together instead of aligned properly. It's challenging to ...

The formgroup label is currently displayed in uppercase, but I would prefer it to be in title case

In the angular template below, I have noticed that even though the labels are in titlecase, they appear in uppercase when the page is rendered. This wasn't the desired outcome so I attempted to use the titlecase pipe and enclose the label text within ...

The :empty selector is failing to function properly in @media print queries

Currently, I am working in Twig on Symphony and have encountered an issue with an empty div that needs to be hidden in print mode. <div class="form-fields__list"> </div> Surprisingly, the :empty selector works perfectly fine on the screen, bu ...

Exploring the ins and outs of flexbox and utilizing overflow:auto

In my current situation, I have successfully resolved all cases but am now seeking assistance in debugging my mental model. Specifically, I am focusing solely on Chrome for the sake of simplicity. Within my nested "holy grail-ish" flexbox layouts, I have ...

I keep getting a "Resource Not Found" error when using Angular's URL template

While working on my page, I encountered an error message related to pagination implementation: angular.min.js:87 GET http://localhost:56485/Areas/EVerify/Views/EVerify/dirPagination.tpl.html 404 (Not Found) My JavaScript file looks like this: var EVerif ...

The expression '<xsl:value-of select="document(content)//title"/>' results in a null node response

Can someone assist me in retrieving the title of a basic HTML document in order to create a sitemap? I have been receiving an empty value every time I try. Upon inspecting the issue, it appears that the document(content) is returning document nodes. It s ...

Bootstrap - the input group button is designed to align to the right side

Currently, I am in the process of constructing a navigation bar for a website that includes a search bar and a grouped button. Everything functions properly when viewed on a desktop, but as soon as I switch to mobile view and the collapsible menu is activa ...

Begin the process of setting up PDF.js on the website

I've been attempting to incorporate PDF.js into my website, but I'm struggling to do it correctly. When I try to display the PDF file on the screen, I encounter this issue: https://i.sstatic.net/aixrP.png Although I can't see the PDF file ...

Loading a PHP template into HTML using a JQuery loop

I am struggling with an API call that returns an array of arrays. My goal is to loop through each value and use them to load a PHP template that I have created. The issue I am facing is that only the first value is being displayed properly, while the subse ...

Displaying an HTML file in a Node and Express application using JavaScript File handling

Starting my first Node, Express & Angular project has been a bit tricky due to issues with the asset pipeline. I'm not entirely sure if it's related to my folder structure, which I tried setting up based on online tutorials but am open to suggest ...

changing pictures with jquery

Struggling with this code snippet: $('#clicked_package').css({"background-image" : "url(img/head_2.png)"}).fadeOut("slow"); $('#clicked_package').css({"background-image" : "url(img/head_2_normal.png)"}).fadeIn("slow"); No matter which ...

Root location for offline and pre-production in the Backbone boilerplate router

I am currently utilizing the backbone boilerplate found at https://github.com/tbranyen/backbone-boilerplate My development process involves working on static HTML/JS files offline and conducting tests offline before deploying them to another site for pre- ...

Retrieve the scrolling height in Vue.js 2 window

I need to apply the sticky class to the navbar when a user scrolls down, and remove it when they scroll back up. To achieve this, I am attempting to calculate the scrolled height. Currently, my code looks like: mounted(){ this.setUpRoutes(); wind ...