Tips on adjusting the width of an HTML table to prioritize a larger size for the last column over the first two

Hello everyone, I am pretty new to Django, HTML, and CSS. This is actually my first time using stackoverflow so I could really use your assistance.

My issue involves a table I have created:

<div class="grade_book"><table class="table">
      <tr><th>Year</th><th>Grade</th><th>Category</th></tr>
   {% for year, grade, category in results %}
           <tr><td>{{year}}</td><td>{{grade}}</td><td>{{category}}</td></tr>        
   {% endfor %} 
        
    </table></div>

My goal is to set the first two columns' width to 100px and the third to last one to 250px.

I have attempted to achieve this with some CSS but it seems that only the first column's width gets set to 100 px.

  .grade_book table td {text-align: left;font-size: 14px;}

  .grade_book .table{ table-layout:fixed; font-size: 16px; width: 100%;  margin-bottom: 100px; border-collapse: collapse;}

  .grade_book th {  width: 90px;}

  .grade_book th+th {  width: auto;}

I would greatly appreciate any help you can provide as I am still learning about CSS. I have tried searching for similar questions but haven't been able to find the exact solution I'm looking for.

Answer №1

Check out the :nth-child() selector, it allows you to target specific items in order of appearance.

<html>
<head>
 <style>
  .grade_book table td {text-align: left;font-size: 14px;}

  .grade_book .table{ table-layout:fixed; font-size: 16px; margin-bottom: 100px; border-collapse: collapse;}

  .grade_book th {  width: 100px}
  .grade_book th:nth-child(3) {  width: 250px;}

 </style>
</head>
<body>
 <div class="grade_book">
  <table class="table">
   <tr><th>Year</th><th>Grade</th><th>Category</th></tr>
   <tr><td>2020</td><td>A</td><td>Science</td></tr>
   <tr><td>2020</td><td>B</td><td>History</td></tr>
   <tr><td>2020</td><td>F</td><td>Math</td></tr>
  </table>
 </div>
</body>
</html>

Don't forget to adjust the .table by removing the 100% width setting.

Answer №2

Considering that the header of your table is hand-crafted, you have the flexibility to modify it as follows:

<div class="grade_chart">
  <table class="chart_table">
    <tr><th width="100">Year</th><th>Grade</th><th width="250">Category</th></tr>
       {% for year, grade, category in data %}
         <tr><td>{{year}}</td><td>{{grade}}</td><td>{{category}}</td></tr>        
       {% endfor %}         
  </table>
</div>

By fixing the first and third columns, the second column will adjust accordingly. Subsequent rows will also adhere to this format. Make sure to eliminate all width CSS properties.

Answer №3

By utilizing the :last-child selector, I was able to make adjustments to the width of your .table class in order to ensure that the width is properly maintained.

I made the decision to eliminate the twig loop, however, the functionality should remain intact:

 .grade_book table td {text-align: left;font-size: 14px;}

  .grade_book .table{ table-layout:fixed; font-size: 16px; width: 450px;  margin-bottom: 100px; border-collapse;}

  .grade_book th {  width: 100px; padding: 0px;}

  .grade_book th:last-child {  width: 250px;}
<div class="grade_book"><table class="table">
      <tr><th>Year</th><th>Grade</th><th>Category</th></tr>
 
           <tr><td>{{year}}</td><td>{{grade}}</td><td>{{category}}</td></tr>        
        
    </table></div>

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

Homepage featuring a stacked image background similar to Kickstarter

I am trying to replicate the stacked image effect seen on kickstarter.com. However, I am facing an issue where the images do not overflow on the screen as desired. The arrow indicates the area that needs to be filled with the image. Check out the image he ...

Rejuvenate a just-launched window.open starting from the about:blank

After receiving data from an ajax result, I am trying to open a pdf in a new window. However, the pdf viewer is only displayed if I manually resize the window (using manual hotspot resizing). How can I ensure that the contents display properly in its popu ...

My Perl script is displaying as a text file during testing and is not executing

As a novice web programmer, I recently created an HTML form that is supposed to run a script. However, when I try to test it by opening the file on my computer (not on a web server), the script does not execute. Instead, it simply displays as a text file ...

Absence of Icon in PrimeFaces RowEditor

