How can we ensure that the width of the td element matches the width of the input element using only CSS or

I have attempted to inquire about this question before but did not receive any helpful responses, so I am trying again in the hopes that someone can assist me.

My desired outcome is as follows:

  • The table should stretch to 100%
  • The first input element should "fill" the table since it needs to be at 100%
  • The other input elements should be of different sizes
  • The td elements should match the width of the input elements with no space in between
  • This should be achieved by only making changes to the CSS (no JavaScript or HTML modifications)

In the code snippet provided below, you can view my HTML table. I do not wish to alter the structure in any way, and adding classes or using JavaScript is not an option for me.

When you run the code snippet, you will notice the issue I am facing – the td elements are wider than the input element. My goal is for the second, third, and fourth tds to adjust their widths to match those of the input elements while allowing the first td to expand (as the table must be at 100%).

table {
  width: 100%;
  border-collapse: collapse;
}

td {
  border: 1px solid #000;
}

.small {
  width: 20px;
}

.medium {
  width: 40px;
}
<table>
<tbody>
  <td>
    <div>
      <input type="text" /> 
    </div>
  </td>
  <td>
    <div>
      <input class="small" type="text" /> 
    </div>
  </td>
  <td>
    <div>
      <input class="medium" type="text" /> 
    </div>
  </td>
  <td>
    <div>
      <input class="medium" type="text" /> 
    </div>
  </td>
</tbody>
</table>

I am aware that setting the width on the td elements instead of the input could solve this problem, but that is not the solution I am seeking. I also understand that adjusting the table width to auto could work, but that is not the approach I want to take. My objective is to make the td elements match the width of the input elements using only CSS.

Answer №1

Have you achieved your desired outcome? I have set the first td and input to a width of 100%, allowing them to fill the remaining space in the table after all other divs have adjusted their width based on the elements inside (as defined by your class). By using box-sizing: border-box, you can ensure that borders do not affect the width of the elements. Another option is to apply the box-sizing property specifically to the table and all its elements with

table * { box-sizing: border-box; }
.

* {
  box-sizing: border-box;
}
table {
  width: 100%;
  border-collapse: collapse;
}
td {
  border: 1px solid #000;
}
table td:first-child,
table td:first-child input {
  width: 100%;
}
.small {
  width: 20px;
}
.medium {
  width: 40px;
}
<table>
  <tbody>
    <td>
      <div>
        <input type="text" />
      </div>
    </td>
    <td>
      <div>
        <input class="small" type="text" />
      </div>
    </td>
    <td>
      <div>
        <input class="medium" type="text" />
      </div>
    </td>
    <td>
      <div>
        <input class="medium" type="text" />
      </div>
    </td>
  </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

Storing User Information in Cookies using jQuery

My current jQuery script is linked to a button and it functions by toggling font styles on a website by enabling or disabling a stylesheet upon clicking the button. Now, I want to enhance this functionality by saving user preference to a cookie so that the ...

How can I confirm that all elements have been properly reset to their original positions prior to making any further adjustments to them?

In need of some creative brainstorming, I'm working on a website design featuring ten overlapping cards. When the mouse hovers over a card, it expands while others move away. My goal is for each card to return to its original position once the cursor ...

Specify touch event regions with the ngTouch directive

I recently implemented ngTouch in my AngularJs web app to detect swipe left and right gestures. I am using this feature to open and close a side bar menu. Currently, the swipe events are linked to the entire wrapper like this <div ng-style="body_st ...

Methods for optimizing a web page's targeting to a specific geographic region

I have been working on creating my own website hosted on a server at home. It's not anything too big, but now I want to make it beta so that my friends can test the functionalities and provide feedback for improvement. However, some of my friends do ...

Mathjax3 causing bootstrap columns to become overwhelmed

Currently, I am utilizing MathJax3 in conjunction with Bootstrap4. It seems that the feature of automatic line breaking has not yet been incorporated into MathJax3. The code snippet below showcases a layout consisting of 3 columns: a left spacing column, ...

