Adjusting table column widths in Bootstrap 4

I am completely new to bootstrap and am in the process of enhancing an existing MVC web application that is already utilizing bootstrap 4. My goal is to incorporate a table with form fields, but I am facing difficulties in controlling the column widths of the table. Particularly, one of the drop down lists has lengthy options, and I would like to limit the size of that column and manage text overflow/wrapping within the drop down list. However, I am finding it challenging to even size an empty table properly. The structure of the page includes a dummy table that mirrors the layout of the actual one.

<div class="form-horizontal col-10 offset-1">
    <div class="row">
        <h5 class="mt-n1 pl-1">Section title</h5>
    </div>
    <div class="row" style="background-color:#e8eef4; border: 1px solid #d2d2d2; border-radius: 3px; padding-top: 4px;">
        <table class="table table-borderless table-condensed">
            <thead>
                <tr>
                    <th class="col-3">Column 1</th>
                    <th class="col-6">Column 2</th>
                    <th class="col-3">Column 3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <select class="form-control">
                            <option>Select...</option>
                            <option>Option 1</option>
                            <option>Option 2</option>
                        </select>
                    </td>
                    <td>
                        <select class="form-control">
                            <option>Select...</option>
                            <option>An example of the really long text for the select option that will need to overflow.</option>
                            <option>Another example of the really long text for the select option that will need to overflow.</option>
                        </select>
                    </td>
                    <td>
                        <textarea class="form-control" cols=20 rows=2></textarea>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

My attempts to control the column widths have not yielded the desired results. I have come across suggestions to include the container class in one of the divs for better control over column sizes, but I'm unsure of which div to apply it to. Additionally, I have read about using the table-responsive class, although I would prefer to avoid a scrollable table. Any guidance or assistance you can provide on this matter would be highly appreciated.

Answer №1

To ensure proper formatting, make sure to include col- for both <th> and <td> elements, not just <th>. Additionally, add d-flex to enable flex behavior.

Below is the updated version of the <table> code block:

<table class="table table-borderless table-condensed">
    <thead>
        <tr class="d-flex">
            <th class="col-3">Column 1</th>
            <th class="col-6">Column 2</th>
            <th class="col-3">Column 3</th>
        </tr>
    </thead>
    <tbody>
        <tr class="d-flex">
            <td class="col-3">
                <select class="form-control">
                    <option>Select...</option>
                    <option>Option 1</option>
                    <option>Option 2</option>
                </select>
            </td>
            <td class="col-6">
                <select class="form-control">
                    <option>Select...</option>
                    <option>An example of the really long text for the select option that will need to overflow.</option>
                    <option>Another example of the really long text for the select option that will need to overflow.</option>
                </select>
            </td>
            <td class="col-3">
                <textarea class="form-control" cols="20" rows="2"></textarea>
            </td>
        </tr>
    </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

What is the best way to retrieve the parent element quickly using Jquery?

html: <span class="ui-spinner ui-widget ui-widget-content ui-corner-all"><input class="spinner1 ui-spinner-input" id="q1" name="value" value="1" min="1" aria-valuemin="1" aria-valuenow="2" autocomplete="off" role="spinbutton"><a class="ui ...

Tips for stopping an image from stretching the cell in flexbox using CSS

When using Flexbox to style elements, the width of the parent should be determined by the text inside child1. The image inside child2 should grow proportionately as the parent grows. However, it is important to ensure that the parent div does not exceed it ...

Dynamic color change in SVG

I am facing an issue with changing the color of svg images in a menu list using the filter CSS property. Even though I have obtained the correct filter value from a library that converts hex to CSS filter, React seems unable to set this property on the ima ...

What is the title of this particular CSS method?

I've been implementing a unique approach for more than a year now, and I have yet to come across similar practices elsewhere. Essentially, I am structuring display "states" or "modes" by utilizing CSS classes. Despite searching for terms like "css mod ...

Avoid Refreshing the Page When Pressing the Like Button

