Setting a minimum width for a single column in Bootstrap 4

After implementing a basic grid-based layout using Bootstrap 4, I encountered a challenge with the responsiveness of my columns.

My layout consists of two columns: Left and Right. The left column is defined as div.col-12.col-md-5, while the right column is set as div.col-12.col-md-7. The issue arises with the left column containing a table, which restricts its resizability. To ensure the table data is always displayed correctly, I decided to set a minimum width of 460px for the left column. However, I still want the right column to remain fully responsive and only stop resizing when the screen size falls below the medium breakpoint -768px-.

In an attempt to achieve this desired behavior, I wrote the following CSS:

@media (min-width: 768px) {
    .min-width-460-md {
        min-width: 460px;
    }
}

I then assigned the .min-width-460-md class to the left column, resulting in the following HTML structure:

<section class="container-fluid">
    <div class="row">
        <div class="col-12 col-md-5 min-width-460-md">
            First column (left) containing a table with a minimum width of 460px.
        </div>
        <div class="col-12 col-md-7">
            Second column with resizable data, not requiring a minimum width.
        </div>
    </div>
</section>

However, the application of the new class caused the left column to stop resizing at 460px, causing the right column to position itself below the left one, which is not the desired outcome.

My goal is for the left column to stop resizing at 460px while allowing the right column to keep resizing until the screen reaches less than 768px (md breakpoint).

Is there a way to achieve this using CSS and Bootstrap 4?

Thank you in advance!

Answer №1

To make things easier, you can utilize an auto-layout column (.col) on the right side...

<section class="container-fluid">
    <div class="row">
        <div class="col-12 col-md-5 min-width-460-md border">
            <table class="table table-striped">
                ..
            </table>
        </div>
        <div class="col bg-dark text-white">
            Second column (it contains more resizable data, so no min-width required here).
        </div>
    </div>
</section>

Check out the live demonstration here:

Answer №2

This issue is rather straightforward. The col-* col-md-* classes are not suitable for this task, making it difficult to achieve proper display.

Take a look at the code snippet below.

.new-section{
background-color: black;
color: white;
padding: 30px;
}
.new-section .row{
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
    -ms-flex-direction: row;
        flex-direction: row;
-ms-flex-wrap: wrap;
    flex-wrap: wrap;
    margin-left: -15px;
    margin-right: -15px;
}
@media (min-width: 768px){
.new-section .row{
-ms-flex-wrap: nowrap;
    flex-wrap: nowrap;
}
}
.new-section .row .min-width-460-md,
.new-section .row .right-column{
padding-left: 15px;
padding-right: 15px;
}
.new-section .row .min-width-460-md{
border: 1px solid #f2f2f2;
-ms-flex-preferred-size: 100%;
    flex-basis: 100%;
-webkit-box-flex: 0;
    -ms-flex-positive: 0;
        flex-grow: 0;
-ms-flex-negative: 0;
    flex-shrink: 0;
}
@media (min-width: 768px){
.new-section .row .min-width-460-md{
-ms-flex-preferred-size: 460px;
    flex-basis: 460px;
}
}
.new-section .row .right-column{
width: 100%;
}
<section class="container-fluid new-section">
    <div class="row">
        <div class="min-width-460-md">
            First column (left) which contains a table and needs to be min-width 460px.
        </div>
        <div class="right-column">
            Second column (it contains more resizable data, so no min-width required here).
        </div>
    </div>
</section>

This approach is not compatible with bootstrap@4. The following snippet provides the expected solution using bootstrap@4.

@media (min-width: 768px) {
        .min-width-460-md {
            flex-basis: 460px !important;
            max-width: 460px !important;
        }
    }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

    <section class="container-fluid">
        <div class="row flex-md-row flex-md-nowrap">
            <div class="col-12 min-width-460-md bg-secondary">
                First column (left) which contains a table and needs to be min-width 460px.
            </div>
            <div class="col-12 bg-warning">
                Second column (it contains more resizable data, so no min-width required here).
            </div>
        </div>
    </section>
    

Answer №3

Modify col-md-5 and col-md-7 to just col-md and eliminate the custom min-width-460-md rule. col-md will adjust automatically to screen width on smaller devices and equalize column widths as the screen size increases.

Apply the col-xl-5 class to the table column for large screens.

Press the "run code snippet" button below and expand to full page:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<section class="container-fluid">
    <div class="row">
        <div class="col-12 col-md col-xl-5 bg-secondary"> 
            The following line will give this div 460px min-width. <br>
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
           <br>
            First column (left) which contains a table and needs to be min-width 460px.
        </div>
        <div class="col-12 col-md bg-warning">
            Second column (it contains more resizable data, so no min-width required here). Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex totam qui voluptatibus reiciendis possimus laboriosam dolor libero alias quasi sequi at voluptas, porro, obcaecati amet minima fuga consectetur facilis eligendi.
        </div>
    </div>
</section>

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

Transforming hover interactions into onclick events involves adjusting the CSS and JavaScript

