What is the best way to target a nested selector in CSS?

To ensure the entire cell is filled when a modal is present, the padding of the table should be zero. See image https://i.sstatic.net/Qq2T4.png

How can I target the specific selector nested within the table under different classes and IDs?

I've experimented with various combinations like: #tabletu .modal #myBtn2 td, but none have successfully removed the padding. For instance:

// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn2");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
#tabletu .modal {
  padding: 0px;
}

#tabletu {
  background-color: transparent;
  border-collapse: collapse;
  width: 100%;
}

#tabletu td,
th {
  border: 1px solid #000000;
  padding: 8px;
}

#tabletu th {
  padding: 8px;
  text-align: left;
  border: 1px solid #000000;
  background-color: rgba(0, 110, 167, 1);
  color: white;
}


/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

.modal td {
  padding: 0px;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.notebook {
  border-collapse: collapse;
  width: 70%;
}

.notebook td:hover {
  background-color: #ddd;
}

.notebook td,
.notebook th {
  border: 1px solid #ddd;
  padding: 0px;
}

.block {
  display: block;
  width: 100%;
  border: none;
  background-color: rgba(0, 166, 214, 1);
  padding: 14px 28px;
  font-size: 16px;
  cursor: pointer;
  text-align: center;
  box-sizing: border-box;
}

.block:hover {
  background-color: rgba(0, 166, 214, 0.78);
  color: black;
}

.month {
  text-align: center;
}

#category {
  padding: 8px;
}
<h2>Experiment Notebook</h2>
<center>
  <table id="tabletu" class="notebook">
    <tr id="category">
      <th></th>
      <th id="category">Test1</th>
      <th>Test2</th>
      <th>Other</th>
    </tr>
    <tr>
      <td rowspan=2 class="month">April</td>
      <td>
        <!-- Trigger/Open The Modal -->
        <button id="myBtn2" class="block">Open Modal</button>

        <!-- The Modal -->
        <div id="myModal" class="modal">

          <!-- Modal content -->
          <div class="modal-content">
            <span class="close">&times;</span>
            <p>Some text in the Modal..</p>
          </div>

        </div>
      </td>
      <td><button id="myBtn2" class="block">Open Modal</button></td>
      <td>Germany</td>
    </tr>
    <tr>

      <td>Berglunds snabbköp</td>
      <td>Christina Berglund</td>
      <td>Sweden</td>
    </tr>
    <tr>
      <td></td>
      <td>Centro comercial Moctezuma</td>
      <td>Francisco Chang</td>
      <td>Mexico</td>
    </tr>
    <tr>
      <td></td>
      <td>Ernst Handel</td>
      <td>Roland Mendel</td>
      <td>Austria</td>
    </tr>
    <tr>
      <td></td>
      <td>Island Trading</td>
      <td>Helen Bennett</td>
      <td>UK</td>
    </tr>
    <tr>
      <td></td>
      <td>Königlich Essen</td>
      <td>Philip Cramer</td>
      <td>Germany</td>
    </tr>
    <tr>
      <td></td>
      <td>Laughing Bacchus Winecellars</td>
      <td>Yoshi Tannamuri</td>
      <td>Canada</td>
    </tr>
    <tr>
      <td></td>
      <td>Magazzini Alimentari Riuniti</td>
      <td>Giovanni Rovelli</td>
      <td>Italy</td>
    </tr>
    <tr>
      <td></td>
      <td>North/South</td>
      <td>Simon Crowther</td>
      <td>UK</td>
    </tr>
    <tr>
      <td></td>
      <td>Paris spécialités</td>
      <td>Marie Bertrand</td>
      <td>France</td>
    </tr>
  </table>

</center>

Thanks to A. Meshu for assistance so far, but there's still a small white line appearing below the modal. https://i.sstatic.net/3wBFs.png

Answer №1

To address this issue, you can apply a specific class to all the <td> elements containing buttons and then override their default styling using the !important rule. Here, I have created a class called modalTD:

.modalTD {padding: 0!important;} // removes horizontal padding
.modalTD button {height: 65px!important;} // adjusts button height to fill the entire cell

The corresponding HTML code would look like this:

.
.
.
<td class="modalTD"><!-- Trigger/Open The Modal -->
  <button id="myBtn2" class="block">Open Modal</button>
.
.
.

As a side note, in the future, the :has pseudo-class may simplify tasks like these, although it is not widely supported at the moment.

EDIT

Alternatively, a more efficient solution that affects the entire row (which may not be visually problematic) is:

#tabletu tr:nth-of-type(2) td {padding: 0;}

Answer №2

