Ways to implement adjustable width in structured table layout?

I have a table with three <td> cells.

  • The first <td>'s width should adjust to its content.
  • The second <td>'s width is fixed.
  • The third <td> should fill the remaining space.

Note1: My website is responsive, so the width of the third <td> needs to change based on the screen size.

Note2: To prevent content overflow in the third <td>, I must use table-layout: fixed; and width: 100%; for the table. Additionally, I need to apply word-wrap: break-word; to the <td>'s. It's important to note that word-break: break-all; should not be used as it breaks words in the middle of the cell.


This is my implementation:

HTML:

<div class="container">
   <table>
      <tr>
         <td class="variable-width">depends on content</td>
         <td class="fixed-width">always fixed</td>
         <td class="fill-space">fills remaining space</td>
      </tr>
   </table>
</div>

CSS:

.container{
   width: 60%;
}

table{
   table-layout: fixed;
   width: 100%;
}

td{
   word-wrap: break-word;
   vertical-align: top;
}

.variable-width{
   width: auto;
}

.fixed-width{
   width: 10px;
}

.fill-space{
   width: 98%;
}

View the code in action on this fiddle.


Desired outcomes:

Example 1:

+-+---+------------------------------------------+
|4|fix|this is a test...!                        |
+-+---+------------------------------------------+

Example 2:

+----+---+--------------------------------------+
|2   |fix|this is a test...!                      |
+----+---+--------------------------------------+
|    |fix|no first TD here                        |
+----+---+--------------------------------------+
|1234|fix|no first TD here                        |
+----+---+--------------------------------------+

Example 3:

+---+---+-----------------------------+
|123|fix|adjustable vote up width       |
|   |   |with word wrap                 |
+---+---+-----------------------------+

Example 4:

+---+---+-------------------------------------------------------+
|123|fix|Integrated testttttttttttttttttttttttttttttttttttttttttttt|
|   |   |ttttttttttttttttttttttttttttttttttttttttttttttttttttttt  |
|   |   |tttttttttttttttttttttttttttttttttttttttt               |
+---+---+-------------------------------------------------------+

Example 5:

+---+---------------------------------------------------------+
|fix|remove variable-width cell if missing                     |
+---+---------------------------------------------------------+
|fix|remove variable-width cell if missing                     |
+---+---------------------------------------------------------+

Is JavaScript necessary to achieve this functionality?

Answer №1

Consider using this CSS property:

white-space: pre-line;

You can explore additional values and behaviors here

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 causes my animation to “reset” upon adding a new element using innerHTML?

One of the functionalities on my webpage involves a script that inserts a div called "doge" using innerHTML when a button is clicked. Additionally, there is a CSS keyframes animation applied to another div on the same page. However, whenever the button is ...

Seeking ways to activate child input text selection in Mozilla Firefox through CSS?