I've been working on code for liking a post and saving the value to a database using server-side code. However, I'm running into some issues when the page refreshes after clicking the like button. I tried using event.preventDefault() in my JavaSc ...

JS and its dynamic color changes

A game has been developed using JavaScript and HTML. The game features a table with cells that are assigned IDs for toggling colors using an onClick function. The objective is simple: when a cell is clicked, all neighboring cells, including the clicked one ...

How can you implement a switchable theme feature similar to Twitter on your website?

As a beginner programmer and college student with limited design knowledge, I am working on creating a microblogging website similar to Twitter. I noticed that some websites use pre-loaded styles or themes without separate CSS files. I am unsure of how t ...

Which CSS preprocessor is the best choice for WordPress: SASS or

After comparing SASS and LESS, I found that SASS is the clear winner in my opinion. Unfortunately, setting up SASS on my server requires ruby, gems, and other tools that I don't have access to. On the other hand, it seems like adding LESS to my proj ...

The text in my image is positioned away from the top

Hi there, I am new to exploring HTML and CSS and I have been trying to display some images and text on a webpage. My goal was to have the text appear next to the image, which is working fine. However, I am encountering an issue where the text aligns itself ...

What could be causing the sudden disappearance of the button?

There is an element with the ID "alpha" that contains the text "Now I'm here...". When the button is clicked, only the text should be removed, not the button itself. <div id="alpha">Now I'm here...</div> <button type="button" oncl ...

Unspecified variable in AngularJS data binding with Onsen UI

I am new to Onsen UI and AngularJS, and I have a simple question about data binding. When I use the variable $scope.name with ng-model in an Onsen UI template, it returns as UNDEFINED. Here is my code: <!doctype html> <html lang="en" ng-app="simp ...

Combining Angular and Bootstrap: how to create two overlapping divs

Looking to enhance a project in Angular 15 + bootstrap 5 by creating two draggable divs where the lower one can be vertically resized to overlap the upper one? I've managed to set up most of it, but when dragging the lower div, its transparency makes ...

Obtain the Class Name Value by Utilizing the "Begins With" Selection Method

Consider the following HTML structure: <a href="#" class="a b nl-3522">First</a> <a href="#" class="a b nl-7352">Second</a> <a href="#" class="a b nl-4874">Third</a> <!-- Note that classes nl-* are being added dynami ...

Unable to vertically align an image within a navigation bar item that is not the brand

Having a unique issue for which I cannot find a solution. In the navbar of Bootstrap 4, I am attempting to include an image next to one of the items, not the navbar-brand as commonly asked about. When Bootstrap 4 is loaded with a custom alpha dark theme, ...

Stopping the WordPress gallery at the end without looping using Elementor

Is there a way to make my gallery stop at the end of the last picture without having to add any jQuery code? I inserted this gallery using elementor widgets and would appreciate your help on this matter. Additional information: Upon clicking on an image, ...

Data within object not recognized by TableCell Material UI element

I am currently facing an issue where the content of an object is not being displayed within the Material UI component TableCell. Interestingly, I have used the same approach with the Title component and it shows the content without any problems. function ...

Looking to properly align the items in your navbar? Discover how you can achieve this effortlessly with HTML and the

I'm trying to create a navbar with the logo on the right side and other items like Home, Contact Us, About, etc. aligned towards the left. However, when I attempt to align the items, they all shift completely to the left. I want to evenly space them w ...

How to center text both horizontally and vertically in HTML

I am struggling with centering a background image along with a 1-line text vertically and horizontally inside a div. Although I have successfully centered the image, the text remains misaligned. I attempted using vertical-align: middle without success. Be ...

Reveal covert web data table

I am encountering difficulties while attempting to extract information from a table that does not seem to be visible. The specific table can be found on this page, within the 'Code History' section highlighted in light purple. Login credentials ...

Can you use CSS to target a parent div based on the elements it contains?

If I have the following HTML: <div class='row'> <div class='cell'> <span class='image'> </span> Image Cell </div> <div class='cell'> Regular Cell </div&g ...