Upon clicking the modal open, a modal-open class will be added to the cell, which applies 0px padding. When closing the modal, the class is removed, thus removing the padding as well.

        #tabletu {
            background-color: transparent;
            border-collapse: collapse;
            width:100%;
        }

        #tabletu td, th {
            border: 1px solid #000000;
            padding: 8px;
        }

        #tabletu th {
            padding: 8px;
            text-align: left;
            border: 1px solid #000000;  
            background-color: rgba(0,110,167,1);
            color: white;
        }
        /* The Modal (background) */
        .modal {
            display: none; /* Hidden by default */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            padding-top: 100px; /* Location of the box */
            left: 0;
            top: 0;
            width: 100%; /* Full width */
            height: 100%; /* Full height */
            overflow: auto; /* Enable scroll if needed */
            background-color: rgb(0,0,0); /* Fallback color */
            background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
        }

        .btn-cell {
          padding: 8px;
        }
        .btn-cell.modal-open {
          padding: 0px!important;
        }
        /* Modal Content */
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }


        .modal td {
            padding:0px;
        }
        /* The Close Button */
        .close {
            color: #aaaaaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }

        .notebook {  
            border-collapse: collapse;
            width:70%;}
        .notebook td:hover {background-color: #ddd;}
        .notebook td, .notebook th {
            border: 1px solid #ddd;
            padding: 0px;
        }
        .block {
            display: block;
            width: 100%;
            border: none;
            background-color: rgba(0, 166, 214, 1);
            padding: 14px 28px;
            font-size: 16px;
            cursor: pointer;
            text-align: center;
            box-sizing: border-box;
        }

        .block:hover {
            background-color: rgba(0, 166, 214, 0.78);
            color: black;
        }
        .month {
            text-align: center;
        }        
        #category {
            padding:8px;
        }
    </style>
</head>
<body>

    <h2>Experiment Notebook</h2>
    <center>
        <table id="tabletu" class="notebook">
            <tr id="category">
                <th ></th>
                <th id="category">Test1</th>
                <th>Test2</th>
                <th>Other</th>
            </tr>
            <tr>
                <td rowspan=2 class="month">April</td>
                <td class='btn-cell'><!-- Trigger/Open The Modal -->
                    <button id="myBtn2" class="block ">Open Modal</button>

                    <!-- The Modal -->
                    <div id="myModal" class="modal">

                        <!-- Modal content -->
                        <div class="modal-content">
                            <span class="close">&times;</span>
                            <p>Some text in the Modal..</p>
                        </div>

                    </div>
                </td>
                <td class='btn-cell'><button id="myBtn2" class="block ">Open Modal</button></td>
                <td>Germany</td>
            </tr>
            <tr>

                <td>Berglunds snabbköp</td>
                <td>Christina Berglund</td>
                <td>Sweden</td>
            </tr>
            <tr>
                <td></td>
                <td>Centro comercial Moctezuma</td>
                <td>Francisco Chang</td>
                <td>Mexico</td>
            </tr>   
            ...
            
        </table>

    </center>



    <script>
        // Get the modal
        var modal = document.getElementById("myModal");

        // Get the button that opens the modal
        var btn = document.getElementById("myBtn2");

        // Get the <span> element that closes the modal
        var span = document.getElementsByClassName("close")[0];

        // When the user clicks the button, open the modal 
        btn.onclick = function() {
            modal.style.display = "block";
            togglePadding(true)
        }

        // When the user clicks on <span> (x), close the modal
        span.onclick = function() {
            modal.style.display = "none";
            togglePadding()
        }

        // When the user clicks anywhere outside of the modal, close it
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
                togglePadding()
            }
        }

        var togglePadding = function(add) {
          var buttonCells = document.getElementsByClassName('btn-cell')
            for (var i = 0; i < buttonCells.length; i++) {
              if (add) {
                buttonCells[i].classList.add('modal-open')
              }
              else {
                buttonCells[i].classList.remove('modal-open')
              }
            }
        }

    </script>

Answer №3

Utilizing an id selector on the parent element #tabletu td takes precedence over the class selector .modal td. To override the id selector, simply apply !important:

.modal td {padding:0} !important

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

perfecting the animation of popovers

