What is the best way to adjust column width in an HTML table using Bootstrap 4 and HTML helpers?

I've created a simple table with two columns and an additional button in the body's td elements:

<table class="table table-hover">
    <thead class="table-info">
        <tr>
            <th class="text-center">
                @Html.DisplayNameFor(model => model.Code)
            </th>
            <th class="text-center">
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                @Html.EditorFor(model => model.Code)
            </td>
            <td>
                @Html.EditorFor(model => model.Name)
            </td>
            <td>
                <button type="button" class="btn btn-danger">Delete</button>
            </td>
        </tr>
    </tbody>
</table>

The visual output can be seen here: image

However, I'd like the first column to occupy only 25% of the total table width. I have tried setting the th element with this value but it didn't work. Here is what I attempted:

<th class="text-center" style="width:25%;">
    @Html.DisplayNameFor(model => model.Code)
</th> 

I am using ASP.NET Core 2.1 + MVC + Razor.

It seems that the issue lies in the ASP.NET HTML helpers. How can I achieve the desired outcome?

Answer №1

If you are looking to create a table and manage its columns using HTML

Otherwise, I recommend designing the table with Bootstrap to leverage the features of Bootstrap

https://i.sstatic.net/40zP1.jpg

Here is the source code for utilizing Bootstrap 4:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid bg-light">
<div class="table col-12 mt-3 pt-2">
<div class="row table-info">
<div class="col-3 font-weight-bold text-center py-2">
Code
</div>
<div class="col-7 font-weight-bold text-center py-2">
Name
</div>
<div class="col-2 font-weight-bold text-center py-2">
Action
</div>
</div>
<div class="row d-flex flex-row justify-content-around py-2">
<div class="col-3">
<input type="text" class="h-100 w-100 px-2 border border-dark" value="User1" />
</div>
<div class="col-7">
<input type="text" class="h-100 w-100 px-2 border border-dark" value="User2" />
</div>
<div class="col-2">
<div class="btn btn-success w-100 text-center">Save</div>
</div>
</div>
<div class="row d-flex flex-row justify-content-around py-2">
<div class="col-3">
<input type="text" class="h-100 w-100 px-2 border border-dark" />
</div>
<div class="col-7">
<input type="text" class="h-100 w-100 px-2 border border-dark" />
</div>
<div class="col-2">
<div class="btn btn-danger w-100 text-center">Delete</div>
</div>
</div>
</div>
</div>

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

Issues arise: Bootstrap unexpectedly fails in my React JS project

