Utilizing the CSS :not() selector within a nested table

I am having an issue with my AjaxControlToolkit where the main-page CSS is affecting the inner "calendar" popup.

To illustrate what I need, what I've tried, and the temporary workaround, I have simplified everything into a one-page example. I am in search of a proper CSS solution.

Below is all the information you need:

table.my_table_padding {border-collapse: collapse;}

/* starting point (looks a mess) */
/* table.my_table_padding tr td {padding: 20px;background-color: greenyellow;} */

/* My attempts (neither worked) */ 
/* :not(.ajax_calendar) table.my_table_padding tr td {padding: 20px;background-color: orange;} */ 
/* :not(div.ajax_calendar) table.my_table_padding tr td {padding: 20px;background-color: orange;} */ 

/* The workaround that is a mess and a horrid bodge - is there a better way in a single class, using a :not() ? */
table.my_table_padding tr td {padding: 20px;background-color: greenyellow;}
div.ajax__calendar table.my_table_padding tr td {padding: 0;background-color: white;} 
<table border="1" class="my_table_padding">
  <tr>
    <td>Header-text</td>
    <td>Sample Text... Sample Text...<br />Text is here</td>
  </tr>
  <tr>
    <td>Header-text</td>
    <td>
      <div class="ajax__calendar">
        <!-- Actually using a popup nuget:AjaxControlToolkit, this is an example -->
        See: <a href="https://ajaxcontroltoolkit.devexpress.com/Calendar/Calendar.aspx">AjaxControlToolkit Sample Page</a>
        <table border="1" class="my_table_padding">
          <tr><td colspan="7">Popup calendar-control</td></tr>
          <tr><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>
          <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
          <tr><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td></tr>
        </table>
      </div>
    </td>
  </tr>
</table>

