Showcasing two columns side by side in the first row and one column beneath them in the second row within a single tr

Here is the code snippet I created:

<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>A: $10</td>
    <td>B: $15</td>
    <td colspan="2">Sum: $25</td>
  </tr>
</table>

Link to view on JsFiddle

I am looking to insert 3 <td> elements within a single <tr> tag. The first two <td> elements should be displayed on the same line, while the third <td> should go on a new line, all within the same row.

Apologies for any confusion earlier in my explanation.

Answer №1

To achieve the desired outcome with your table structure, you can utilize the following CSS styles. The key is to convert the table into block elements and then apply styling accordingly. It's important to note that the nth child element in this case is based on 2 columns - if you had three columns, as shown in your second example, it would be 3n+1

table, table * {
    display:block;
    box-sizing:border-box;
}
table {
    padding:3px 0 0 3px;
    border: 1px solid black;
    width:299px;
}
table:after {
    content:'';
    display:block;
    height:0;
    clear:both;
}
th, td {
    border: 1px solid black;
    width:144px;
    margin-bottom:3px;
    margin-right:3px;
    float:left; 
    
}
th:nth-child(2n + 1), td:nth-child(2n + 1) {
    clear:left;
}
.newline {
    float:none;
    width:291px;
}
<table>
    <tr>
        <th>Month</th>
        <th colspan="2">Savings</th>
    </tr>
    <tr>
        <td>January</td>
        <td colspan="2">$100</td>
    </tr>
    <tr>
        <td>February</td>
        <td colspan="2">$100</td>
    </tr>
    <tr>
        <td>A: $10</td>
        <td>B: $15</td>
        <td class="newline">Sum: $25</td>
    </tr>
</table>

I've included colspans in the table above to ensure your HTML remains valid even when CSS is disabled.

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

What is the method for causing a patch event using inline HTMX tags when a carousel-item in a Bootstrap 5 carousel is activated?

I am currently working on triggering a HTMX patch request with a specific id whenever an item in a Bootstrap5 carousel becomes active. While I understand that this can be achieved with some additional scripting, I am curious if it can be purely done using ...

Simple steps to validate an ajax response with a specific string

I'm encountering a problem with a simple ajax call that involves checking the returned text against a string: // in my php file echo 'mystring'; // in my javascript if((request.readyState == 4) && (request.status == 200)){ if(req ...

`Positioning of inline-block elements`

I am encountering an issue with the display CSS attributes set for three tags: a - inline-block img - block span - inline <a id="first"> <img/> <span> A first line of text B second line of text </span> </a> <a id="secon ...

Strategies for obtaining newly updated data with every request and implementing a no-cache approach in Apollo GraphQL and Angular

Every time a request is made, we need a fresh value, like a unique nonce. However, I am facing issues while trying to achieve this with Apollo's Angular client. My initial solution was to utilize watchQuery with the no-cache strategy: this.apollo.wat ...

Adjusting the width of elements using percentage values in CSS and altering the layout with Bootstrap

I am facing a peculiar issue. I have added a d-flex justify-content-between container to my div container with text on one side and images on the other. However, when I try changing the width using % (for example, width:5%), the flex behavior becomes str ...

The layout generated by Metronic does not display the CSS styles

I've been working on creating a Metronic layout using the layout builder, but I'm running into an issue. Whenever I try to open index.html, all I see is this: screenshot It appears that the CSS styles are not loading properly. Interestingly, wh ...

Limiting the number of rows in pagination using a select option

Having a pagination feature on my page, I now aim to add a new functionality. When the user selects the number of rows from a drop-down menu, the webpage should display matching data. Despite trying to implement this with ajax, the limit variable is not be ...

AJAX - Self-Executing Anonymous Function

I have a question that may seem trivial, but I want to make sure I'm heading in the right direction. I've created two different versions of an XMLHttpRequest wrapper, and both are functioning correctly. const httpRequest = function () { let ...

The React Swiper.js slider functions properly only when the page is resized

After implementing Slider.js React, I encountered an issue where the slider only functions properly after resizing the page. Clicking on the next button does trigger a console log for onSlideChange event, but it does not actually move to the next slide. An ...

Gatsby's build process encounters a stumbling block when webpack fails due to issues

Every time I try to run gatsby build, an error pops up: failed Building static HTML for pages - 10.179s ERROR #95313 Building static HTML failed Refer to our documentation page for more details on this issue: https://gatsby.dev/debug-html 343 | ...

Creating responsive images in Bootstrap columns

I've found a solution to my issue with large images, but I am still struggling with smaller images not fitting well into larger spaces. Essentially, I created a CMS for a user who required templates with various bootstrap columns as content areas. Cu ...

Issue encountered during registration for the pset9 cs5 (unforeseen exceptions)

"successfully registering a user causes an exception in the application (refer to the log for more details)" "registration is rejected due to duplicate username causing an exception in the application (refer to the log for more details)" ...

Tips on maintaining the numerical value while using JSON.stringify()

Having trouble using the JSON.stringify() method to convert some values into JSON format. The variable amount is a string and I want the final value in JSON format to be a number, but it's not working as expected. Even after applying JSON.stringify(), ...

What could be causing the NodeJs cluster module to be malfunctioning?

Currently, I am in the process of constructing an express server and have been exploring the concept of scaling NodeJS applications. Upon my research, one of the initial things that caught my eye was the built-in cluster module which allows for leveraging ...

Tips for fixing the issue of "The use of getPreventDefault() is outdated. Please use defaultPrevented instead."

When attempting to fetch data for a specific user from an SQL Server database using JSON data, I encountered an error message in the console: "Use of getPreventDefault() is deprecated. Use defaultPrevented instead." Additionally, the values are not bei ...

Switching chart types in ChartJS doesn't function properly when transitioning from a pie chart

My goal is to create a dynamic chart that can adjust as the data changes and also be able to switch between different chart types such as line, bar, or pie. I have made some progress already, but I have run into several issues. One major problem is encoun ...

Sending an array from a URL to JavaScript

Is it possible to dynamically pass an array from a GET request in the format below: [x, y], [x, y]... Into the second part of this Google Visualization API call? var data = google.visualization.arrayToDataTable([ ['Initial', 'Second' ...

AJAX does not support functioning links

I've been experimenting with AJAX on my website, and encountered an issue. When I dynamically add content to the page using container.insertAdjacentHTML('beforeend', thing); everything looks fine. However, when I try to click on the links wi ...

Is the parent node of the input element the input element itself?

When working with a DOM object obj of type <input>, I encountered an issue where attempting to obtain its parent node using obj.parentNode resulted in the same obj being returned. Is this behavior specific to <input> objects? What other types o ...

having difficulty with the design of my google map

Struggling to style my Google Map this week - I have the JSON values but no clue how to add them into the JavaScript. Also, need to move the zoom bar control to the right instead of it being hidden behind site content on the left. Any help would be greatl ...