Unexpectedly, bootstrap has ceased to function on my current project. The reason behind this sudden issue is unknown to me at the moment. Below is a snippet from our package.json file: { "name": "blankets", "version": "0.1.0", "private": true, ...

Retrieving the result of a LINQ query within a repository

How can I create a method inside an interface like this? List<T> OngoingEvent(); I have implemented the method inside the repository as follows: public List<T> OngoingEvent() { var output2 = DbContext.ParticipantsTable.Join(DbContext.Even ...

Looking to streamline your design? Find out how to translate multiple elements to the same position seamlessly

Greetings! This is my debut post on this platform. I am currently engrossed in a project that has presented me with a challenge, and I'm reaching out to seek your insights on a potential solution. Here's the scenario: I have a parent div containi ...

Is there a way to tally the checked mat-radio-buttons in Angular?

I am seeking a way to determine the number of radio buttons checked in a form so I can save that number and transfer it to a progress bar. <mat-radio-group name="clientID" [(ngModel)]="model.clientID"> <mat-radio-button *ngFor="let n of CONST ...

Can a Data Table and a grid be combined within an MVC View?

Is it possible to display data from a database table in a 2x3 grid format, which can be paginated for easy navigation without having to list everything vertically? I currently have the following code that displays all the data one after the other, but I wo ...

Tips for creating a "sticky" button in Angular that reveals a menu when clicked

Is there a way to incorporate a feature similar to the "Get help" button on this website: The button, located in the lower right corner, opens a sticky chat menu. I am interested in adding dynamic information to the button (such as the number of tasks a u ...

Utilizing jQuery fadein effect to reveal a set of links upon the page's initial load

Is it possible to utilize jQuery to smoothly fade in the links within a navigation div when the page loads? Currently, the links are styled with an a tag and have individual IDs. #navtop a {etc.} Do I need to convert them to a class, add another class to ...

Enhance the appearance of the og:image by applying CSS styling

Can CSS styling be applied to a dynamic facebook og:image used for Facebook shares? Instead of using a static image, I'd like to add some custom styling such as a colored block in the top right corner with styled text. Currently, my tag simply grabs ...

I want to know how to move data (variables) between different HTML pages. I am currently implementing this using HTML and the Django framework

I am currently working on a code where I am fetching elements from a database and displaying them using a loop. When the user clicks on the buy button, I need to pass the specific product ID to another page. How can I retrieve the product ID and successful ...

What are some strategies for creating a distinctive list design using markup?

Possible Duplicate: Achieving sub numbering on ol items html I am attempting to create a format similar to the one below using an unordered list: 1. Title Objective: some text here... 1.1 option1 1.2 option2 1.3 option3 2. Title ...

Slide toggle in jQuery reveals itself briefly before vanishing

Check out the codepen here: https://codepen.io/anon/pen/gRJEwx Essentially, with jQuery toggle 'slide', the div slides in and right back out. It seems to only happen every second or third time the button is pressed. The first try never triggers ...

The movement of the Bootstrap navbar brand causes a shift in adjacent elements

Is there a way to use the bootstrap navbar brand without it affecting the layout of other elements? I'd like to have the icon aligned to the left and the navbar elements perfectly centered. Alternatively, is there a way to include the element as a n ...

Issues with Skrollr JS on mobile device causing scrolling problem

Currently facing an issue with Skrollr js. It is functioning perfectly in web browsers and mobile devices. However, there seems to be a problem when tapping on a menu or scrolling down arrow as it does not initiate scrolling. Assistance would be greatly ...

Is it true that event.stopPropagation does not function for pseudoelements?

I am facing an issue with event handling in my ul element. The ul has three li children elements, and the ul itself has a useCapture event handler for click. In the click event handler, I successfully stop the event using event.stopPropagation(), and every ...

Vue Template ref Error: Unable to access scrollHeight property of null object

I'm currently working on creating a responsive sidebar in Vue, and I encountered an issue after refactoring my project to remove the state.js file. The problem is related to the dropdown/collapse elements, and it's throwing a TypeError: Cannot re ...

Tips for organizing a dashboard design with Bootstrap

I am looking to design a dashboard using the Bootstrap grid system. Here is the layout I have in mind: https://i.sstatic.net/cqWP5.jpg What would be the optimal way to structure this in the HTML file? Would a layout of 5 rows and 2 columns work best? Th ...

Can you inquire about utilizing advanced search features when a user selects all options?

Can anyone help with advanced search queries when all options are selected by the user? My form consists of 2 drop-down menus and 1 submit button. ________________ ________________ body : |______All_______| color : |______All_ ...

Are the JQuery appended elements exceeding the width of the parent div?

I have an HTML div that is styled using the Bootstrap class span9. <div class="span9"> <div id = "selectedtags" class="well"> </div> <button class="btn" type="button" id="delegatecontent">Generate report</button&g ...

If there is no content present, the html element with contenteditable will have an <br>

I'm encountering an issue with a <div contenteditable="true"></div>. Whenever I enter content into it and then delete that content, the browser seems to automatically insert a <br> element into the div. Has anyone else experienced th ...

What is the best way to deactivate div elements once an overlay has been applied to them?

My goal is to place an overlay on my form to prevent users from accessing the content. Even though I have added an overlay, users can still interact with input fields. How can I prevent that? .overlay { background: rgba(0, 0, 0, .75); text-align: ce ...