Achieving proper stacking of a table with other div elements

I've come across some similar inquiries, but none seem to fully address the specific issue at hand. If there's an existing thread on this that I overlooked, I apologize in advance.

So, here's a multi-part problem: my solution for the first part has inadvertently caused a second issue.

I have a 4-column layout where the contents must maintain uniform height, like so:

https://i.sstatic.net/tRdY2.png

The second and fourth boxes should have background colors and need to match the height of the other two, which will be images. Currently, the most reliable way I found to achieve this is by structuring it as a table and setting the cell height to 100%, as shown below:

<table>
 <tbody>
  <tr>
   <td style="width: 25%;">
   <figure><img alt="" class="img-responsive" src="http://lorempixel.com/1000/1000/business" /></figure>
   </td>
   <td class="bg-brand-dark" style="height: 100%; width: 25%;">
   <div class="block-padding-hor">
   <h3 class="text-inverse h6"><strong>Featured Corporate News</strong></h3>

   <hr class="rule-part--bright center-block" />
   <p class="text-inverse h6">Hardly Dude, a new 'vette? The kid's still got, oh, 96 to 97 thousand, depending on the options.</p>

   <p class="text-center"><a class="currentURL" href="">Continue Reading</a></p>
   </div>
   </td>
   <td style="width: 25%;">
   <figure><img alt="" class="img-responsive" src="http://lorempixel.com/1000/1000/business" /></figure>
   </td>
   <td class="bg-gray-dark" style="height: 100%; width: 25%;">
   <div class="block-padding-hor">
   <h3 class="text-inverse h6"><strong>Featured Corporate News</strong></h3>

   <hr class="rule-part--bright center-block" />
   <p class="text-inverse h6">Hardly Dude, a new 'vette? The kid's still got, oh, 96 to 97 thousand, depending on the options.</p>

   <p class="text-center"><a class="currentURL" href="">Continue Reading</a></p>
   </div>
   </td>
  </tr>
 </tbody>
</table>

This resolved the initial problem effectively but led to a new complication. Any additional content placed below this markup gets obscured by the table above it, as illustrated in the undesired scenario below:

https://i.sstatic.net/XiVk2.png

Therefore, my question is: is there a reliable method to prevent this overlap? I initially assumed a table would act like a block element and stack accordingly, but apparently, that's not the case. Thank you in advance for any guidance, even if it involves blunt feedback along with a link to an existing thread addressing the issue!

::EDIT::

I omitted the CSS since it likely doesn't affect this issue, but if necessary for clarity, feel free to request it and I'll provide it.

::EDIT 2::

As requested, here's the CSS code, although it mainly involves setting background colors and centering elements:

<style>
  .img-responsive {
    max-width: 100%;
  }


  .bg-brand-dark {
    background-color: #004892;
  }


  .block-padding-hor {
    padding-left: 30px;
    padding-right: 30px;
  }


  .rule-part--bright {
    border: 2px solid #08b5fe;
    width: 50%;
  }


  .center-block {
    margin: 0 auto;
  }


  .text-inverse {
    color: #ffffff;
  }


  .h6 {
    font-size: 13px;
  }


  .text-center {
    text-align: center;
  }

  .bg-gray-dark {
    background-color: #4D4F53;
  }

</style>

Answer №1

After thorough investigation in the development tools, I finally pinpointed the issue. Surprisingly, it was not part of my initial markup but rather embedded within the content management system I was using.

The previous DIV element had a specific "max-height" property assigned to it with a pixel value. This caused the table content to overflow into the section below. The solution turned out to be quite simple, hiding in plain sight just outside my immediate focus.

Answer №2

In the past, tables were commonly used to create layouts with equal height columns.

But with the introduction of CSS Flexbox, achieving the same result requires less code and offers more flexibility.

There are multiple ways to achieve equal height columns using flexbox:

  1. Utilize the flex-grow property
    This property determines how much space an item should occupy within the flex container. By setting flex-grow: 1 (or using flex: 1 shorthand), the boxes will always fill up all available space in the container, ensuring equal heights regardless of content size or quantity.

  2. Use the align-items property.
    This property controls how flex items distribute available space along the cross axis. The default value is stretch, which expands each item across all available space, thereby keeping the boxes at equal heights by default.

  3. Apply the min-height property to the flex container.

HTML

<article id="container">
    <section class="box">1</section>
    <section class="box">2</section>
    <section class="box">3</section>
    <section class="box">4</section>
</article>

CSS (excluding decorative styles)

#container {
    display: flex;
    justify-content: space-around;
    min-height: 250px;
}

.box {
    display: flex;
    flex-direction: column;
    width: 20%;
    min-height: 200px;
    box-sizing: border-box;   
    overflow: auto;
}

.box:nth-child(even) {
    background-color: aqua;
}

DEMO

In the demo, any amount of content added to one box - including images - will result in all sibling boxes adjusting their height accordingly.

Note that flexbox is now supported by all major browsers, except for IE 8 & 9. Some newer browser versions like Safari 8 and IE10 may require vendor prefixes. To easily add all necessary prefixes, consider using Autoprefixer. More information can be found in this answer.

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 reset the default display option in a select dropdown using JavaScript or jQuery?

