Bootstrap 5 - Achieve automatic column width within a row

In my Bootstrap 5 setup, I have a variety of pages that have different column layouts - sometimes 2 columns, sometimes 3.

The template system I'm using dynamically adjusts the layout to accommodate the need for a third column when necessary.

<div class="container pt-5">
<div class="row gx-5">
<div class="col-sm-12 order-2 order-md-1 col-md-3">
<!-- Content for the left column goes here -->     
</div>
<div class="col-sm order-1 col-md-6">
<!-- Content for the center column goes here -->     
</div>
<!-- An if statement to determine layout goes here -->
<div class="col-sm-12 order-3 col-md-3">
<!-- Content for the right column goes here -->     
</div>
<!-- End of if statement -->
</div>
</div>

This setup works well for a three column layout, but in cases where the third column is intentionally excluded (resulting in a two column layout), the center column needs to span col-md-9 instead of col-md-6.

While I can manually adjust the template to accommodate this, I prefer to rely on Bootstrap for handling the layout seamlessly.

Initially, I tried using col-md-auto for the center column to dynamically resize, but this caused it to break onto a new line.

I am looking for a way to ensure that col-md-auto spans 9 as needed, or if there's something I might be missing in the implementation.

Your insights and assistance on this matter would be greatly appreciated.

If there are any elements that I may have overlooked, please do point them out.

Based on some feedback I received, I realize there may have been some confusion. With the current Bootstrap classes, at the small breakpoint, the three columns stack with the center on top, followed by the right column, and then the left column at the bottom. For larger breakpoints ('sm' and above), the layout changes to have the left column on the left, center column in the center, and the right column on the right. The only issue arises when there is no left column (which is a valid scenario for the site); in this case, the center column for sizes above 'sm' should expand to occupy the remaining space (either col-9 when there is no right column, or col-6 when there is).

Answer №1

To achieve auto width in this column, simply update

<div class="col-sm order-1 col-md-6">
to
<div class="col order-1">
.

Here is the entire code snippet:

<div class="container pt-5">
  <div class="row gx-5 bg-primary">
    <div class="col-sm-12 order-2 order-md-1 col-md-3 bg-warning">
      <!-- Content for the left column goes here -->
    </div>
    <div class="col order-1 bg-info">
      <!-- Content for the center column goes here -->
    </div>
    <!-- Insert your own if statement here -->
    <div class="col-sm-12 order-3 col-md-3 bg-body">
      <!-- Content for the right column goes here -->
    </div>
    <!-- End of if statement -->
  </div>
</div>

Answer №2

After struggling to find a solution using only bootstrap, I had to implement an if statement in the templating engine. My final code involved the templating engine generating different templates depending on whether there were two or three columns needed. I realized my initial assumption was incorrect - instead of relying solely on bootstrap for handling 2 or 3 columns, the decision should be made by the templating engine to create either a 2 column layout using bootstrap or a three column layout. I appreciate everyone's time and input, and I believe this approach is the best way to resolve the issue.

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

The height of the absolute div tag does not match the height of the parent div

I am currently designing an editor that requires a line-number bar (gutter) on the left side. I have set a div with a height of 100% and its parent div with overflow: auto. My expectation is that if the content exceeds the given height, the editor-body sh ...

Which terms are acceptable for meta tag titles?

During my studies on w3school, I came across a page that mentioned only the following values are valid: application-name author description generator keywords http://www.w3schools.com/tags/tag_meta.asp However, upon further exploration of webs ...

CSS: Firefox shows the menu with adequate right margin

Just delving into CSS and have successfully crafted a dropdown menu. It's all smooth sailing in Chrome and IE, but Firefox presents two challenges: 1) The color gradient appears darker in Firefox compared to the lighter green in Chrome and IE. 2) An ...

Converting a JavaScript IIFE library into a React Component

