Adjust the width of the table data cells dynamically in HTML and CSS based on the screen resolution and content

My React JS app has a page that features a table with tbody, tr, and td elements. The table consists of only two columns represented by the td tags. I am trying to make the first column adjust automatically based on screen resolution and content. I have experimented with width:"fit-content" and width:"auto", but both options seem to set the width at about 50% of the table's total width. When I switch it to width:"20%", it works better but still doesn't adjust correctly for different resolutions. I have searched through other posts without finding a solution that works for me. Below is the code I have tried:

<table className="myTable">
  <tbody>
    <tr>
     <td width="fit-content">
       <control1/>
     <td width="auto" > 
       <control2/>
     </td>
    </td>
  </tbody>
</table>

//css
.myTable
  table-layout: fixed
  width: 100%

After experimenting with table-layout settings, I realized auto setting causes my table to run off the page, so I need to keep it fixed. However, the width of the first td element does not accurately adjust to its content.

Answer №1

One possible solution is to use <td style="width:20vw"> in order to set the column width to 20% of the viewport's resolution.

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

Sending the complete data from an HTML table to a web method

Is there a way to pass the data from a dynamically generated HTML table grid to a web method using jQuery AJAX? The table grid creates multiple rows on button click at runtime, and I need to send the entire table data. Please refer to the following snaps ...

Finding the file path to a module in a NextJS application has proven to be a challenge when utilizing the module

Currently, I am utilizing the webpack plugin module-federation/nextjs-mf, which enables us to work with a micro-frontend architecture. Based on the official documentation and referencing this particular example, it is possible to share components between ...

The issue arises when trying to use qtip2 in JavaScript upon page initialization

I successfully created a heatmap using JavaScript, utilizing a table with varying colors based on the displayed values' thresholds. To enhance user experience, I wanted to implement a popup window that shows additional information when hovering over a ...

Minimize external requests when loading a page within an iframe

My iframe is set up to load dynamic pages that may contain references to external resources like JavaScript files or images. This causes the iframe to attempt downloading these external resources from the internet, resulting in frequent HTTP 404 errors. ...

Having trouble establishing a connection between React and Graphcool backend

Currently, I am working on setting up a basic project using React and Graphcool. I have successfully set up the Graphcool backend and can access the playground at: When running this query in the playground, I can see results: query { allGroups { ...

Zurb's Vertical Navigation Bar: A Stylish and Functional Design

I attempted to replicate the left navigation bar from this link: The responsive navigation bar and contents on the right caught my attention. Currently, I am working on a project that requires a similar navigation bar. While I am proficient in HTML and C ...

Troubleshooting a React app deployment on Heroku: Dealing with the JS heap overflow

Seeking Solution: I have been facing a challenge while trying to deploy my React application on Heroku. Despite successful build and deployment, the app crashes immediately upon startup. Upon investigation, I suspect that the issue may be related to the s ...

Error: Attempting to access properties of an undefined object (specifically, the 'prototype' property) within a React application

I encountered an error while working on my React application: TypeError: Cannot read properties of undefined (reading 'prototype') (anonymous function) C:/Users/tatup/Desktop/GrowApp/frontend/node_modules/express/lib/response.js:42 39 | * @pub ...

Proper techniques for enlarging a background image using CSS

When utilizing the CSS scale() function along with a transition and transform on a div element that contains a high-quality background image, I noticed that the image becomes blurry when zoomed in. How can I achieve a smoother zoom effect for high-quality ...

What is the best way to merge two JSON arrays using Axios in a React project?

I am currently working on getting data using axios React from Laravel, but I am facing some challenges in combining two arrays by their respective Ids. In my case, I am trying to retrieve product data and images from the Laravel Controller. Can anyone prov ...

"Troubleshooting a JSON structure containing a complex array of objects with multiple layers

I've structured a complex array with JSON objects to allow users to customize background images and create unique characters. Below is the main code snippet: var newHead; var newBody; var newArm; var masterList = [ { {"creatureName":"Snowman ...

Learn the best way to utilize a stylus in Vue files to interact with JavaScript variables

For instance: <script> export default { data() { return{ varinjs: 1, } } } </script> <style lang="stylus"> varincss = varinjs body if varincss == 0 ba ...

Explore the possibilities of using a unique custom theme with next.js, less, and ant design

Trying to customize the default theme in antdesign has been a challenge for me. I've switched from sass to less, but there seems to be something that just won't work. I've exhaustively searched online for solutions - from official nextjs ex ...

Android mobile browser lacks visibility of certain elements

please provide a brief description of the image The issue I am facing is consistent across all mobile browsers, with the exception of Firefox. It also manifests in Windows Chrome devtools mode and when the device toolbar is activated. I came across a sim ...

Utilizing jQuery to convert object properties into a table

Here is the table structure I am working with: <table> <thead> <tr> <th>Loan Type</th> <th>Amount Borrowed</th> <th>Current Payment< ...

Achieving distinct CSS margins for mobile and desktop devices in a Django template without the need for multiple {% block content %} statements

Is there a way to dynamically change the margin-left of my main {% block content %} in the base.html template based on whether the viewer is using a mobile or desktop device? This is how my current base.html looks like: <div class="content container-f ...

What causes a header to appear transparent when it has a background color set in HTML?

I'm attempting to create a webpage that resembles Gmail. I have a header with a background color, but when I scroll, the content in the body becomes visible. Here is the view without scrolling: https://i.stack.imgur.com/UQ2sO.png However, while scr ...

Avoid triggering a keydown event if certain div elements have been clicked

When the user presses the space bar, my script is set up to perform certain actions, such as stopping an audio player: $('html').keydown(function(e){ if(e.keyCode == 32){ // Stop the audio player } } However, an issue arises when a ...

Typescript error: The argument containing 'username' and 'password' fields cannot be assigned to a parameter expecting only a 'string' type

My service is designed to take an endpoint and execute a HTTP call. Here is the code snippet: export const updatePassword = (data: { username: string; password: string; }): HttpResponse<string> => { const { usernam ...

Creating your own custom selection is an exciting opportunity to personalize your choices

I am interested in creating my own customized select element. Currently, the design of my select element is as follows: https://i.sstatic.net/eQNXb.png JS Fiddle This is the code for how it is currently implemented: <p>Title</p> <form&g ...