Creating uniform height for divs nested within table cells

I need to display data in a table layout, but the design requires separation borders between cells.

After exploring different options, I found that using div elements within the td elements was the most effective solution.

Example:

<table>
    <tr>
       <td>
          <div class="inner">
             Content
          </div>
       </td>
    </tr>
</table>

CSS:

table td {
    padding: 15px 10px;
    border-bottom: 1px solid #e5e5e5;
}

table td div.inner {
    padding: 10px 25px 10px 0;
    border-right: 1px solid #e5e5e5;        
}

While this setup works well visually, there is an issue with the varying heights of the vertical lines created by the border-right property on div.inner.

I attempted setting a fixed height on div.inner, but it caused alignment problems. Aligning the content vertically and maintaining equal heights for the separator lines has proven to be challenging.

Is there a way to ensure equal height for the vertical separator lines?

Alternatively, how can I maintain vertical alignment when using a fixed height on div.inner?

Please note: Using line-height to achieve the desired result is not feasible due to the presence of additional elements like progress bars within div.inner.

Answer №1

When it comes to browser support, my solution relies on the use of the :before or :after pseudo elements, as well as the :last-child selector. This approach may not be fully compatible with all browsers, considering that CSS generated content has about 84.9% total support with an additional 7.56% partial support. Similarly, CSS3 selectors are supported by around 84.79% of browsers.

While achieving perfect cross-browser compatibility may not be possible, especially for older browsers, it should not impact the overall functionality of your site, only its layout. Setting the position of each table cell to relative and adding a pseudo-element with a right border of 1px can help maintain a consistent design across various browsers.

table td {
    padding: 15px 10px;
    position: relative;
    border-bottom: 1px solid #e5e5e5;
}
table td:before {
    border-right: 1px solid #e5e5e5;
    content: "";
    position: absolute;
    top: 10px;
    bottom: 10px;
    right: 10px;
    width: 0;
}
table td:last-child:before {
    display: none;    
}

This method is commonly supported in modern browsers like Chrome, Safari, and Firefox. By adding the :before pseudo element to the table cell instead of the inner div, you ensure that it adjusts to the final height of the row, maintaining equal heights for all cells. Keep in mind that this technique may not work on browsers lacking support for CSS generated content and/or CSS3 selectors, so consider providing a fallback style for those users.

Answer №2

I encountered a similar issue with my menu, and I found a CSS solution that worked for me:

Here is the HTML setup:

<table>
    <tr>
       <td>
          <div class="inner">
             Content
          </div>
       </td>
        <td>
          <div class="inner">
              <span style="font-size:40px;">AAAA</span>
          </div>
       </td>
        <td>
          <div class="inner">
              <span style="font-size:8px;">asd</span>
          </div>
       </td>
    </tr>
</table>

And here is the CSS code:

   table td {
    padding: 15px 10px;
    border-bottom: 1px solid #e5e5e5;
}

table td div.inner {
    padding: 10px 25px 10px 0;
    position: relative;
}
table td div.inner:after {
    position: absolute;
    top: 50%;
    margin-top: -20px;
    margin-bottom: -20px;
    margin-left: 23px;
    height:40px;
    width: 1px;
    content: "";
    background-color: #e5e5e5;
}

You can view the jsfiddle example here.

Answer №3

In my opinion, the solution might involve specifying a fixed height for the div and vertically aligning it in the td. It could be helpful to create a fiddle with some placeholder rows to better understand the issue.

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

Move the div from side to side when clicked

My mobile navigation bar currently has overflow, requiring users to scroll left or right to see all the choices. I want to implement buttons with arrows that will automatically slide the navigation bar to the desired direction when clicked. I have attempt ...

Zurb Foundation's sections have a strange issue where navigating between them introduces unexpected whitespace

I'm feeling frustrated as I try to work with Zurb Foundation sections. I've updated to the latest version 4.3.1. Following the documentation provided here: but encountering strange behavior while navigating through results. Refer to the screen ...

The actions performed within an event listener function are undone once they have been carried out

I'm a newcomer to Javascript and I am currently experimenting with it. While browsing through a tutorial on w3schools, I came across an example of changing the color of a button after clicking on it. My idea was to take it a step further by loading a ...

The pure css overlay does not display correctly on Chrome

