Collapsing margins within table elements

There are multiple tables on a single page that need to be separated by some space. However, some of the tables have empty cells, resulting in zero height, and I do not want any extra spacing between them.

Although margin collapsing is typically an effective method for adding space, tables establish a block formatting context (BCF), which prevents margin collapsing between boxes that establish a BCF.

An example can be seen with the snippet below: there is 1em space between "1" and "2", but 2em space between "2" and "3. Ideally, I would always like to maintain a consistent 1em space. How can this be achieved?

Note: There are certain limitations to consider - it is not possible to remove markup from the empty tables or add specific classes to them. However, a container could be added for each table, such as a <div>. Initially, moving the margin-bottom to that container was attempted, but it did not work due to the parent elements also establishing a BFC. Alternatively, JavaScript could be used to dynamically add a class with margins to non-empty tables, although this solution may not be preferred.

table { margin-bottom: 1em }
<table><tr><td>1</td></tr></table>
<table><tr><td>2</td></tr></table>
<table><tr><td></td></tr></table>
<table><tr><td>3</td></tr></table>

Answer №1

Fascinating inquiry. Definitely a challenging one. How about trying out this solution for size: https://codepen.io/WDYA/pen/eJKLpI? The spacer is shaded in grey to illustrate the concept:

table {  
  border-spacing: 0;
}

td {
  border: 1px solid grey;
}

td:empty {
  display: none;
}

td:not(:empty) {
  &:after {
    content: '';
    display: block;
    height: 30px;
    width: 10px;
    background-color: rgba(55,78,22,0.5);
  }
}

Since your example consists of only one cell per table, this method should work perfectly.

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

Create interactive elements (such as circles, rectangles, and other shapes) that can be moved around the screen, all

Currently, I am developing a PHP web application for restaurant management. One of the key features I am working on is creating a floor plan layout of tables. This will allow the admin to easily drag and drop tables from the left side onto the canvas wher ...

The CSS styling of Confluence content is stripped when exporting to Word

Greetings StackOverflow community, I have embarked on creating a document generator within Confluence 6.10.2 (utilizing JQuery 1.7.2) that offers a range of options for the user. Users are able to interact with checkboxes or radio buttons that trigger th ...

Using a combination of jQuery and JavaScript to switch image tags between different classes

I'm really struggling with this issue and could use some guidance. Essentially, I have a code that should change the class of an img tag when a user clicks on a div element. Let's take a look at the HTML structure: <li><img src ...

How can I add information into a nested div element?

I encountered an issue when attempting to insert data into my block. The div structure is as follows: <div class="1"> <div class="2"> <div class="3"> <div class="4"></div> </div> ...

Is it possible to enable the position:sticky property to function within an iframe by synchronizing it with the scrolling of the parent document

One challenge I'm facing is ensuring that the <footer> within an <iframe> remains sticky at the bottom, even when the <iframe> doesn't have a scrollbar. This should be based on the scroll behavior of the parent document. Import ...

Console command to change paragraph tag color---Modifying the color

I am attempting to change the color of all paragraph tags on a webpage to white by utilizing the console. My initial attempt was: document.p.style.color = 'white'; However, this method did not have the desired effect. Interestingly, I have had s ...

Tips for aligning text to the left instead of the right when it exceeds container width in IE 11

Within the div, there is text with a 5px margin on the right. When the container div narrows, the text overflows from the container and loses the right margin. Is it feasible to maintain this margin on the right side while allowing the text to overflow fro ...

Utilize the qtip function in a loop using the .each method

I'm encountering an issue with implementing the Qtip jQuery plugin across a whole class of HTML elements: $(".peoplebtn").each(function() { $(this).qtip({ content: { text: $(this).attr('qTipText') } }); }); ...

What happens when browsers encounter unfamiliar attributes in HTML code

Direct and to the point: if I were to execute: <div id = "thisid" class = "a plethora of classes" mother = "your mother" levelofhotness = "over 9000"> I am aware that Chrome and Firefox (the browsers I typically test with) simply disregard these a ...

Could it be an issue with Jquery? Maybe it's related to AJAX reload, or possibly just a case

Utilizing the following Jquery : $('#resultstable').on('click', 'tbody tr', function() { alert("helloworld") $('#resultstable tr.Selected').removeClass('Selected'); $(this).addClass('Selected'); ...

Cease and Begin Page Overflow / Scroll with a Single Click Feature

Background: I recently followed a tutorial on how to create a lightbox using HTML, CSS, and JavaScript. However, I encountered an issue where I wanted to disable the scrollbar functionality when the lightbox is displayed, preventing users from scrolling ...

What is the best way to adjust the minimum width of the HTML5 progress element?

There is a similar question which addresses input elements having a minimum width determined by the size parameter. I am attempting to create a layout with an inline-flex element with a flex-direction: column property like this: .item { border: 1px s ...

Is there a way to access data from a database using ajax and php?

I am attempting to verify if a field exists in the database before submitting a form. To achieve this, I have added the keyup event to the field to fetch data from the db using ajax. Below is the code snippet that I have added where the form is: $(docum ...

Increased pixels being incorporated into my line spacing

Looking for a solution: A <div> containing only one word at a font size of 16px with a line-height set to 1. Encountering issues with extra space above the text in Chrome and Firefox, while the text bleeds slightly out of the bottom. IE exacerbates t ...

Tips for rotating and positioning an element in the top left or top right corner of a page

I recently tried rotating a div with text and wanted to position it at the top left corner. I was able to successfully align it at the top, but I'm having trouble getting it to align with the left edge. Any tips on how to achieve this? .credit { ...

The value for the specified date and time is not appearing on the

When using the Bootstrap DatetimePicker, I'm able to select a date and time. However, after clicking on a date, it does not prompt me for the time until I click on the clock icon. Another issue arises when attempting to display the stored date and ti ...

What are the different ways in which :style is utilized in re-frame - with brackets, curly brackets, or simply without any characters?

For my current project, I am utilizing Clojure, ClojureScript, lein, shadow-cljs, Emacs, and CIDER to develop a dynamic web application. Typically, when building the project, I initiate the command cider-jack-in-cljs in Emacs, select shadow-cljs, opt for ...

Trouble Arising from Regional Settings in my Hybrid App Developed with CapacitorJS and Vue.js

Issue with capacitor app input Correct input in chrome app I'm facing a problem related to regional settings in my mobile application, developed using CapacitorJS and Vue.js 2. The datetime-local control is appearing in a language that isn't the ...

React - Updating the document title upon user navigation away from the current page

I have encountered a challenge and I am seeking guidance on how to resolve it. Throughout our website, all pages have a default title based on our index.html file: <html lang="en"> <head> <title>My Page Title</title> ...

How can I display an image caption within a lightbox using jQuery?

There is a caption under each image. <p> <a href="large.jpg"> <img width="85" height="75" src="thumb.jpg/> </a>Caption</p> I am looking to display the same caption at the bottom of the lightbox. <span id="lightbox-image ...