Using Rowspan and Colspan in Tables

I am in need of creating a table similar to the one shown in this image: https://i.sstatic.net/ztBIj.png

Here is the HTML code for the table I implemented:

<table border="1" cellpadding="8" cellspacing="0">
    <tr>
        <th rowspan="3">Subject code and Subject Name</th>
        <th colspan="2" rowspan="2">Internal Assessment</th>
        <th colspan="2" rowspan="2">External Assessment</th>
        <th colspan="2">Grand Total</th>
        <th rowspan="3">Remarks</th>
    </tr>
    <tr>
        <th rowspan="2">MM</th>
        <th rowspan="2">MO</th>
        <td></td>
    </tr>
    <tr>
        <th>MM</th>
        <th>MO</th>
        <th>MM</th>
        <th>MO</th>
    </tr>      
    <tr>
        <td class="subject">Economics Theory</td>
        <td class="total">40</td>
        <td class="obtained">32</td>
        <td class="total">60</td>
        <td class="obtained">43</td>
        <td class="total">100</td>
        <td class="obtained">75</td>
        <td>P</td>
    </tr>
    <tr>
        <td class="subject">Elemetns of Statistics</td>
        <td>40</td>
        <td>31</td>
        <td>60</td>
        <td>38</td>
        <td>100</td>
        <td>69</td>
        <td>P</td>
    </tr>
    <tr>
        <td class="subject">Company Law</td>
        <td>40</td>
        <td>32</td>
        <td>60</td>
        <td>47</td>
        <td>100</td>
        <td>79</td>
        <td>P</td>
    </tr>
    <tr>
        <td class="subject">Money, Banking, Financial Management</td>
        <td>40</td>
        <td>31</td>
        <td>60</td>
        <td>36</td>
        <td>100</td>
        <td>67</td>
        <td>P</td>
    </tr>
    <tr>
        <td class="subject">Elements of Coding</td>
        <td>40</td>
        <td>32</td>
        <td>60</td>
        <td>47</td>
        <td>100</td>
        <td>79</td>
        <td>P</td>
    </tr>
</table>

The outcome I received from the above code was not as expected. I attempted to remove certain parts but that resulted in an unexpected outcome. https://i.sstatic.net/FtF3F.png

While trying to correct the issue with additional elements in the code, the result was completely different than anticipated. https://i.sstatic.net/6mLf2.png

Additionally, here is the CSS code used for styling:

table {
    margin-left: auto;
    margin-right: auto;
    text-align: center;
}

.subject {
    text-align: left;
    padding-left: 10px;
    padding-right: 95px;
}

.second {
    width: 20%;
}

.total {
    padding-left: 30px;
    padding-right: 30px;
}

.obtained {
    padding-left: 50px;
    padding-right: 50px;
}

Answer №1

Is it really "completely different"? The functionality of the table remains unchanged; the only noticeable (albeit minimal) difference lies in the adjustment of height between the "Grand Total" and other headings.

In my experience, achieving a strikingly similar appearance was possible by simply incorporating vertical padding into the "Subject code and Subject Name" heading. This padding can also be applied to the "Remarks" heading, or any element spanning two rows for that matter.

padding-top: 35px;
padding-bottom: 35px;

This approach appears to closely resemble the desired outcome you are aiming for.


On a side note, utilizing modern CSS grids rather than tables might provide a more efficient solution. Tables tend to be cumbersome to manipulate, as evidenced by the complexity of this particular fix.

Answer №2

Perhaps I arrived a bit late,
but allow me to share a clever method to achieve the desired appearance,
Simply make the border of the unwanted cell invisible and adjust the cell width to 0

Here is my approach,

    <table border="1" style="border-collapse:collapse; border:none; border-color:black" width="100%" height="30%" cellpadding="8px" align="center">
        <tr height="50px">
            <th rowspan="3">Subject code and Subject Name</th>
            <th colspan="2" rowspan="2">Internal Assessment</th>
            <th colspan="2" rowspan="2">External Assessment</th>
            <th colspan="2">Grand Total</th>
            <th rowspan="3">Remarks</th>
        </tr>
        <tr>
            <th rowspan="2">MM</th>
            <th rowspan="2">MO</th>