EDIT 1 (to clarify what I'm after)

I have discovered a cumbersome method to achieve the desired display outcome: 1 - implementing the original CSS rule [table.my_table_padding tr td] 2 - Including another CSS rule underneath it [div.ajax__calendar table.my_table_padding tr td] that counteracts the changes made by the first rule

However, this approach seems messy. I am pondering if there is a cleaner way of articulating the original (CSS rule 1), so that it selects all tables except for those that are children of the "div class=ajax__calendar" element.

EDIT 2 - The original ASP.NET Webforms code

<asp:DetailsView runat="server" ID="detv_Main" CssClass="my_table_padding detv_example">
 <Fields>
  <asp:TemplateField HeaderText="Start Date" SortExpression="EventStartDate">
   <EditItemTemplate>
     <asp:TextBox ID="txt_EventStartDate" runat="server" Text='<%# Bind(example) %>' CssClass="edit_background"></asp:TextBox>
     <ajaxToolkit:CalendarExtender ID="calext_EventStartDate" TargetControlID="txt_EventStartDate" runat="server" Format="d MMM yyyy" />
  </EditItemTemplate>
  <ItemTemplate>Example date</ItemTemplate>
</asp:TemplateField>

Answer №1

When it comes to applying CSS specifically to the parent table only, you must utilize the child combinator (>) along with the :not. Without the child combinator, the inner table still follows the rule because its descendants (excluded by the :not) are also descendants of the outer table (not excluded by the :not).

In order to incorporate the child combinator with a table and tr, you need to include the implicit tbody. The revised selector would be:

:not(.ajax__calendar) table.my_table_padding > tbody > tr > td

This will achieve most of what you need. However, keep in mind that the default background color is transparent, so the inner table will inherit the background color of the outer table unless specified otherwise. You can set it directly on .ajax__calendar or on any descendant of the outer td where the background color is being set.

table.my_table_padding {border-collapse: collapse;}

:not(.ajax__calendar) > table.my_table_padding > tbody > tr > td {padding: 20px;background-color: greenyellow;}

/* This is necessary since the transparent background of the inner table would otherwise should the greenyellow of the outer table. This could be set at a different level as desired */
div.ajax__calendar {background-color: white;} 
<table border="1" class="my_table_padding">
  <tr>
    <td>Header-text</td>
    <td>Sample Text... Sample Text...<br />Text is here</td>
  </tr>
  <tr>
    <td>Header-text</td>
    <td>
      <div class="ajax__calendar">
        <!-- Actually using a popup nuget:AjaxControlToolkit, this is an example -->
        See: <a href="https://ajaxcontroltoolkit.devexpress.com/Calendar/Calendar.aspx">AjaxControlToolkit Sample Page</a>
        <table border="1" class="my_table_padding">
          <tr><td colspan="7">Popup calendar-control</td></tr>
          <tr><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>
          <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
          <tr><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td></tr>
        </table>
      </div>
    </td>
  </tr>
</table>

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

Discovering elements using CSS Selector with Selenium in Python

<button class="btn btn-primary" type="button" data-sku-id="4523156" style="padding:0 8px"> <svg aria-hidden="true" role="img" viewBox="0 0 100 100" style="width:16px;height:16px;margin-bottom:-2px;margin-right:9px;fill:currentColor"> < ...

Tips for discovering the exact positions of child divs that are Nth siblings within a parent div

I have designed a new calendar interface with dynamic functionality. When the user clicks on any time slot displayed in IMAGE1, a span element is created dynamically to represent 8 hours starting from that clicked time. My question is, how can I access th ...

Form-linked Progress Bar

This is a little project I created (for fun and learning purposes, even though it may not be the most optimized solution). If you're interested, here's the link to the code: https://codepen.io/paschos/pen/xxGXMQb I'm currently seeking assi ...

Align two DIVs in the correct layout: One as a sidebar that remains fixed, and the other as an expanding element

I am currently working on a code snippet to build a basic website featuring two main sections: a sidebar and the main content: html, body { height: 100%; margin: 0; } .container { display: flex; flex-direction: row; height: 100%; } .sidebar { ...

Bottom section of a multi-level website with horizontal dividers

I was struggling with aligning divs next to each other, but I managed to find a solution. The only issue is that the text now aligns to the right instead of going below as it should. Typically this wouldn't be an issue since all content is within the ...

Designing tables and image galleries using HTML, CSS, and jQuery: A guide

How can I easily transform tabular data into thumbnails with the click of a button? I need a 2-view system that switches between table and thumbnail views without including image thumbnails. I welcome any creative suggestions! :) Examples: ...

What is the best method for uncovering hidden elements using Selenium when using knockout.js?

I have been struggling to locate the "Logout" button within a user portal using knockout.js frontend and Selenium testing framework. Despite being able to access many elements by ID, the logout button remains elusive. Outdated resources online have not pro ...

Illuminate the current page

I have been working on a website with around 20 pages that all have a side navigation bar. To make it easier to manage, I decided to centralize the links in a JavaScript (JS) file and include it on each HTML page. This way, any changes can be made quickly ...

Tips for adding style to a group of SVG elements:

I currently have an array of .svg icons, each with unique properties that I need to customize: <svg width="24" height="24" viewBox="0 0 24 24"> ... </svg> import styled from 'styled-components'; import Github from 'assets/git ...

Tips for updating the color of the first td element in a table using a specific class

Below is the CSS code for my table: table.show-my-request-table { border: 1px solid black; margin-left:auto; margin-right:auto; border-collapse: collapse; } tr.show-my-request-table-header{ border: 1px solid black; } tr.show-my-requ ...

Stop the bootstrap navbar from resizing

I implemented a bootstrap navbar that looks like this... <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">WebSiteName</a> </div& ...

Achieve vertical alignment of a span text that spans multiple lines within an inline-block element

I am facing an issue with this code as the phrase "lorem ipsum" is not positioned in the center of its parent div: <div style="width: 500px; height: 500px; background-color: #f0f0f0"> <div style="display: inline-block; width: 100px; height: 1 ...

Elements shifting positions based on the size of the window

I am facing an issue with positioning the register form on my website. I want it to be fixed at the bottom-right corner of the window, without reaching the top. Although I managed to achieve this, the problem arises when the screen resolution changes. For ...

The CSS selector used in Selenium IDE may vary from the one copied directly from the browser's development tools

When it comes to selecting CSS selectors, Selenium IDE may sometimes generate a different code than what you see in the developer tools. To illustrate this, consider the following example page: For instance, the price selector recorded by Selenium IDE is ...

What causes the shift in the position of the div element?

I have been struggling with a problem in my code. Whenever I hover over this element, it rotates as intended but it also changes its position. I can't figure out why this is happening. Can someone please help me debug this? I would greatly appreciate ...

Are you looking for methods to alter the elements of a webpage prior to viewing it?

I have created a page with the intention of using it as a tool, but I am facing some challenges due to my limited experience in this field. As a newcomer, I am struggling to achieve certain goals: - My objective is to modify the values of an element on a p ...

Discovering the element with selenium-webdriver and Java: A step-by-step guide

What is the proper way to call xpath for a specific class? <button class="inline" data-ember-action="" data-ember-action-11310="11310">Load</button> I attempted to call it using: By.xpath("//span/button[text()='Load'][1]"); Unfort ...

Hovering over an element will change its background color along with adjusting the background

I encountered a situation with the following code: CSS: <style> div.testy { border:1px solid black; } div.testy:hover { background-color:red; } </style> HTML: <div class="testy" style="height:100px; back ...

Changing the appearance of a shadow root element using CSS styling

My CustomField has a FormLayout inside, and I want to change the #layout element within the shadow-root. While it can be done with JavaScript: document.querySelector("#myid > vaadin-form-layout") .shadowRoot .querySelector("#layout" ...

Are none of the page links clickable?

Currently, I am in the process of creating a portfolio website. This is my first attempt at coding HTML and CSS from scratch without the aid of a layout template. I've encountered an issue that has me stumped - the links within the container are not ...