Design a table where the thead element is larger in width compared to the overall width of

My table is very basic, consisting of 3 rows and 3 columns. I am attempting to make it resemble the layout shown in this image

https://i.sstatic.net/31nIN.jpg

(specifically, the thead should have a greater width than the other rows). How can I achieve this effect? I have experimented with colspan but haven't been successful so far. The structure of my basic table looks like this:

 <table>
   <thead>
   <tr>
   <th>Extra details</th>
   </tr>
   </thead>
   <tbody>
   <tr>
   <td>A</td>
   <td>B</td>
   <td>C</td>
   </tr>
   <tr>
   <td>A</td>
   <td>B</td>
   <td>C</td>
   </tr>
   <tr>
   <td>A</td>
   <td>B</td>
   <td>C</td>
   </tr>
   </tbody>
 </table> 

Any suggestions or tips would be greatly appreciated!

Answer №1

To achieve the desired layout, ensure you include colspan="3" in the th tag within each tr of your table. Additionally, apply a wider style to the last td element in every tr using the :last-child pseudo selector. For more information on this selector, visit this link.

table {
  width: 100%;
}
thead th {
  background-color: #ddd;
}
td:last-child {
  width: 60%;
}
<table>
  <thead>
    <tr>
      <th colspan="3">Additional Information</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
    </tr>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
    </tr>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
    </tr>
  </tbody>
</table>

Answer №2

To create a virtual fourth column with 50% width, you can utilize the "colspan" attribute along with the following CSS:


tbody tr:after {
  content: '';
  display: table-cell;
  width:50%
}
th {
  background: gray;
  border: solid;
}
table {
  width: 100%;
}

Implement this HTML structure within your code:

<table>
  <thead>
    <tr>
      <th colspan="4">Extra details</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
    </tr>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
    </tr>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
    </tr>
  </tbody>
</table>

Answer №3

In order to properly structure your table and provide a more semantic approach, you can utilize the caption element that extends across the width of the table. This accurately describes the content of the entire table. You can then employ a pseudo-element to occupy the remaining space within the rows and condense the other columns.

table {
  width: 100%;
}

caption {
  border: 2px solid darkgray;
  background-color: lightgray;
}

tr:after {
    display: table-cell;
    content: "";
    width: 100%;
}
<table>
   <caption>Additional Information</caption>
   <tbody>
   <tr>
   <td>A</td>
   <td>B</td>
   <td>C</td>
   </tr>
   <tr>
   <td>A</td>
   <td>B</td>
   <td>C</td>
   </tr>
   <tr>
   <td>A</td>
   <td>B</td>
   <td>C</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

Why is the column width on this Tumblr page constantly flickering?

