CSS tip - A clever way to hide a table row if the row above it is empty

I have a table in HTML structured like this:

<tbody>
    <tr class="grid_row_content">
        <td class="gridCell">
            <div class="componentswitcher">
            <div style="display:none;"></div>
            </div>
        </td>
    </tr>
    <tr class="grid_row_spacer"></tr>
    <tr class="grid_row_content">
        <td class="gridCell">
            <div class="componentswitcher">
            <div style="display:none;"></div>
            </div>
        </td>
    </tr><tr class="grid_row_spacer"></tr>
</tbody>

For the spacing between two tr elements, I am using a tr class "grid_row_spacer" with CSS {height:4px}. It retains its height even when the tr above it is empty and has no height. However, I need a CSS solution for "grid_row_spacer" that will adjust its height based on whether the preceding tr element is empty or not.

Your suggestions are welcome.

Answer №1

You have the ability to adjust an element's appearance based on whether it is empty or not using a CSS selector called :empty. This method specifically targets elements that do not contain any children. Check out this example on how it works: https://jsfiddle.net/entio/rmyqf0jo/1/

If you want to modify the structure of a table based on its content, one approach is to add a class to empty elements like this:

<tr class="grid_row_content empty">
    ...
</tr>
<tr class="grid_row_spacer"></tr>

Then, you can apply styles with just

.grid_row_content.empty + .grid_row_spacer { display: none; }
.

Unfortunately, achieving this functionality solely with CSS may not be possible.

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

Using JavaScript to control the state of a button: enable or

In the process of creating a basic HTML application to collect customer information and store it in a database, I have encountered a specific user interface challenge. Once the user logs into their profile, they should see three buttons. Button 1 = Print ...

Guide to creating a List from a Table and its rows in Dart

I am looking for a way to extract data from an HTML Table to populate a list of Objects. Consider the following class: class Employee { String name; String department; num salary; ...methods } And in my HTML, there is a table structured like thi ...

Dynamic Filtering with jQuery List

I'm attempting to create a dynamic filter list on keypress event. For example, if I type "It" into the input field, the elements that do not match this value will be hidden. I'm unsure if the code structure I have implemented below effectively ac ...

In certain cases, webkit browsers may conceal LI A (display:block) elements

I have created a unique menu design that is based on the classic 'ul li' structure, but with 'a' elements set to display:block for precise positioning and sizing. There is also some transform:rotate applied, but this does not affect the ...

Utilizing _.where() in underscore to perform case-insensitive value comparisons

I need help with a webpage feature that allows users to search for specific values in a file. However, I want the search functionality to be case-insensitive while keeping the original case sensitivity of the data in the file. Currently, I am using an und ...

There is a lack of CSS import in the HTML document

I have organized my files in the following structure: - index.html - css/styles.css This is the HTML code I am using: <!DOCTYPE html> <html lang="en> <head> <title>Bootstrap Case</title> <meta charset="utf-8"> & ...

Can the `resize` CSS property be applied to the `body` element in HTML?

Check out my website which currently has a fixed max-width. I am interested in enhancing the user experience by adding a draggable border to the sides, allowing users to adjust the width based on their preference. Many online suggestions involve complicate ...

"PHP: Are you sure you want to delete? Confirm your

I'm trying to add a delete account button on my website, but I'm struggling with the implementation. Here's what I have so far: Javascript: <script language = "JavaScript" > function deleteUser(id) { if (confirm ...

Enhance the responsiveness of a ReactJS landing page

We have developed a large React application without relying on any existing UI framework. All of our UI components have been custom-built. Now, we are looking to make the landing page section responsive within the application, which will require the imple ...

Steps to shorten HTML content while maintaining consistent column width

Is there a way to ensure that the fixed column width is maintained even with long strings? I am trying to display a report in a jsp by having headers in one table and content in another table, both with columns set to the same width. However, some of the ...

Is there a way to align text in the middle while having different icons on each side?

Is there a way to center align the text in this image with minimal use of CSS/HTML? The icons on the left are causing it to be off center: https://i.sstatic.net/TKOYG.png Below is the relevant HTML and CSS for this top section: <div class="navbarhead ...

Leverage the power of Next.js to dynamically render a specific section of an HTML webpage

I want to incorporate Next.js into a specific section of my website, while the rest of the site is built using different frameworks that are not even based on React. I aim to use Next.js within a <div id="nextjs_div">...</div> element ...

Automating Web Form Completion with VBA and Selenium

I am experiencing an issue with filling out a form. I have attempted the following methods: driver.FindElementByXPath("//div[@id='s_dane_dokumentu-section?grid-1-grid?c_nr_lrn_komunikatu-control?xforms-input-1']").sendkeys "2112345 ...

Steps to activate a single button within a deactivated fieldset

Is there a way to deactivate all elements within a Fieldset, while keeping certain buttons active? Check out this demo. <fieldset ng-disabled="true"> <legend>Personalia:</legend> Name: <input type="text"><br> Em ...

Review a roster of websites every half a minute (Revise pages every half an hour??)

Just starting out with HTML coding! Can someone please provide the code that will allow me to save various webpages and automatically cycle through them every 30 seconds? And also ensure that the webpages are updated every 30 minutes. ...

Button in HTML not responsive to clicks

I'm facing an issue with my HTML button as it's not clickable at all. I suspect it may be related to incorrect div settings or a background issue. Can someone advise on what setting needs to be changed for the "sign the petition" link to become ...

Displaying images in Dash Python by utilizing external Javascript

Dear audience, I am looking to incorporate an image on a Python Dash webpage that is created by JavaScript code. For more details, you can refer to the documentation here: . To include this script in a static HTML page, one would typically add the followi ...

Upon clicking on another div, command it to expand to full height and width

I am encountering an issue when loading an HTML file into a content div after clicking on a menu bar option. The HTML file contains 5 tables, but the div only displays the first 5 lines of the first table before requiring a scroll bar to view the rest of t ...

Utilizing Jquery's Selectable IDs in a repetitive manner

I'm a newbie to jQuery and I'm attempting to create two lists where each item can be selected. Check out my code: <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI s ...

Utilizing a JavaScript Function on an HTML Page Through an External JavaScript File

I am facing a challenge with JavaScript functions in my HTML page. I have one function that returns a value, and I need to catch this value in another JavaScript function located in an external file linked to the HTML page. Can someone guide me on how to a ...