Hello, I am currently in the process of learning React JS and there are two JavaScript files that I am working on: Polyfill.js -> hosted on github CustomNavbar.js -> created by me Here is the structure of polyfill.js: export default (function(win ...

Inject the value of a JavaScript array into an HTML element

Is it feasible to transfer a global javascript array value to an HTML tag, particularly within an img (title) tag? I'm curious if the following is achievable: <img src="_info.gif" height="26" width="37" title=myArray[5]/> If it is possible, p ...

Enhance your HTML5 video by incorporating strategically placed buttons (images) as an overlay

Recently, I embedded a video: <video preload muted autoplay loop id="sty"> <source src="content/1/sty.mp4" type="video/mp4"> </video> To make this video full-width, I made sure to set the CSS properti ...

Optimizing Open Graph tags for sharing WhatsApp links

I am utilizing Open Graph tags to optimize sharing on social media platforms. The image I have specified is 1200 x 630 pixels and displays correctly on Facebook as well as Whatsapp: <meta property="og:image" content="https://emotionathlet ...

Optimizing row performance for Angular grids in the Chrome browser

When creating a component that includes a table with numerous rows, everything works well with small amounts of data. However, once the item count reaches 2000 or more, it starts lagging. Scrolling and animations become sluggish. Even after trying to impl ...

What is the best way to assign a Python variable to an <a-assets> element?

There exists a Python function that provides the chosen background <a-sky ID="sky" color="{{object.background}}"></a-sky> The backgrounds can be in the format of '#c6aa99', '#e1a600', '#290942', 'star' ...

Guide to creating a parallax scrolling effect with a background image

My frustration levels have hit an all-time high after spending days attempting to troubleshoot why parallax scrolling isn't functioning for a single image on a website I'm developing. To give you some context, here's the code I've been ...

How can I customize the color of the disabled state in a mat-slide-toggle?

I am trying to customize the disabled state color of a mat-slide-toggle. This is what my slide toggle currently looks like: https://i.sstatic.net/Lhz0U.png Here is the code I have been using: <div> <mat-slide-toggle>Slide me!</mat-slide ...

Stretch a component outside of the bootstrap container

Currently, I am working on a mockup using Bootstrap 4 and have encountered an unusual element inside the container where the background extends beyond the container. I'm not sure how to address this issue effectively. I attempted to use position: abso ...

Guide to integrating react-phone-number-input into material-ui TextField

Would it be possible for me to use a Material UI TextField component as the inputComponent prop for the PhoneInput component from react-phone-number-input? I am facing an issue where I am unable to apply the ref successfully. Even though I can see the Mat ...

Designing a webpage - patterns in the background and images layered on top

I've been tasked with creating a webpage that resembles a theatrical stage. The design calls for the following layout: [curtain repeat][left curtain][stage][right curtain][curtain repeat] The left curtain, stage, and right curtain should maintain a ...

Creating a diagonal division border using CSS alongside a background image

I've been attempting to place 2 divs next to each other with a diagonal space between them. While I've come across several tutorials and discussions on Stack Overflow about creating diagonal divs, they often involve using borders with solid color ...

Getting rid of background color in a tooltip using Material UI

I'm currently tackling an issue with Material UI's tooltip. I can't seem to find a way to make the background of the tooltip completely transparent. By default, it displays with a grey background and white text. Changing the background color ...

Activate the preview mode by transmitting information between two controllers

I'm trying to set up a preview mode in one controller to show an image in another controller using an angular service. However, I'm struggling with the final step of getting the URL from the passed parameter into the ng-src in SideMenuCtrl dynami ...

Animations in Angular fail to operate within mat-tabs after switching back when custom components are being utilized

I am facing a similar issue as Olafvv with my angular 16 project. The problem occurs in a mat-tab-group where the :enter animations do not work when navigating away from a tab and then returning to it. However, in my case, the issue lies within a custom su ...

Positioning three squares with the same class adjacent to each other by utilizing absolute positioning

I've been pondering whether it's possible to align elements with the same class and position: absolute next to each other. After an hour of experimenting, I discovered a solution: http://jsfiddle.net/hv01ad1r/1/ However, when attempting to use d ...

The route seems to be downloading the page instead of properly rendering it for display

I'm facing a simple issue with my Express page - when I navigate to the FAQ route, instead of displaying the page it downloads it. The index page loads fine, and the FAQ page is the only other page available at the moment. I am using EJS templating, a ...