Ensuring consistent input width in a table for all columns in reactjs and bootstrap, regardless of their content

I'm currently designing a bootstrap table in reactjs, complete with a filter beneath each column header.

Take a look at the code snippet below:

<table className="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th onClick={() => this.handleColumnSort("id")}><b>Id</b> <i className={`fa fa-fw ${this.handleColumnSortCss("id")}`}></i></th>
      <th onClick={() => this.handleColumnSort("name")}><b>Name</b> <i className={`fa fa-fw ${this.handleColumnSortCss("name")}`}></i></th>
      <th onClick={() => this.handleColumnSort("munit")}><b>Munit</b> <i className={`fa fa-fw ${this.handleColumnSortCss("munit")}`}></i></th>
      <th onClick={() => this.handleColumnSort("rate")}><b>Rate</b> <i className={`fa fa-fw ${this.handleColumnSortCss("rate")}`}></i></th>
    </tr>
    <tr>
      <th></th>
      <th><input type="text" onChange={(e) => this.onChangeHandler("id",e)}/></th>
      <th><input type="text" onChange={(e) => this.onChangeHandler("name",e)}/></th>
      <th><input type="text" onChange={(e) => this.onChangeHandler("munit",e)}/></th>
      <th><input type="text" onChange={(e) => this.onChangeHandler("rate",e)}/></th>
    </tr>
  </thead>
  <tbody>
  {this.props.items.filtereditems.map((item,index) => (
    <tr key={index} >
        <th> {(index+1)+((this.props.page_number-1)*(this.props.page_size))}</th>
        <td> {item.id}</td>
        <td> {item.name}</td>
        <td> {item.munit}</td>
        <td> {item.rate}</td>
    </tr>
    ))}
  </tbody>
</table>

Here is the resulting output: https://i.sstatic.net/U9CA2.png

However, it has been observed that the input elements affect the layout negatively. They tend to expand and do not fit properly within their designated space.

https://i.sstatic.net/IJlY8.png

Is there a way to retain the intended layout while preventing the input fields from expanding too much?

Answer №1

It seems like what you're looking for is to enhance the appearance of your <input /> element by applying Css styling with fixed width or padding. Learn more about the css box model here

Answer №2

To achieve the desired outcome, consider adjusting the width of the name column to 100%.

Expanding the name column will automatically reduce the width of other columns, ensuring fluid widths are maintained.

 <table>
  <thead>
    <tr>
      <th>#</th>
      <th>Id</th>
      <th>Name</th>
      <th>Munit</th>
      <th>Rate</th>
    </tr>
    <tr>
      <th></th>
      <th><input type="text" /></th>
      <th><input type="text" /></th>
      <th><input type="text" /></th>
      <th><input type="text" /></th>
    </tr>
  </thead>
  <tbody>
      <td>1</td>
      <td>100</td>
      <td>Name</td>
      <td>100000</td>
      <td>Rate</td>
  </tbody>
</table>

To demonstrate, you can target the name column using this CSS:

table th:nth-child(3),
table td:nth-child(3) {
    width: 100%;
}

It is important that the table element has the following rule in place:

table {
    table-layout: auto;
}

View a live demo on Fiddle: https://jsfiddle.net/49fach5w/

If you are utilizing Bootstrap, you can utilize the helper class:

w-100

Apply this class to any table cell containing 'name' data, such as:

<th class="w-100">...</th>

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

I'm wondering if there is a specialized event for leaving the "select scroller" interface on iOS that is specific to vendors

Recently, I encountered a problem with iOS where tapping on a <select> box triggers the "scroll wheel" interface, pushing the modal being accessed upwards. While this behavior is acceptable, the issue arises when the modal fails to retain its origina ...

Using jQuery to refresh a div element

I am struggling with updating the "right" div in my jsp page using the script below. Despite being contained in a single file, the update does not seem to work. Any assistance is appreciated. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E ...

Tips for aligning content vertically in bootstrap 4

Looking to vertically center content in Bootstrap 4? <div class="container"> <div class="content"> <div class="row"> <div class="col-6 align-middle" ...

What is the best way to retrieve an object within a intricate JSON document like this one?

