Apply CSS3 to target all td elements within the main table that contains nested tables

Today, I delved into the world of CSS3 and HTML5 through Codecademy.

HTML:

<table class="main">
    <tr class="questioncontainer">
        <td class="serialnumber">1.</td>
        <td class="question" colspan="2">What is the question?</td>
    </tr>
    <tr>
        <td></td>
        <td>
            <table class="notmain">
                <tr>
                    <td>
                        <div class="answer">Answer 1</div>
                    </td>
                    <td>
                        <div class="answer">Answer 2</div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="answer">Answer 3</div>
                    </td>
                    <td>
                        <div class="answer">Answer 4</div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

CSS:

table.main {
    text-align: center;
    width: 75vw;
    margin-top: 15%;
    margin-left: auto;
    margin-right: auto;
    border-collapse: collapse;
    background-color: #fff3f3;
}

table.main tr td:first-child {
    border-right: thin solid lightgray;
}

I am seeking to target all the leftmost table cells of the top table specifically. This means selecting the first td element of each tr within the main table.

The selector table.main tr td:first-child also affects the inner table cells, which is not my desired outcome. Attempts with

table.main>tr>td:first-child
, .main>tr>td:first-child, and table>tr>td:first-child failed to work as expected, failing to target the top table's td elements altogether.

What would be the appropriate selector in this case, and what was incorrect about the attempts I made? Ideally, I want to avoid assigning a specific class to those targeted td elements.

For reference, here is the Fiddle link: http://jsfiddle.net/x70srmae/1/

Answer №1

I attempted to use various selectors such as

table.main>tr>td:first-child
, .main>tr>td:first-child, and table>tr>td:first-child, but none of them were successful in capturing the top table's td elements.

This issue arises because the tr elements are actually not direct children of the table, but are instead children of the tbody (which browsers add when parsing the HTML document).

To address this, a selector like

table.main > tbody > tr > td:first-child { background:red; }

or something similar that considers the automatically inserted tbody should work – you can view an example at http://jsfiddle.net/x70srmae/2/

(I am still mentioning the .main class in this context to ensure that the selector targets the first td within the tr inside the tbody of the outermost table exclusively.)

Answer №2

You were so close! Instead of focusing on the .questioncontainer element, try targeting the first child of a table within the main class:

table.main td:first-child

Check out this link for more information!

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

Highchart displays text centrally on legend hover

I am struggling with the code provided here. My goal is to make text appear in the center of the donut chart when hovering over the legend. Similar to how it works when hovering over a single piece of the donut chart. var chart = new Highcharts.Chart ...

Generate Files Automatically through PHP Script

I am working on a project which requires the creation of files using the fwrite function in PHP. My goal is to ensure each file is unique and does not overwrite others. The project involves capturing text from a PHP form and saving it as an HTML file. I ...

Unlocking the power of dynamic text using a single form

My comment reply system is experiencing an issue where the first reply works fine, but subsequent replies are unable to get the reply text value. How can I ensure that all replies work properly based on the Razor code provided below? <h4>Comments< ...

Identifying when floats exceed their limits

Is there a way to identify overflow on floating elements? It's important to note the difference between setting overflow to visible or hidden. overflow: visible; overflow: hidden; Do all floated elements inherently contain overflow? Why is it chal ...

php$json_data = json_decode($response);

I am brand new to PHP coming from a background in Python. I was fairly comfortable with retrieving JSON results from websites in Python, but for some reason, I can't seem to get it working in PHP. Here is the code snippet I'm using: <?php $ ...

What is the best way to revert the Highcharts bar color back to its default color?

I am currently working with Highcharts version 4.1.10 In my bar chart, I have implemented a feature where the color of the bar changes when its value exceeds a certain threshold. //This function is called at regular intervals by a timeout function myTime ...

Show and hide a division based on the status of a checkbox

I have a specific requirement regarding authentication modes. When selecting a single factor, only one type of authentication mode can be chosen at a time and the corresponding div will be displayed. However, when selecting multiple factors, I should be ab ...

Tips for showing an inline <span> within <div> elements

Is there a way to showcase inline spans within a div element while ensuring they are displayed equally in size and space, based on the user's screen? And how can I ensure that these spans are positioned behind other div elements? body { width: au ...

Modifying a header's properties by incorporating chosen values from a form

I am tasked with creating an HTML form that includes 3 select attributes (color, shadow, background-color). Depending on the user's selections, these attributes will be applied to a h1 element on the page. I need to achieve this without using JavaScri ...

Struggling to center the hidden divs both vertically and horizontally on the page when they are made visible

I'm having trouble aligning my hidden divs in the center of the page. When I set their position to relative, the scroll increases as I move from one page to another (meaning the div is not vertically centered on the page). Should I place all three d ...

jQuery functions correctly within jsfiddle, yet it encounters issues when utilized within WordPress

I've been experimenting with a jQuery script to grey out the screen. While it works perfectly on my jsFiddle link provided below, I'm having trouble getting it to work from within my WordPress plugin. Check out the working script on my jsFiddle ...

Is it possible to style a strikethrough item in a list using CSS?

Within this list, I want to use CSS to indent any list item that drops a line. I attempted to achieve this using the following CSS: li:not(::first-line) { text-indent: 15px; } Unfortunately, it doesn't seem to be working as expected. Here is an exa ...

Troubleshooting delays in jQuery animations

Hello, I've encountered an issue with the delay in jQuery's .animation function. When quickly moving over boxes, the animation doesn't trigger; however, it works perfectly when done slowly. Any assistance on adding a 500 millisecond delay wi ...

Is it possible to modify the dirpagination filter in AngularJS from an input box to a select box?

I have been experimenting with the AngularJS dirpagination library. Feel free to check it out at this link. You can also see a code demo at: this link. My question is, is it possible to change the input box to a select box and still have the same functio ...

Enhancing elements in Bootstrap Studio with borders

After exploring the interface of bootstrap-studio, it appears that there is no direct option to add a border to an element. However, this can still be achieved either through custom css code or by utilizing the predefined boostrap border classes. You may r ...

Tips on showcasing an array as a matrix with a neat line property

I am currently developing an application using TypeScript, and utilizing a JSON array structured like this: data = [{"name":"dog", "line":1}, {"name":"cet", "line":1}, ...

Extracting HTML Data in Point of Interest (POI)

When creating a spreadsheet report using POI, I often come across html content with various tags like <p>, <b/>, &nbsp;, etc. Is there a way to parse these html tags in POI? Are there any functions in POI that can handle html content? Here ...

Setting the second tab as the primary active tab

I am currently working on a script that is well-known, and everything is functioning perfectly. However, I want to change it so that when the page is first opened, it displays the second tab instead of the first one (the first tab being a mail compose tab ...

What is the best method for retrieving the latest three objects that have been created in the database?

I'm struggling to figure out how to display the latest 3 created objects on my website. I need to order the models by created date and then access the 1st, 2nd, and 3rd items in that ordered set. However, I'm not sure how to do this correctly. He ...

Identifying the method of navigation using PerformanceNavigationTiming

Previously, I was able to determine if a user had arrived on the page by clicking the back button in the previous page using window.performance.navigation.type like this: if (window.performance.navigation.type === 2) { window.location.reload() } Howe ...