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

Intercepting 401 with Angular 5 HTTP Interceptor recognizes it as status code 0

One issue I am currently facing is the inability to intercept a 401 status and redirect to the login page, which is a common practice in most applications. My interceptor code seems pretty standard: intercept(request: HttpRequest<any&g ...

methods for transferring javascript variables to modal

<div> <h5> <a href="<?php echo base_url(); ?>/vendor/home/projects" >Return to Projects</a> </h5> <div> <div class="container"> <div class="row"> ...

Enhance your website with dynamic effects by incorporating CSS3 columns and responsive layouts. Utilize CSS3 or jQuery/JavaScript to

I have decided to create a masonry style layout using CSS3 for the columns, which has made building the layout easier and more precise. This is especially useful as I am working on a responsive design. Currently, I am implementing CSS3 media queries to ad ...

What crucial element am I overlooking in the React Transition Group Component while implementing it for a carousel design?

I'm trying to add a feature to my Carousel where the opacity changes between images. When clicking on the icons, the images should transition smoothly. I've been using React Transition Group, but for some reason, it's not working as expected ...

Having trouble displaying a gray background/overlay in a Twitter Bootstrap modal design

Library version: Jquery 1.11.0 Framework version: Bootstrap 3.1.1 CodePen link: https://codepen.io/amitshahc/pen/r6ueupfj/1/ HTML snippet: <!-- Modal --> <div class="modal fade" id="az-progress" tabindex="-1" role="dialog" aria-labelledby= ...

Create a stylish slide-in menu using CSS, triggered by an onclick event. No JavaScript required!

I implemented a hamburger menu that slides in when clicked. However, I'm facing an issue where the slide-in menu disappears after a second. How can I make sure the menu stays visible? #navigationWrap { position: fixed; top: 0; ...

Enable automatic indentation for wrapped text

I'm still learning the ropes here, but I'm looking to have text automatically indent when it's wrapped. Instead of this: Peter piper picked a peck of pickled peppers. It should look like this: Peter piper picked a peck of pickled pepp ...

The upper margin is ineffective

Apologies, I am new to CSS. Here is the HTML code I have: <div class="box-A" >Box A content goes here</div> <div class="box-B" >Box B content goes here</div> I attempted to apply the following CSS to it: .box-A{ background-c ...

I cannot locate the dropdown list anywhere

As a beginner in html and css, I decided to follow a tutorial on youtube. The tutorial focuses on creating a navigation bar with dropdown menus using html and css. I wanted the names Ria, Kezia, and Gelia to be displayed when hovering my mouse over the Su ...

The dimensions of my textarea and input field are different

Just starting out with HTML and CSS here. I'm trying to use flexbox to create a form, but the width of my input fields and text area is off for some reason. Any ideas on what might be missing from my code? Thanks in advance! Here's the CSS I&apo ...

Utilizing CSS to Implement a Background Image

Here is where I am looking to upload the image. Ideally, I would like to position my image to the left side of the text related to Food and Travel. You can view the image here. .block-title h3 { color: #151515; font-family: 'Montserrat', s ...

Enhancing the appearance of the CSS panel with Font Awesome icons

I need help adding a fontawesome or any other icon to my CSS3 based sliding panel button. The icon should change from open to close when clicked on. Any ideas on how to achieve this would be greatly appreciated! * { margin:0; padding:0; font-famil ...

`Can a creation of this nature be accomplished?`

In my text input field, users can type messages to send to me. I'd like to add buttons on the page with symbols like "!", "XD", and "#". When someone clicks on a button, such as the "!" button, that corresponding symbol should be inserted into the tex ...

The MIME type 'text/html' is incompatible with stylesheet MIME type and is not supported

I have exhausted all possible solutions for the issue, from specifying the type for <link rel="stylesheet" href="./style.css" /> to using app.use(express.static('public')), but unfortunately, none of them seem to resolve the problem. index ...

A layout featuring two columns that have heights which are fixed to the bottom of the footer

I've designed a simple layout featuring 2 columns and a footer that adjusts based on the height of the longer column. <div id="holder"> <nav class="navbar navbar-default navbar-fixed-top"></nav> <div class="container"> ...

Glowing CSS animation surrounding a PNG image

.zuphologo { padding-top: 33%; padding-bottom: 2%; } .fx { box-shadow: 0px 0px 100px 4px #fff; animation: glow 1.5s linear infinite alternate; } @keyframes glow{ to { box-shadow: 0px 0px 30px 20px #fff; } } <div class="container"> ...

Tips on displaying hyperlinks within a text area in an Angular application

In my Angular application, I am facing an issue with displaying hyperlinks within a text area box for dynamic content. The hyperlinks are not recognized and therefore cannot be clicked. Can someone please advise on how to properly display hyperlinks with ...

Arrange the child divs on top of the parent div

<div id="1"> <div id="2"> </div> </div> It is proving challenging to position div2 above div1 in the vertical dimension. Applying a negative top position does not have the desired effect, as it appears that the top border of the ...

Choosing an Option from Drop-down Menu in Materialize CSS with Selenium in C#

I am attempting to choose an item from a dropdown field on a page that is styled using Materialize CSS. I am currently working with Selenium webdriver in C#. Here is my most recent attempt: IWebElement tipoLoja = driver.FindElement(By.XPath("//select[@id ...

Establishing the footer within dompdf

This snippet demonstrates the specified behavior (jsfiddle). <!--Footer--> <div id="footer"> <span class="alignleft">$unique_ID</span> <span class="alignright"> Page <span class="pagenum"></span></span> < ...