Here is some HTML code for a select element: <select> <br> <option>1</option><br> <option>2</option><br> </select> This select element will initially display the first option item (display ...

What is the process of linking this JavaScript code to an outer stylesheet and integrating it with the HTML document?

After encrypting an email address using Hiveware Enkoder, the result can be found below. obfuscated code I typically include it directly inside the div as is. However, I would like to streamline my code by placing it in an external file. While I know ho ...

Closing the dropdown menu by directly clicking on the button

I've noticed several inquiries on this platform regarding how to close a drop-down menu by clicking anywhere outside of it. However, my question is a bit different. I want the dropdown-menu to remain open once clicked, only closing when the user clic ...

Modifying the order of Vuetify CSS in webpack build process

While developing a web app using Vue (3.1.3) and Vuetify (1.3.8), everything appeared to be working fine initially. However, when I proceeded with the production build, I noticed that Vue was somehow changing the order of CSS. The issue specifically revol ...

Issue encountered when trying to import an image URL as a background in CSS using Webpack

I have been trying to add a background image to my section in my SCSS file. The linear gradient is meant to darken the image, and I'm confident that the URL is correct. background-image: url(../../assets/img/hero-bg.jpg), linear-gradient(r ...

Place a picture and words within a submit button

I need assistance with aligning text and a small airplane image on a submit button. Currently, the text is overlapping the image and I am unsure how to fix this issue. How can I adjust the position of the text to display to the right of the button? Is ther ...

Extraction of Canonical URLs

I am completely new to VBA programming and I need assistance with extracting links related to specific keywords from a website. My goal is to have these sourced links as the final outcome for all keywords in my list using a VBA program. The code I've ...

What is the best way to adjust the width of a column grid header in extjs

When adjusting the width of a vertical header in a grid to 50px, the header text disappears unless manually dragged to the left. How can I ensure the header text remains visible even with a smaller header size? Consider the following code snippet: { text ...

jQuery selector unable to locate the specified class in the table row

After receiving an array of strings as a response from an SQL query, I am using JavaScript to dynamically add this data as rows to an HTML table: loadUsers: function() { . . . function displayUsersOnTable(response) { for(var i = ...

What is the best way to create a button that can show or hide an unordered list with a click?

This is where you can find my special button: <body> <h3 class="paragraph">To remove duplicates in 2 Javascript arrays (refer to the readme), combine the results into a new array and display the unique names in an unordered list below ...

Implement various actions when the window is resized

The carousel code I have found returns a different number of items to display based on screen size. You can check it out here. $(function() { var mini = 1000; var big = 1280; if (window.screen.availWidth < mini) { $('#carousel1').jsCar ...

A crisscrossing dashed design running along the edges of the text block

<div class="search"> <p>SEARCH</p> </div> https://i.sstatic.net/yxOga.png I am attempting to incorporate a dashed lateral padding similar to the one shown in the image, but only on the sides of the text. Is there a method I can ...

Is there a reason the hr tag elements aren't extending to the full width within the list?

I am having trouble with my hr tag elements in the table I created - they are not spanning the full width like the line above them. I have tried adjusting the width property but it doesn't seem to be working. Can anyone offer advice or guidance on how ...

dividing Handlebars HTML content into different files or sections

I am looking to design a single route webpage, but I would like to divide the HTML code into multiple files. When rendering this particular route, my goal is for the rendered file to pull content from different template files and combine them into one coh ...

display a dual-column list using ngFor in Angular

I encountered a situation where I needed to display data from an object response in 2 columns. The catch is that the number of items in the data can vary, with odd and even numbers. To illustrate, let's assume I have 5 data items to display using 2 co ...

"JQuery event handlers not functioning as expected: .click and .on failing

For some time now, I've been facing this issue and I'm at a loss trying to figure it out. I have attempted various solutions such as using .click(), .on(), .delegate, and even utilizing .find() to locate the specific element causing the problem. ...

Tips for creating a div that slides from the right side to the left side

I received a code from someone and I need help with making the sliding div slide from right to left. Essentially, I want the div to hide on the right side and slowly move to the left within a width of 300px. Here is the HTML code: <a id="loginPanel"&g ...

What is the best way to conceal a CSS div through PHP if the div is empty?

I need some help refining my previous question to ensure clarity. Is there a straightforward method for hiding a div when it doesn't contain any information, without causing confusion for the client? This specific div receives information from Jooml ...

I believe the term for this is an inline text alignment: center, but for some reason, I am unable to make it function properly within

Can anyone help me align the "Sizes" section in the center? I've tried everything but can't seem to get it right. Any tips on what and where to insert in the code? Thanks! <form action="https://www.paypal.com/cgi-bin/webscr" method="post" ta ...

Optimize your HTML with Meleze.web's ASP.NET MVC 3 Razor minification and compression features

I came across meleze.web on NuGet, which claims to remove extra white spaces in generated HTML results. However, after adding the necessary configuration to my web.config file and running the application, I have not seen any changes in the result. Are th ...