<!--cover up the unwanted cell-->
            <td style="padding: 0;padding-top: 18px;border: none;width: 0;"></td>

        </tr>
        <tr>
            <th>MM</th>
            <th>MO</th>
            <th>MM</th>
            <th>MO</th>
        </tr>
        <tr>
            <td>Economics Theory</td>
            <td>40</td>
            <td>32</td>
            <td>60</td>
            <td>43</td>
            <td>100</td>
            <td>75</td>
            <td>P</td>
        </tr>
        <tr>
            <td>Elements of Statistics</td>
            <td>40</td>
            <td>31</td>
            <td>60</td>
            <td>38</td>
            <td>100</td>
            <td>69</td>
            <td>P</td>
        </tr>
        <tr>
            <td>Company Law</td>
            <td>40</td>
            <td>32</td>
            <td>60</td>
            <td>47</td>
            <td>100</td>
            <td>79</td>
            <td>P</td>
        </tr>
        <tr>
            <td>Money, Banking, Financial Management</td>
            <td>40</td>
            <td>31</td>
            <td>60</td>
            <td>36</td>
            <td>100</td>
            <td>67</td>
            <td>P</td>
        </tr>
        <tr>
            <td>Elements of Coding</td>
            <td>40</td>
            <td>32</td>
            <td>60</td>
            <td>47</td>
            <td>100</td>
            <td>79</td>
            <td>P</td>
        </tr>
    </table>

Answer №3

I believe there is room for improvement here

<table border="1" cellpadding="8" cellspacing="0">
    <tbody>
        <tr>
            <td rowspan="2">Subject Code and Subject Name</td>
            <td colspan="2">Internal Assessment</td>
            <td colspan="2">External Assessment</td>
            <td colspan="2">Grand Total</td>
            <td rowspan="2">Remarks</td>
        </tr>
        <tr>
            <td>MM</td>
            <td>MO</td>
            <td>MM</td>
            <td>MO</td>
            <td>MM</td>
            <td>MO</td>
        </tr>
        <tr>
            <td class="subject">Economics Theory</td>
            <td class="total">40</td>
            <td class="obtained">32</td>
            <td class="total">60</td>
            <td class="obtained">43</td>
            <td class="total">100</td>
            <td class="obtained">75</td>
            <td>P</td>
        </tr>
        <tr>
            <td class="subject">Elements of Statistics</td>
            <td>40</td>
            <td>31</td>
            <td>60</td>
            <td>38</td>
            <td>100</td>
            <td>69</td>
            <td>P</td>
        </tr>
        <tr>
            <td class="subject">Company Law</td>
            <td>40</td>
            <td>32</td>
            <td>60</td>
            <td>47</td>
            <td>100</td>
            <td>79</td>
            <td>P</td>
        </tr>
        <tr>
            <td class="subject">Money, Banking, Financial Management</td>
            <td>40</td>
            <td>31</td>
            <td>60</td>
            <td>36</td>
            <td>100</td>
            <td>67</td>
            <td>P</td>
        </tr>
        <tr>
            <td class="subject">Elements of Coding</td>
            <td>40</td>
            <td>32</td>
            <td>60</td>
            <td>47</td>
            <td>100</td>
            <td>79</td>
            <td>P</td>
        </tr>
    </tbody>
</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

Discover the color value within an array that begins with the "#" symbol