Is there a way to convert this hover menu into an onclick menu? <div id='cssmenu'> <ul> <li class='has-sub'><a href='#'><span>Users</span></a> <ul> ...

What is the best way to split an array into four columns and allocate 10 <li> items from the array to each column?

If I have a dynamic array with 40 elements that changes on every render, how can I loop through the array and return 4 groups of elements, each containing 10 items without any repetition? I want to style these objects in the array using a parent flex con ...

The Bootstrap Navbar appears hidden beneath other elements on mobile devices

While using bootstrap to style my header contents, I encountered a strange issue. The navbar that appears after clicking on the hamburger menu is displaying behind all the components. Even though I've set the z-index to the maximum value, it still doe ...

Pressing the reset button on a basic calculator

I am experiencing an issue with my calculator. It works fine initially, but after using the reset button, it stops functioning properly. Currently, I only have the multiplication set up as I debug this problem. I am new to JavaScript and would appreciate a ...

One way to generate div elements based on the number in an input field when a button is clicked, but ensuring it only happens once

What I am attempting to achieve is: Retrieve data from a JSON file upon button click. Display the data in separate boxes, each one different for every element of the array. For instance, if the JSON provides 3 rows of data, there should be 3 distinct box ...

Creating a slider directly under the header in an Ionic 3 application without any gaps

I need help with positioning a slider below the header within the ion-content. The issue is that I am experiencing unwanted white space on the top, left, and right sides, as depicted in the image. https://i.sstatic.net/HH1c6.jpg This is my HTML code for ...

Pure CSS navigation - radio buttons to display or hide different pages

I'm attempting to create a website navigation bar using radio buttons, where the pages are displayed based on the selection. So far, I've managed to achieve this using <a href='#div'></a> and a :target, by placing the .page ...

What is quicker: loading SVG images or requesting sprite images?

Is there a recommended approach for creating a basic shape and icon that is used in various sections of the website with different colors? Should I opt for SVG or sprites? However, I wonder if there is a universally accepted solution for this issue. ...

What is the best way to implement CSS Background-size: cover; while also making room for a menu?

Recently, I came across the CSS property background-size: cover, but I encountered a problem. I want the area behind my navigation on the left to be black. However, when I leave black space in the image itself, it gets cropped off when the image resizes. ...

Accordion menu causing Javascript linking problem

After following a tutorial, I managed to create an accordion menu in JavaScript. In the current setup, each main li with the class "ToggleSubmenu" acts as a category that can hide/show the sub-lis. Now, my query is this: How can I retain the link functio ...

Methods to Maintain Consistent HTML Table Dimensions utilizing DOM

I am facing an issue with shuffling a table that contains images. The table has 4 columns and 2 rows. To shuffle the table, I use the following code: function sortTable() { // Conveniently getting the parent table let table = document.getElementById("i ...

Problem with CSS dotted borders appearing as dashed in Chrome when applied to adjacent columns in a table

After testing the code on both Chrome and Firefox browsers, I noticed that they display the border differently. In Chrome, there is a dash in between adjacent cells while in Firefox, there are no dashes rendering. Can anyone suggest a solution to fix thi ...

css avoiding code duplication with nested classes

I created a custom CSS class named button.blue: button.blue { background-color: blue; ... } button.blue:active { background-color: darkblue; ... } button.blue:hover { background-color: lightblue; ... } To implement this class, I use the following code: ...

ReactJS Navbar component experiencing CSS hover bug

While developing a navbar component in React, I encountered an unexpected issue - the hover effect on my navigation links suddenly stopped working. Strangely enough, the cursor: pointer functionality still operates on my logo but not on the nav links durin ...

Guide to opening a link in a new tab within the same webpage

I have observed numerous web applications that feature a single web window with multiple links. When clicking on the links, the corresponding form opens in a new tab within the same web page of the application. I'm curious about how to achieve this fu ...

Tips for customizing the color of the select drop down arrow component:

Is there a way to change the color of the select drop down box arrow part? It needs to be compatible with IE9, Firefox, and other browsers. Currently, the drop down box looks like this: I would like the arrow part to have this appearance: I believe this ...

Creating a bordered triangle using only CSS

Looking to create a message holder with a triangular tip bordered shape? I've successfully crafted the tip using two triangles: #triangle-border { width: 0px; height: 0px; border-style: solid; border-width: 0 100px 80px 100px; bor ...

Verify whether the chosen options from a dropdown list coincide with a predetermined value

I am working with a form that generates dropdown lists dynamically. <c:forEach var="companyList" items="${company.detailList}" varStatus="status"> <tr> <td>c:out value="${companyList.employeeName}" /></td> <td> <select ...

Passing data from Angular 2 to a modal component

Within my app component, I have implemented a table that triggers a modal to appear when a user clicks on any row. The modal displays details related to that specific row. The functionality is achieved through the following HTML code within the component c ...

Is it possible to ensure that a newly created element functions in the same way as an existing element?

I am currently in the process of developing an application that empowers users to generate new items, modify existing items, or delete items by utilizing corresponding icons located adjacent to each item. When it comes to existing items, the action icon d ...