Creating Sleek Tables - comparing the benefits of using table-layout:fixed versus setting individual td-width to

Several weeks ago I posted a query (Table overflows parent div when td content is too wide) related to table design. The response I received seemed to resolve the issue perfectly. Now, as I attempted to redesign the table yesterday, I encountered a challenge in adjusting the width of a td element.

My requirements are:

  1. I need the header to remain unwrapped and not show overflow. This was addressed by the previous solution.
  2. Additonally, I want the yellow td cell to occupy 20% width instead of the fixed 50%.

To date, I've yet to find a solution where both issues are resolved simultaneously. Visit this jsFiddle, toggle comments to observe both dilemmas:

table {
    width: 90%;
    border: 1px solid black;
    /*table-layout:fixed;*/    1. Problem (Header wraps)
    table-layout:fixed;        2. Problem (td-width != 20%)
}

.header {
    overflow:hidden;
    /*white-space: nowrap;*/   1. Problem (Header wraps)
    white-space: nowrap;       2. Problem (td-width != 20%)
}

Is there a way to solve these two challenges concurrently?

Answer №1

Your issue lies in the fact that within a table with a fixed layout, the column width is determined by the first row.

To solve this problem, you can insert 2 dummy cells in the initial row and shift the header down into its personal row. Additionally, ensure that your .red column spans 3 rows to accommodate this extra row:

<div>
  <table>
    <tr>
        <td class="red" rowspan="3">RED</td>
        <td class="dummy-green"></td>
        <td class="dummy-yellow"></td>
    </tr>
    <tr>
        <td class="header" colspan="2">TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT 
        TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</td>
    </tr>
    <tr>
        <td class="green">GREEN</td>
        <td class="yellow">YELLOW</td>
    </tr>
  </table>
</div> 

Adjust the width of the dummy-yellow element to 20%:

.dummy-yellow {
  width: 20%; 
}

Keep table-layout: fixed; and white-space:nowrap unchanged.

The only drawback I've noticed with this approach is the creation of an invisible 2px row above the header.

View the demonstration

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

Resolve background image alignment issue in Firefox browser for Bootstrap carousel

I am currently utilizing this particular bootstrap template. My goal is to have the carousel background images fixed, however, when I include the following CSS: background-attachment: fixed; like so: <div class="carousel-item active" style=&q ...

The Page Transcends the Boundaries of the HTML Tag

This particular issue sets itself apart from the common overflow problems that are often faced by people. Interestingly, I have only encountered this problem while using Chrome (Version 41.0.2272.101 64-bit). IE 9+ and Firefox, on the other hand, seem to b ...

Adjust SVG size to fit parent container/division

I am trying to make the SVG completely fill the containing TD. I don't care about preserving the proportions of the SVG. Instead, I want the height of the SVG to be the same as its width when the height should be larger than the width. There is paddin ...

Dimensions of HTML Container on Google Website

I'm attempting to incorporate a collapsible table using HTML Box in a Google site. The code for the collapsible table can be found at http://tutorials.seowebpower.com/google-sites-advanced/collapsible-table. Here is the code: <html> <head> ...

What is the reason that certain CSS pages are not being utilized on my website?

I am facing issues with rendering my website on a tablet device, specifically with my last 2 css pages (800px.css and 800pxland.css). Despite working fine just hours ago, now they seem to be neglected while all the other ones work perfectly. I have tried ...

Exploring the Application of CSS Styles in Selenium

I need help with a webpage that contains a configurable field. The values of this field can be set through a configuration panel, and it can be marked as required by checking a checkbox which applies a specific style to the field label. My question is, how ...

Modifying icons with JavaScript

When I click on the play icon, it changes to the pause icon. However, I am only able to change the first icon from play to pause in my JavaScript code. How can I apply this functionality to all the audio elements on the page? let playIcon = "https://ima ...

Unable to use the select function in Android 2.2

When I click on the select button in my app, it displays multiple options. This functionality works smoothly on Android 4.1, but on Android 2.2, clicking on the select button does not have any effect and therefore the options cannot be selected. <sec ...

Having trouble getting PostCSS nesting to work with CSS variables in Tailwind CSS and Next.js?

I've been attempting to utilize PostCSS nesting alongside CSS variables, but unfortunately the CSS variables are not being converted at all. Instead, I am seeing Invalid property value in the DOM for CSS Variables. The tailwind.css file I have inclu ...

Differences in CSS between IE10 and Chrome are causing compatibility issues

Website in question: What is causing the difference in display between IE 10 and other browsers? The website appears to function correctly on Chrome: IE 10: ...

Running CesiumJS is a straightforward process that can be done by

After diligently following the provided instructions, I managed to get cesium running locally. I then attempted the hello world example as shown: However, despite clicking on "open in browser," I couldn't see anything. It has been a constant struggl ...

choosing and identifying the iframes for YouTube videos

I have a webpage that showcases images of YouTube videos, allowing users to choose from them. This is how I am presenting them: <ul> <li> <div class="youtubeMediaContainer"> <img src="video_preview_image1.jpg"& ...

Creating personalized templates in DJANGO - Efficiently linking help_text to individual fields

When creating a registration form using my own CSS and HTML, I encountered an issue with displaying error messages from Django's default help_text. Although I can show the help_text using the code below, I am having trouble associating the errors with ...

Issue with menu display on Internet Explorer

Having some trouble with IE. My menu displays fine in Firefox and Chrome, but IE is causing issues... It seems that IE isn't rendering the <li> tag properly, so my dropdown menu isn't functioning in IE. Can anyone assist me? Here's t ...

When hovering, the <a> element does not fully encompass the background color

I am currently working on a navigation bar that changes the background color of the links when hovered. However, I encountered an issue where the dropdown menu in the navigation bar does not cover the entire area. body { margin: 0; font-family: Aria ...

The issue of the back button not functioning in the React Multi-level push menu SCSS has

I have been developing a mobile-friendly Multi-level push navigation menu for my website using dynamically generated links based on projects from my GitHub account. I found a push menu on CodePen here and am in the process of integrating it into React inst ...

Files are nowhere to be found when setting up an angular project

After creating an Angular project, I noticed that some key files were missing in the initial setup, such as app.modules.ts and app-routing.modules.ts The project was generated using the command ng new name Here is a screenshot displaying all the files th ...

Excessive width of Bootstrap container

Encountering a rather simple issue: the bootstrap container inside my navbar is too wide, causing an unwanted horizontal scrollbar as it expands the body. You can view the problematic page here. The theme used for this site can be found here. What's ...

What is the best way to customize the cursor for each individual div?

Can you alter the cursor from a pointer to "not-allowed" within the div or GridList element in ReactJS or Material UI? ...

Creating a color-changing checkbox toggle in Bootstrap 5

Is it possible to customize the color of a Bootstrap 5 switch without changing it site-wide? I want some checkbox switches to have a unique color. Most tutorials online only cover how to change colors globally or use images, but I'm hoping to achieve ...