Seeking assistance with a minor issue on my Tumblr page - the content column seems to be flickering on mouseover, shifting between full width and its original programmed width. Any insight on the CSS causing this? Much appreciated! Check it out here (Dis ...

Discover the best way to showcase items within an arrayList using dual CSS styles!

I'm currently working on a project that involves an arrayList with two names: Bob and Steve. I have the requirement to display them in different colors, where Bob should be displayed in green and Steve in red. Component.CSS .Bob{ font-weight:bold; ...

Meteor: utilizing the background image for the body

I'm working on a Meteor project that has various Routes set up. Within one of my css files, I currently have the following styling for the body: body { font-family: 'Roboto', sans-serif; font-size: 17px; font-weight: 400; c ...

Assign the two values to variables in the CSS script

My challenge lies in passing two values to the CSS within the <style> tags. The values are: (background-color: --placeholder; color: --placeholdtext;). I am able to pass either one of the values successfully, but not both at the same time. When I cop ...

Utilizing Google Maps to display an infowindow upon clicking a location from a geojson file

I want to implement info windows that pop up when markers on a map are clicked. I used the sample code provided by Google (below) to create a marker map from geojson files dragged and dropped into the window. My goal is for the info windows to show text ta ...

How can one extract information from an ASP.NET MVC on a separate webpage?

Experimenting with ASP.NET MVC for the first time here, so bear with me if this comes across as textbook-like. A basic content management system has been developed using ASP.NET MVC. The URL to retrieve a list of content, specifically announcements, looks ...

How to trigger events in a dynamically created list using jQuery

$(document).ready(function() { $(".addtophundredwatchlist").click(function() { $.ajax({ type : "GET", url : "/watchlisttophundred/", success : function(data){ var raw_tag = "& ...

AngularStrap's bs-modal is failing to display content

I have a question, here is a Plunker with the code: http://plnkr.co/edit/SNFy2XcOBefUavG1QCqD?p=preview <button class="btn btn-default" data-template="customer.tpl.html" bs-modal="modal">New Customer </button> < ...

Tips on concealing all classes except one through touch swiping

If you have a website with a single large article divided into multiple sections categorized as Title, Book1, Book2, & Book3, and you want to implement a swipe functionality where only one section is displayed at a time, you may encounter some issues. ...

IE and Chrome are experiencing issues where the radio buttons are disappearing

Here is the style sheet code: select,input ,td a {border:1px solid green; width:25%;height:25px;font-size:20px;margin- left:.1em;} input.myradio {border:none;width:0%;height:0%;font-size:0%;} And here is the corresponding HTML code: <td><inpu ...

Which is more beneficial: incorporating a ready-made rule for every CSS style or crafting your own rule (OOCSS)?

Learning about Nicole Sullivan's concept of Object-Oriented CSS (OOCSS), I discovered the idea of using pre-existing basic building blocks as classes and then customizing them to suit individual needs rather than starting from scratch each time. The O ...

Creating a rotating HTML5 video and exporting the result as a canvas image

I'm currently using the jpeg_camera library to display an HTML5 Video on my website (https://github.com/amw/jpeg_camera) Here's how the video output looks in my HTML: <video autoplay="" src="blob:http://localhost:49209/41c42143-5d0e-4525-8fe ...

Tips for connecting lines between md-radio-buttons housed in a div with a label using Angular Materials

I am looking to enhance my md-radio-buttons by adding connecting lines between them in a component that uses dynamic data. <md-radio-group ng-model="selected" layout="row"> <div ng-repeat="i in items"> <label>{{i.name}}</labe ...

Adaptive Style Sheets Strategy

The images below depict the concept I am trying to achieve. The first image shows a longer width while the second one represents a shorter width. The red blocks are positioned on the right and left sides, with the yellow block adjusting its width based on ...

Iterate over an array to locate the position of a user-entered value, and subsequently update the element at that position within a different array

I'm currently working on a Hangman game with a unique twist using JS/jQuery. I've managed to successfully identify the index of the word being guessed based on user input, but I'm struggling with replacing the blank underscoreArray with the ...

CSS code preventing hyperlink from functioning

My website is almost complete, but I'm running into an issue with some hyperlinks on my event registration page. The first link works fine, but the second one does not. To troubleshoot, I created a test page with just the two links and still encounter ...

Trouble with jump link functionality within an ASP.NET iframe

On my asp.net page named Home.aspx, I have an iframe with the ID frame1. The iframe contains some HTML design with multiple headers. I am trying to search for the headers using quick links, but it seems to work fine on an HTML page, not on the asp.net page ...

Troubleshooting: Issues with window.location.href and window.open within an iframe

Code Update <div> <button type="button" class="submit btn btn-default" id="btnSubmit">Submit </button> <button type="button">Cancel</button> </div> <script> $("#btnSubmit").click(function(e) { ...

Building a row of five dynamic columns using Bootstrap's responsive design

I'm looking to set up a row with 5 responsive columns that have equal padding on all sides, but I'm struggling to find the best way to do this using Bootstrap. Here's my current HTML: <div class="row five-col-row"> < ...

Python: Fails to reach the second iteration in the For loop

I need Python to iterate through multiple pages by incrementing the page number in the URL. # Using get request on the site from bs4 import BeautifulSoup import requests from warnings import warn response1 = requests.get('https://lasvegas.craigslist ...