Could you take a look at the link provided below (just widen the result window a bit) and let me know why there are white lines on top and on the sides of the boxes when the hover overlay is fully loaded? Even when I use a background image for those divs, ...

Guide on coding in Node.js to showcase an image on a web browser

Is there a way to set a local image file as the background for my browser page? I am experiencing an issue where the image does not display when I run my app index.js file through the node.js terminal and visit localhost:3000, even though it works fine whe ...

Ember's distinctive feature: Named Block Helpers

Can we create "named blocks" in a different way? For instance, {{#customBlock "repeatableBlock"}} {{!-- block containing numerous properties and data that may become messy if hardcoded multiple times --}} {{/customBlock}} Then, elsewhere in the code, {{ ...

Modify the CSS of one div when hovering over a separate div

I've been working on a simple JavaScript drop-down menu, and it's been functioning correctly except for one issue. When I move the mouse out of the div that shows the drop-down menu, it loses its background color. I want the link to remain active ...

What is the best way to display the time of a post update similar to the style of

I am currently working on creating a notification deck. Instead of displaying the time when a notification was received, I aim to show the time that has passed since its arrival similar to how Twitter and Facebook do it with formats like "32m" or "1 hour a ...

The annoying Facebook "add a comment" popup refuses to close

Occasionally, the "add a comment" popup (iframe) in Facebook's Like plug-in fails to close and obstructs access to the content underneath. This issue has been noted while using Chrome 21 and Firefox 15. To replicate this problem, you can visit the fo ...

Deciphering HTML entities and decoding URLs within a continuous flow

Is it possible to find functions that accept streams and output streams for decoding operations, rather than loading the entire encoded document into memory like the HttpUtility.UrlDecode and HttpUtility.HtmlDecode methods do when taking strings as input ...

Animate the jQuery: Move the image up and down inside a smaller height container with hidden overflow

Check out the fiddle to see the animation in action! The image is programmed to ascend until its bottom aligns with the bottom of the div, and then descend until its top aligns with the top edge of its parent div, revealing the image in the process. ...

Place two pictures in the center of a div container

Here is a div setup that I am working with: <div id="browsers"> <h2>Title</h2> <p>Text recommending the use of either Chrome or Firefox.</p> <a href="http://google.com/chrome/"><img src="img/64-chrome.png" /> ...

My Bootstrap 4 table is not displaying as responsive

Need assistance with displaying a table in a Bootstrap 4 HTML template in a 320x480 view. The table is not responsive even when the .table-responsive class is added. Any idea what could be causing this issue? Here is the HTML code snippet with the table: ...

Transferring data from multiple form datepickers to identical destinations

I'm encountering difficulties with setting up multiple distinct datepicker instances within a single div and then sending the dates through a form. The issue lies in the fact that my div currently contains multiple array elements constructing this fo ...

What's inside the navigation bar, icons, and content cards? (Using HTML and Bootstrap)

Having a few issues that need resolving: Unable to display the contents of my navbar after the navbar-brand section. Looking to add a home button, about section with a drop-down menu including: 'Our mission', 'our team', & ...

Troubleshooting issues with custom global components failing to apply styling in NuxtJs

I have designed various components such as buttons that I want to be able to use and reuse across my entire website. I have already developed plugins Object.entries(components).forEach((([name, component]) => { Vue.component(name, component) })) and ...

Align elements vertically with React-Bootstrap by using the built-in functionality for center

Struggling to vertically center and center align elements using bootstrap, even with react-bootstrap col and row components. You can view my code on Codesandbox: Link Here is the code snippet: import React, { PureComponent } from "react"; impo ...

Tips on implementing a circular progress bar with locomotive scroll functionality

Can anyone help me integrate progress functionality with Locomotive Scroll using JavaScript? Link to CodePen for reference Check out this code snippet from Locomotive Scroll that calculates the percentage of pages scrolled: const scroller = new Locomotiv ...

Displaying a table with conditional rendering and dropdown selections using React and MUI

In an attempt to conditionally display table information in a reusable format, I am faced with the challenge of rendering a table with dynamic content. If the incoming data includes a "status" property (data.status), the corresponding column should contain ...

Enumerate the data URLs present on a webpage

To automatically download all the files from a webpage (where data urls are different from the webpage url), I need to extract those data urls from the html code. Here's how I go about it: library(XML) url <- "http://www.data.gouv.fr/fr/dataset/re ...