Imagine this scenario: I have a webpage where all elements that are displayed must not be selectable. <html> <head> <style type="text/css"> body { -webkit-user-select: none; -moz-user-select: none; ...

Interface similar to Amazon that allows users to easily choose product size and color by clicking on the desired options (e.g., clicking on a small red box to select a

For my e-commerce website, I am looking to create a user-friendly interface similar to that of Amazon for selecting product size and color. Instead of traditional radio buttons, I want to display a grid of small boxes, each representing a different color o ...

The alignment of the labels on the left side of the div is out of place

After spending hours trying to troubleshoot, I still can't seem to align my hidden radio buttons properly within a label with a background image. Instead of lining up on the left side as intended, they are appearing closer to the middle of the div. Fo ...

Explore our interactive map and widget features that will seamlessly fill your browser window for a

Struggling to make a webpage with a Google Map, Chart, and Grid expand to fill the available space on the screen. Tried various CSS tricks but nothing seems to work. Check out this simplified example in JsFiddle that showcases the issue: http://jsfiddle. ...

Is it possible to adjust the background image with properties like Scale to Fill, Aspect Fit, or Aspect Fill?

Is it possible to set the background image using the following properties in HTML? (These are properties used for iOS images, but I am unsure if there are similar ones in HTML): Scale to Fill, Aspect Fit, Aspect Fill https://i.stack.imgur.com/5X83I.jpg ...

Is there a way to adjust the card quantity in a Bootstrap row using media queries?

<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12" *ngFor="let card of cards"> This specific div is responsible for containing the various cards that are visible on the webpage. Depending on the screen size, a different number of ...

You can see the JavaScript code directly in the browser

I'm completely puzzled as to why this is happening. How strange that the JavaScript code is visible on the browser, almost appearing like regular text on the web page. This mysterious occurrence seems to be exclusive to Firefox. Despite scouring vario ...

How can I change the hover color of the navigation bar buttons in the Bootstrap freelancer template?

Having an issue with the hover color of the main navigation button menu while using the official freelancer template: https://github.com/BlackrockDigital/startbootstrap-freelancer The main button is displayed on narrower screens and should have a hover c ...

What is the best way to clear a canvas when the erase button is clicked using sketch.min.js?

I incorporated the sketch.min.js file obtained from this source. The functionality I aimed for was that clicking on the Eraser link should directly erase the canvas. To avoid the need of clicking twice on the eraser link and canvas, I required that a sing ...

Utilizing jQueryUI for rendering tabbed content with JSON data

Although I have the ability to load JSON data that is returned (appearing "as is" in my HTML page), I am facing an issue where the raw JSON data does not disappear. The code I have used is as follows: $( "#tavole" ).tabs({ cache : false, event: "m ...

Make the minimum height of one div equal to the height of another div

My query is somewhat similar to this discussion: Implementing a dynamic min-height on a div using JavaScript but with a slight twist. I am working on a dropdown menu within a WordPress site, integrated with RoyalSlider. The height of the slider div adjust ...

Is it possible to verify the compatibility of certain at-rules in CSS?

I am in search of a counterpart to @supports specifically for at-rules. I desire the ability to execute: @supports (@counter-style) { things } My goal is to achieve this using only CSS/HTML. Can anyone guide me on how to accomplish this task? ...

An interesting quirk within CSS is that when a field is hidden, it can

I am currently developing a Chrome extension to modify certain text fields, but I have run into an issue. Specifically, I need to hide the "Stato" field so I implemented the following CSS code: https://i.stack.imgur.com/9blvz.png https://i.stack.imgur. ...

UI-Select fails to display the already selected value

Does anyone have a solution for binding a UI-Select control to a Class[] list? The filling up part works fine, but I'm having trouble getting the selected item to display. Any suggestions on how to fix this? Below is my ui-select code: <ui-select ...

Refresh a separate table following an insertion by utilizing a jQuery button coupled with PHP 7

This is a snippet of my jQuery code that inserts data into the database when a button is clicked $(function(){ $(document).on('click', '#addRefBtn', function(e){ var input = $('#qr_ref').val(); var po = $('#get_po& ...

Positioning a div with a set size to the right while allowing a div with a variable width to the left

Even though I achieved the desired outcome in this example (http://jsfiddle.net/jcx3X/40/), I am intrigued by the reasons why this other scenario (http://jsfiddle.net/jcx3X/41/) does not work. What is the significance of the div being floated first in th ...

I can only use innerHTML once in my React application

I am attempting to clear my container div when the user clicks the search button in order to remove all existing search results. However, I am encountering an issue where using innerHTML to clear the container div only works the first time. If a second sea ...

Pass information to a PHP file using JavaScript when the form is submitted

Essentially, I am looking to extract values from various inputs and spans using JavaScript when the submit input is clicked. These values will then be sent to PHP using $post in order to ultimately send them via email. Previously, I tested replacing all of ...

Unable to retrieve the name value of the option_id in jQuery due to an

<script type="text/javascript> $(document).ready(function(){ $('#btnSubmit').bind('click', function(){ $('.hiddenId').each(function(){ var id = $(this).val(); var option = $('#option_&apos ...