When I click on the button, a popover appears. However, the pointer of the popover seems to be coming from a different direction. I have been struggling to correct this issue. Any suggestions? .popover { position: absolute; top: 50px; left: -175px ...

BorderWoocommerce

Something odd has appeared around my shopping cart total ("WARENKORB SUMME") today. Could you please take a look at the screenshot below? Any idea how to remove it? This is for a Wordpress/WooCommerce store. I haven't made any recent changes - I su ...

Difficulty in making Materialize.css column match the height of the entire row

Utilizing React.js, Materialize.css, and certain MaterialUI components in collaboration to build a website. Developed a header (React component, basic div with title and logout button) for the site using a Materialize.css "grid". Issue: The react compone ...

jQuery - patience is required until all elements have completely faded away

I am facing a unique challenge: I need to trigger an action after two specific elements have been faded out simultaneously. The code snippet for this task is as follows: $("#publish-left, #publish-right, #print-run").fadeOut(function(){ //do something ...

Leveraging Bootstrap column classes with diverse div heights

Currently, I am working on a layout design using Bootstrap 3 that involves eight text divs. These divs have almost the same height, but some are slightly taller depending on the screen width. My goal is to arrange them into three columns on desktop and th ...

Incomplete Website Encryption Alert

Currently, I am developing a website called "usborroe.com" and have just set up a GeoTrust Business ID SSL Certificate. However, despite my efforts to ensure full encryption on the site, an error message is indicating that it is not fully encrypted and t ...

Improving Storage of Message Data in MySQL with PHP

I'm in the process of developing a website featuring a messaging system for users. To store the necessary data, I am utilizing MySQL. The structure of the inbox table is outlined below: `mid` INT UNSIGNED NOT NULL AUTO_INCREMENT , `to_uid` INT UN ...

Removing a background by linking a CSS file

Issue Resolved: I have found the solution. The problem was that the css document still contained html tags. Thank you to everyone who provided assistance. While designing a website using css, I encountered a situation where the site worked perfectly when ...

Step-by-step guide on clicking an href link with Selenium

My code contains an HTML href link that I want to click using Selenium. The current code I have been trying to use is as follows: <a href="/docs/configuration">App Configuration</a> Despite my attempts, the link does not redirect to ...

Using JQuery to Execute Matching Based on Text Within <A> Elements

After reviewing the resources on Jquery Extract URL from Text and jquery match() variable interpolation - complex regexes, I am still a bit confused. The issue at hand is that I have a webpage with a dropdown menu at the top. Inside the dropdown, there ...

Navigate through information within designated boundaries

In order to achieve vertical scrolling of a sidebar div and its content (3 links) between two points, I have utilized the CSS property position:fixed; top: 100px. While this setup works well initially by positioning the sidebar 100px down from the top and ...

Initiating a node request within an HTML document

I have recently started learning about node and have been practicing making http requests using the request module. In my current script, I am attempting to have a callback function execute a request that fetches HTML from a webpage and then filters it to ...

The HTML content retrieved through an ajax service call may appear distorted momentarily until the JavaScript and CSS styles are fully applied

When making a service call using ajax and loading an HTML content on my page, the content includes text, external script calls, and CSS. However, upon loading, the HTML appears distorted for a few seconds before the JS and CSS are applied. Is there a sol ...

Creating a website that can be customized with different themes

Currently, I am working on a project to design a website where users can easily customize themes, focusing mainly on colors, fonts, and other styling aspects without altering the layout. My plan is to create a class called "theme1" on the body element and ...

Utilize HTML5, CSS, and Responsive Web Design to place four boxes into a div container

Is there a way to arrange four boxes inside a div so that they are positioned at the corners of the top-left, top-right, bottom-left, and bottom-right? And when you click on the box in the middle, a window opens with text. I'm looking for something s ...

Steps to adding a collection of links (stylesheets, javascript files) to each html document

Is it feasible to add a list of links to multiple HTML files? I was inspired by W3 School's W3 Include functionality, which allows you to import blocks of HTML code into various files simultaneously, making it convenient for making changes across many ...

Having trouble getting the HTML5 Canvas element to display on iOS Chrome or Safari?

I have developed a website that utilizes a user's webcam frames through an HTML <video> element. These frames are then copied to a <canvas> element, which is displayed using opencv.js' cv.imshow(). Additionally, the frame is duplicate ...

Personalize the hover effect of CardActionArea

I'm attempting to personalize the hover effect of material-ui's CardActionArea for a specific component in my React application. Unfortunately, I am struggling to locate detailed documentation on how to style it effectively. Even after experimen ...

Show [image not available] if image src does not display an image

When creating my image's src, I pull the data from a database field called [pictureName]. If the Picture name is null or does not return an image, I want to display the [image not found] picture. Below is the ASPX code used to generate the img: < ...

Display "No results found" on the auto-complete search list when the specified element does not exist

If the element does not exist in the "ul list," I want my search code (autocomplete on an input field) to display a message like "no match found" as a div tag or some other form of notification. How can I achieve this? Do I need to implement a different ...