Unselect all options in Angular's multiple selection feature

Objective: My goal is to ensure that when I invoke the myMethod() function, all options are unselected. Current Issue: Currently, calling myMethod() will only deselect the last option, leaving the others selected if they were previously selected. Possibl ...

Storing the chosen item in a PHP drop-down menu

Is there a way to keep the selected value of a drop down when submitting a form with an onchange event? This is my current code: echo "<form method=\"post\"> <select name=\"Color\" OnChange=\"this.form.submit();\"&g ...

I'm working on creating a login page with Bootstrap. Does anyone know the best way to center it on the screen

For a school project, I am working on creating a sign-in page. Although I have come across this code, it does not appear at the center of the page as intended. Instead, the left side displays to the left while the right side shows up to the right. My goal ...

steps for incorporating a new style in a django project

I am looking to change the row color in a table with a class. How can I add HTML classes dynamically based on certain conditions? Also, I am using Bootstrap. models.py class Car(models.Model): name = models.CharField(max_length=20) color = models. ...

What is the best way to switch text color when the background changes?

Is there a way to dynamically change the color of a text logo to make it readable over differently colored backgrounds, such as on a slider or scroll down effect? While one approach could involve finding the dominant image color, this may be slow and ineff ...

Can you provide me with instructions on how to change the background image?

Having trouble with the CSS code I wrote for a background-image. It's not showing up when I set it in my CSS stylesheet. I tested it and found that it only works when written in the body... <body background="Tokyo.png"> ...but not in the styl ...

Which IDE is the top choice for HTML5 when programming with .NET on the server side?

Is Visual Studio 2010's support for JavaScript and jQuery enough to make it a top choice for working with JS/HTML5? Or are there other IDEs out there that offer better options? I typically use .Net for the server side of my projects, so Visual Studio ...

React Native: Enhance the background with a vibrant stripe of color

I am trying to incorporate a strip of grey in the middle of my white background, but I am encountering an issue where I am unable to input any text inside it. Below is the code snippet I am working with: container: { flex: 1, justifyContent: "cent ...

What's the reason for text-alignment not functioning properly?

After some testing, I have come up with the following code: .Greetings { width:100%; display:block; text-align:center; font-family: "Times New Roman", Times, serif; font-size:75px; color:#208CB7; } The objective was to center the text on the ...

"Enhance Your Website with Custom CSS Backgrounds

Can anyone provide information on the popular CSS style currently seen in many websites? This style features a full-screen background with the website's content displayed on top under smaller dimensions. An example of this can be seen on a website li ...

Template is unable to locate form data within the view

Having gone through the django tutorial and attempted various form-handling methods in django, I'm at a loss as it doesn't function as desired. Despite extensive research - searching on Google and StackOverflow - I simply cannot get it to work. P ...

I'm looking to add a next and previous button within my jumbotron- can anyone offer guidance?

As I near the completion of my Full Stack Nanodegree final project and await assistance from my instructor, I've taken on the task of developing my portfolio. However, I'm facing a challenge with implementing next and previous buttons within my j ...

"Deleting a line from a text file in PHP with a predetermined format: A step-by-step guide

I am facing a situation where I need to manipulate a CSS file through PHP. The CSS file contains the following properties: #firstdiv { width:100%; height:200px; } #seconddiv { width:80%; height:70px; } #thirddiv { width:80%; height:70px; } #firstdiv { col ...

Customizing the size of the selectInput widget in R/Shiny

I'm trying to adjust the size of the selectInput() widget in shiny using selectize.js. I've attempted to tweak various attributes listed on this page (https://github.com/selectize/selectize.js/blob/master/dist/css/selectize.css) but I can't ...

A `<div>` element placed outside of a `<button>` element

I'm currently facing an issue while trying to validate a mobile dropdown in an HTML5 page. Upon validation, I encountered an error stating that div elements are not allowed inside buttons. When attempting to rectify this by wrapping the button with a ...