0

I am using AjaxControlToolkit and the main-page CSS is also affecting the inner "calendar" popup.

I've reduced everything to a simple 1-page example showing what I want, what I've tried, and the "ugly workaround". I'm looking for a proper CSS solution.

All information is in the example below:

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)

Basically, I've found one ugly way to achieve the desired display results 1 - adding the original CSS-rule [table.my_table_padding tr td] 2 - Adding another CSS-rule underneath it [div.ajax__calendar table.my_table_padding tr td] that has the reverse-effect of all the changes in the first rule

but, this approach seems messy. I was wondering if there was a better way of phrasing the original (CSS rule 1), in such a way that it picked all tables other than 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>
Steven_W
  • 788
  • 1
  • 6
  • 15
  • Your question is pretty unclear. You want all of your CSS to NOT affect `
    `?
    – Thomas Bennett Sep 13 '19 at 21:10
  • Your attempts failed primarily because you're running the selector from the bottom up. That's not how CSS works. CSS works in a "cascade": from the top down. – Michael Benjamin Sep 13 '19 at 21:14
  • 1
    I don't think your problem is the `:not()` selector. It's actually your need for a parent selector in CSS, which doesn't exist. https://stackoverflow.com/q/1014861/3597276 – Michael Benjamin Sep 13 '19 at 21:17
  • Possible duplicate of [Is there a CSS parent selector?](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – disinfor Sep 13 '19 at 21:19
  • @ThomasBennett - I already have the "starting point" css in a legacy project - I want to find a what to modify it so that the calendar-popup isn't affected by the existing padding -- I was hoping that I could use some sort of "table.my padding" (where the table is not a child of any element with the ajax_calendar class) – Steven_W Sep 13 '19 at 21:23
  • @Michael_B - yes, that me be the case, but I haven't been able to find the correct css (using the not() selector or anything else) to achieve what I wanted (which is for "my_table" to affect all tables (and child cells) except for the calendar-popups which all happens to be wrapped in a "div class=ajax_calendar" – Steven_W Sep 13 '19 at 21:26
  • @disinfor - I've just had a look - I don't see it as being a duplicate. Rather than saying "is there an operator that does x y or z, I've asked a specific question on a single worked example. -- (Just about to modify the question to try and make it clearer) – Steven_W Sep 13 '19 at 21:30
  • @Michael_B - Just about to add the "real" code (part of a legacy ASP.NET Webforms project) - I can't think of any other significant "unique feature" (multiple pages use the same control, although at least the "site-wide common css-files" are easily accessible and modifiable. – Steven_W Sep 13 '19 at 21:42

1 Answers1

1

As far as making the CSS only apply to the parent table, you need to use the child combinator (>) in addition to the :not. Without the child combinator, the inner table still matches the rule because the descendants of the inner table (which is excluded by the :not) are also descendants of the outer table (which is not excluded by the :not).

To use the child combinator with a table and tr you have to include the implicit tbody. The net result for your selector is this:

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

That will get you most of the way there. However, the default background color is transparent, so your inner table will have the same background as the outer table unless something in between sets it. I chose to set it directly on .ajax__calendar but you could choose to put it on any descendant of the outer td (where the background color is getting 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>
cjc
  • 676
  • 3
  • 13
  • Thanks - that's nice and clear. (the background colour was just for demonstration) I can now see that my original :not() wasn't precise enough, and thus was still appling to all tables, not just the outer-table. – Steven_W Sep 14 '19 at 12:16