Attempting to access the events object array from the JSON file obtained through the API at this URL: http://app.ticketmaster.com/discovery/v1/events.json?keyword=Madonna&apikey=fU1iKOy83W5ewGCIGQmW8I6FIPvOG8de An attempt was made to access the events ...

What are the steps to apply material-ui's error-themed style (color) to non-form text?

When it comes to using error styles for form inputs like text fields, the documentation is straightforward. But what about applying the same style to a custom element, such as a text label for a file upload button or another unique component that doesn&apo ...

Is it possible to exclusively target a child div using JavaScript in CSS, without affecting the parent div?

I'm in the process of developing a calendar feature where users can select a specific "day" element to access a dropdown menu for time selection. The functionality is working fine, but I've encountered an issue. When a user selects a time from th ...

Notifying a Child Component in React When a Props-Using Function Resolves a REST Call

When I submit an item or problem, I trigger a function in the parent component that has been passed down to the child component as props. After sending my information, I want to clear the input fields. The issue is that I am clearing the input fields immed ...

Creating a loading animation using HTML and CSS for textual content

I came across a website that had a unique text loading effect, but despite my efforts, I couldn't find what it's called. Effect: https://i.stack.imgur.com/NyaSN.png Is there any plugin or CSS file available for this effect, similar to Bootstra ...

Markers on Google Maps in React JS only appear upon page refresh

Greetings for taking the time to review this. I am fetching data through an AJAX call to a geocoder and then to google maps to display markers. The map loads correctly and the data is passed to the markers, but only when I manually refresh the page. I have ...

Central peak in an expansive sphere

I'm attempting to mimic the design shown in this visual representation: While I am familiar with how to generate a semi-circle using CSS, my goal is to only create the top central segment of a larger circle. Specifically, I am seeking the CSS code n ...

Changing the Direction of News Ticker Movement from Right to Left

I need to switch the news ticker movement direction from right to left to left to right because I will be using Arabic language, so it is crucial to change the movement direction. Despite trying for several days, I have been unable to find a solution. HTM ...

Is it possible to create a dynamic <hr /> using Flexbox?

In the midst of working on my application, I managed to achieve a simple layout. However, there is one peculiar requirement that has been eluding me for the past 2 days and now I find myself in need of assistance. Here is the current HTML code I am workin ...

Exploring the utilization of a constant value within a styled component

I am looking to pass a styled component to a variable and then utilize that variable in the rendering process However, I am unsure of how to go about doing this. The main reason for passing it through a variable is for conditional purposes. Sample.styled ...

Updating the store in Redux does not trigger a rerender of the components

After updating the Store and checking my console, I noticed that the content of the component did not change. The issue seems to be with the rendering of the Question in my render function. Below are my reducers where I attempted to update the question[0] ...

Mouseover feature for image is functioning, but having issues with alignment

I am currently working on displaying images upon mouse over actions. While the functionality is working perfectly, I am facing an issue where the displayed images appear below and the last image takes up space at the bottom. To rectify this problem, I woul ...

In what ways can a distant ancestor influence its subsequent generations down the line?

As a beginner utilizing Bootstrap v5, I am in the process of creating a navbar using the following HTML code. Within this code, the parent element (ul) of my link items contains a class called "navbar-nav" which results in my link items being styled to s ...

Creating Interactive Labels with React-Three-Renderer (Example code provided)

Currently, I am utilizing react-three-renderer (npm, github) to construct a scene using three.js. In my project, my objective is to create a label that consistently faces the camera by using <sprite> and <spriteMaterial>, inspired by stemkoski ...

Creating a new row using ngFor is a straightforward process that allows for dynamic

I need some help figuring out how to create a new row using *ngFor in Angular. Here is the code snippet I am working with:<div class="row"> <div class="col-lg-3" *ngFor="let user of users">{{user.username}}</div> </div> If the nu ...

Tips for resolving parsing errors when exporting types in a Create React App TypeScript setup

I created an application using the CRA TypeScript template. However, when I tried to use this code snippet, a parsing error occurred. export type { BaseModalProps } from "./BaseModalProps" Parsing error: Declaration or statement expected The f ...

Display buttons when hovering with React

Seeking assistance with implementing functionality in a React application where buttons for editing and deleting display only when the mouse hovers over the corresponding row. Currently, the implemented code displays these buttons in all rows on hover. Sn ...