In my PHP code, I have written a function that extracts values from a CSS file and creates an array. Now, I need to loop through this array and create another array that only contains color values (strings starting with #). The challenge is that the length ...

The ng-model feature is unable to properly save data within a nested ng-repeat function for a 2-dimensional array

I'm currently working with a nested ng-repeat on a 2d array and have added ng-model to the third ng-repeat. However, I am encountering an issue where my ng-model is not properly storing the data within the object. JavaScript Code emrService .getDat ...

Remove bulleted list from HTML using JavaScript DOM

My task involved deleting the entire "agent" array using the removeChild() function, requiring the id of a <ul> element. However, I faced an issue as I couldn't set the id for the "ul" due to using the createElement() function. //old code < ...

Persistent footer text moving around

Check out the sticky header I created here. However, when it sticks in place, the text moves down and goes out of the container. I want the header to remain within the container. Here is my HTML & jQuery: <div id="container"> <div id="menu ...

Exploring the Contrast between Using @import for Styles and js Import for Webpack Configuration

While delving into the source code of ant-design, I couldn't help but notice that each component has both an index.ts and a index.less file in the style folder. This got me thinking - why is JavaScript being used to manage dependencies here? What woul ...

Show information from an array

On the index.php page, there is a script that retrieves data from demo.php and presents the outcome in a div. <div class="leftbox"> <?php echo "<div id='proddisplay'>"; echo "</div>"; ?> </div&g ...

Avoiding the use of a particular URL when using this.router.navigate() in AngularJs

In my angularJS registration form, I am using a bootstrap template for the design. The URL path to access the form page is http://localhost:4200/signup <form method="post" (submit)='register(username.value,email.value,password.value)'> ...

What is the process for customizing the onmouseover property in HTML?

I am currently working on customizing tabs. I want to underline the title of the active tab, but for some reason I am having trouble achieving this with CSS. It seems like I might need a CSS syntax that I am not familiar with. As a solution, I am consider ...

Update various components within a container

I have incorporated a function that automatically loads and refreshes content within a div every 10 seconds. Here is the script: $(function () { var timer, updateContent; function resetTimer() { if (timer) { window.clearTimeout(timer); ...

The checkbox event listener becomes dysfunctional when the innerHTML of its container is modified

My current challenge involves creating checkboxes with a blank line inserted after each one. I also need these checkboxes to trigger a function when changed. This is my code snippet: var div = document.getElementById("test"); var cb1 = document.createEl ...

Creating a repeating sequence in HTML: A step-by-step guide

I have an HTML file containing a portfolio. Each portfolio item is structured like this: <div class="item"> <div class="portfolio-item"> <a href="img/portfolio-item-1.jpg" data-lightbox="ima ...

substitute a variable

I am trying to update the value of a variable using an HTML form. Within my config.php file, I have declared a variable $num = 3; I would like the value 3 to be replaced with whatever I input in the form form.html. Below is the code for my HTML form : ...

A helpful tip for creating line breaks within a Vue JS v-for loop using CSS

I am looking to arrange the names in alphabetical order when a list item is clicked. My tools of choice are Vue.js and Flex Grid. The list item I am working with is called ListAll, When clicking on ListAll, I want to display all the records grouped by na ...

Creating a combined Email and Password form field in a single row using Bootstrap

Recently, I decided to explore bootstrap. I am looking to implement email/password validation in one row on my rails app: email? | password? | OK Currently, I have a functioning code for displaying only an email field and an OK button in one row: email? ...

Tips on adjusting the size of an HTML canvas specifically for the display

Currently, I am working on an innovative project utilizing HTML5 and canvas: The challenge at hand is to draw on a canvas with dimensions of 2200px/1600px (width/height), display it on my website in a smaller window sized at 600px/400px. However, post cli ...

HTML when contenteditable attribute is set to "true" doesn't have a fixed width

I was looking to create a textarea with HTML elements inside it and came across this informative article. My approach involved creating a div element with the attribute contenteditable="true". #call-text-form { height: calc(100vh - 350px); width: ...

Encountering a DateTime discrepancy with the date field

Currently, I am utilizing ext.net 3 and encountering an issue with creating a datetime together. In this ext, the time and date features are not synchronized. <ext:DateField runat="server" ID="date1" MarginSpec="0 10 0 20" LabelAlign="Right" Format="yy ...

Issue with CSS3 calc() validation: Invalid value provided for width, resulting in a parse error

Can you help me validate a CSS3 file that contains the following code snippet? width:calc(96.3% - 312px) When I try to validate it, I get this error message: Value Error : width Parse Error - 312px) My solution might be to implement a JavaScript func ...

What is the best way to seamlessly integrate an image icon into a sentence, so it appears as a natural part of the text

I'm struggling to insert an (i) information icon before the phrase "Learn more" in the sentence: "Click on the 'Learn more' buttons for additional information." The icon keeps overlapping with the words "Learn more." Could someone provide s ...

Having Trouble Adjusting Width of Inline List Items in Internet Explorer?

I am encountering an issue with an unordered list where the list items are displaying inline, and the layout is quite complex. To better explain, here is the Fiddle: Fullscreen: http://jsfiddle.net/spryno724/F5CFD/embedded/result/ Code: http://jsfiddle.n ...