Having difficulty with the p:rowEditor feature in PrimeFaces as the icon is not displaying properly. The style class was not modified and the default icon is being used. Works perfectly in FF, Chrome, and IE9. Unfortunately, project needs to run on IE8 w ...

Modify the conditions of a CSS file in Bootstrap using JavaScript

My project requires internationalization support for right-to-left languages like Arabic and Hebrew, so I need to modify some Bootstrap classes (such as col) to float right instead of left. I am using create-react-app with babel/webpack and react-bootstra ...

Close button situated at the top-right corner overlapped by HTML/CSS styling

Currently attempting to position a button overlapping and extending outside of its parent div, much like the illustration provided. However, my current efforts result in the button being contained within the parent DIV. The visual reference below showcases ...

Error message: The color shade "100" is not in the color palette. The only available shades are primary, accent, and contrast

I am in the process of upgrading from Angular 12 to 13. Encountering this error, and so far I haven't been able to find a solution for it. Could someone please guide me in the right direction? Here is my package.json file: { "name": &quo ...

The visibility of a CSS class is rendered non-apparent when formed through the implementation

function retrieve_files(){ $.ajax({ type: "GET", url: "retrieve_files.php", cache: false, success: function(result){ $("#insideT").html(result); } }); } retrieve_files.php $dir = 'upload ...

Utilize an API function when a button is clicked through Django URLs in Django/Python

I have designed my website with a search bar and button that I want to use to execute an API function in my api.py file. Here is what the api.py file looks like: import urllib2 import json locu_api = '****' def loc_search(request): query = ...

Identifying when a system window covers an iframe

After watching a fascinating YouTube video (https://www.youtube.com/watch?v=TzPq5_kCfow), I became intrigued by how certain features demonstrated in the video could be implemented using JavaScript. One specific question that arose for me was how one can d ...

Is there a way to implement the onClick event within a styled component?

I am experiencing an issue with styling in my code. I want to describe the problem using images. Essentially, when clicking on an image button, I expect the MenuItemContainer to become visible. const MenuItemContainer = styled.div` visibility: hidden; ...

Leveraging the Power of jsPDF in Your Google Apps Scripts

Is it feasible to integrate jsPDF into a Google Apps Script project? I am attempting to create documents using templated HTML, but certain CSS styles like background-color do not translate to PDF when using the getAs() function. Given the limitations of ...

conceal a targeted section from the bottom of the iframe

I have a website embedded inside an iframe for use in a webview application. <body> <iframe id="iframe" src="http://www.medicamall.com"></iframe> </body> This is the CSS code: body { margin:0; padding:0; height:10 ...

Troubleshooting Problem with Default Zoom Out on Iframe Images

When loading an image through iframe and removing default styling of zoom and auto-scrolling, I encountered a problem in displaying images in IE and Chrome with dimensions of 1024x768 (when using the Sample Picture from Windows). However, it works fine for ...

The CSS display:block property isn't functioning as intended

I'm currently working on a contact form using CSS and HTML, but I'm having trouble getting the boxes to display properly in a 'block' form (display:block). HTML: <section class="body"> <form method="post" action="index.php ...

JavaScript to resize images before uploading without displaying a preview

I'm searching for a way to prevent the need to upload large or heavy image files. I believe utilizing the HTML5 FileAPI library is the best solution for this task. All necessary features have been implemented (upload, re-ordering, etc.), so now I ju ...

Having difficulty modifying CSS font classes

This marks the beginning of a website I am currently creating for a client. Check it out here Here's the issue. I initiated this project some time back and upon returning to it, I'm facing difficulty in changing font styles without using inline ...

Is there a way to change the input type=file value?

What is the best method for customizing a file upload using input type="file" in HTML? Here is an example of what I am trying to achieve: <form enctype="multipart/form-data" action="xmlupload" method="post" name="form1" id="form1"> <input type ...

The PHP tag is being terminated by the code within the query $db->

Apologies for my lack of recent experience in php. I am revisiting some old code to troubleshoot a peculiar issue that has arisen. Within the realm of wordpress, I find myself editing php code and noticing an unexpected closure of the php tag below: <? ...

What is the best way to showcase application URLs consistently across the entire project?

I am encountering an issue where my application templates are unable to see URLs within the code. Here is a snippet of my url configuration: urls.py in project: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r&apo ...