Modify the layout of nested columns in Bootstrap 4 to switch from a vertical to a horizontal alignment for better

I am currently working on creating a responsive page layout using Bootstrap 4 and my aim is to adjust the alignment of two nested columns based on the size of the browser window.

Specifically, I am developing a pricing page that showcases a list of features below each of the three available options.

For larger screens (md, lg, xl), all three pricing plans are displayed in columns with features listed one after the other below each plan. However, for smaller screens (xs, xm), my goal is to divide the feature list into two columns and have them presented side by side.

I have been searching for solutions that cater to this particular scenario but have not yet come across anything suitable.

In order to tackle this issue, I am utilizing vanilla Bootstrap 4 within an HTML document, leveraging the built-in breakpoints. Despite my limited experience with flexbox, I have also attempted to use it alongside standard sizing classes, yet nothing seems to be effective thus far.

IMPORTANT: The aspect I am focusing on resolving pertains to the display of "feature 1," "feature 2," and so forth in the two columns. While the overarching plan columns are rendering correctly, the feature columns are not.

<!-- PRICING PLANS -->

<div class="bg-primary p-3 my-5">
<div class="container">
  <div class="row">

    <!-- Package 1 -->
    <div class="col-md h-100 bg-white p-3">
      <div class="d-flex flex-column">
        <div class="mb-3 bg-success text-center">
          <p>Package 1</p>
          <p>$29.99</p>
        </div>
        <div class="bg-success">

        <!-- THIS IS MY CURRENT (NON-WORKING) SOLUTION: -->

          <div class="col-6 col-md-12">
            <p>feature 1</p>
            <p>feature 2</p>
            <p>feature 3</p>
          </div>
          <div class="col-6 col-md-12">
            <p>feature 4</p>
            <p>feature 5</p>
            <p>feature 6</p>
          </div>
        </div>
      </div>
    </div>

    <!-- REMAINDER OF CODE FOR DISPLAY PURPOSES ONLY -->

    <!-- Package 2 -->
    <div class="col-md h-md-100 mx-md-4 my-md-0 my-4 bg-white p-3">
      <div class="d-flex flex-column">
        <div class="mb-3 bg-success text-center">
          <p>Package 2</p>
          <p>$19.99</p>
        </div>
        <div class="bg-success">
          <p>features</p>
        </div>
      </div>
    </div>

    <!-- Package 3 -->
    <div class="col-md h-md-100 bg-white p-3">
      <div class="d-flex flex-column">
        <div class="mb-3 bg-success text-center">
          <p>Package 3</p>
          <p>$9.99</p>
        </div>
        <div class="bg-success">
          <p>features</p>
        </div>
      </div>
    </div>
  </div>
</div>

`

I initially assumed that setting the columns to have a col-xs-6 attribute would align them horizontally until reaching col-md-12, however, they continue to display the same regardless of window size.

Answer №1

The reason for the issue in your code where `bg-success` is not behaving as expected is that it is not set to a flex parent (`display: flex`). By adding an additional class that sets it as a flex parent, you will achieve the desired result.

.make-flex {
  display: flex;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<!-- PRICING PLANS -->

<div class="bg-primary p-3 my-5">
  <div class="container">
    <div class="row">

      <!-- Package 1 -->
      <div class="col-md h-100 bg-white p-3">
        <div class="d-flex flex-column">
          <div class="mb-3 bg-success text-center">
            <p>Package 1</p>
            <p>$29.99</p>
          </div>
          <div class="bg-success make-flex">

            <!-- THIS IS MY CURRENT (NON-WORKING) SOLUTION: -->

            <div class="col-6 col-md-12">
              <p>feature afaa</p>
              <p>feature 2</p>
              <p>feature 3</p>
            </div>
            <div class="col-6 col-md-12">
              <p>feature 4</p>
              <p>feature 5</p>
              <p>feature 6</p>
            </div>
          </div>
        </div>
      </div>

      <!-- REMAINDER OF CODE FOR DISPLAY PURPOSES ONLY -->

      <!-- Package 2 -->
      <div class="col-md h-md-100 mx-md-4 my-md-0 my-4 bg-white p-3">
        <div class="d-flex flex-column">
          <div class="mb-3 bg-success text-center">
            <p>Package 2</p>
            <p>$19.99</p>
          </div>
          <div class="bg-success">
            <p>features</p>
          </div>
        </div>
      </div>

      <!-- Package 3 -->
      <div class="col-md h-md-100 bg-white p-3">
        <div class="d-flex flex-column">
          <div class="mb-3 bg-success text-center">
            <p>Package 3</p>
            <p>$9.99</p>
          </div>
          <div class="bg-success">
            <p>features</p>
          </div>
        </div>
      </div>
    </div>
  </div>

Answer №2

Following a suggestion by @disinfor, I delved into it and finally cracked the solution.

The key to my solution lies in adding two properties to the parent div: d-flex and flex-md-column.

While just using d-flex seemed odd initially, introducing flex-md-column seemed to be the missing piece that made the rendering output perfect.

<div class="col-md h-100 bg-white p-3">
          <div class="d-flex flex-column">
            <div class="mb-3 bg-success text-center">
              <p>Startup Package</p>
              <p>Best value for startups</p>
              <p>$19.99</p>
            </div>
            <div class="d-flex flex-md-column bg-success">
              <div class="col-6 col-md-12">
                <p>feature 1</p>
                <p>feature 2</p>
                <p>feature 3</p>
              </div>
              <div class="col-xs-6 col-md-12">
                <p>feature 4</p>
                <p>feature 5</p>
                <p>feature 6</p>
              </div>
            </div>
          </div>
        </div>

Answer №3

It's quite simple - the issue lies in the incorrect nesting order of your columns.

The correct nesting order should be: containerrowcolrowcol → and so on.

The solution is easy - just include row within

<div class="row bg-success">

<div class="row bg-success">

    <!-- THIS IS MY CURRENT (NON-WORKING) SOLUTION: -->

    <div class="col-6 col-md-12">

For more information, check out the official documentation

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

Using Handlebars, you can easily interpolate variables within an "each" loop

I have integrated this code into an express app that utilizes handlebars as its template engine: res.render("movies", { data: pages, arraypages:arrayPages, movie:movie}) The "arrayPages" variable is represented by an array: [ 1, 2, 3, ...

Initiating CSS3 animations when the display changes

In an attempt to incorporate CSS3 animations for fading elements in during page load, I am faced with the challenge of setting the element to display: none; and then back to display: block;. My goal is to exclusively apply the CSS3 animation upon the init ...

What are some ways to reduce the delay of Dark mode on a website?

Can anyone help me optimize the speed at which dark mode is applied on my website? Currently, there seems to be a delay between page load and the dark mode taking effect. Below is the jQuery.js code I am using: onload = function () { if (localStorage. ...

Adding an HTML select box to a Laravel Eloquent form: A step-by-step guide

I currently have a form structured like this: {{ Form::open(array('url' => 'addjob')) }} <div class="form-group"> <label for="">SubCategories</label> <select class="form-control input-sm ...

Adding a <div> within a <form> on button click using HTML and jQuery tactics

Whenever the '+' button [the addMore() function] is clicked, I want to insert the following div structure. Although I am able to add the div structure, the alignment of the input text is not equal as compared to the hard coded version. Here is t ...

Navigation in PHP webpages and determining the active page

In my template.php file, I am including navigation.php from a separate page. My goal is to have the current page highlighted with a specific CSS style as I navigate through different pages on my website. I want the active link to be dynamically styled base ...

Customizing a ControlsFX PopOver in a JavaFX application

I am looking to personalize the appearance of a JavaFX PopOver control. I have a button that triggers the display of the PopOver. Below is a functional example: package custompopover; import javafx.application.Application; import javafx.event.ActionEvent ...

Utilize Bootstrap's collapsible navbar feature to smoothly navigate to a designated section of the webpage

After creating a navigation bar using bootstrap 4, I encountered an issue when trying to collapse the navbar and jump to an anchor simultaneously in mobile view. If I only include href="#services", it successfully jumps to the correct section of ...

Clicking on text within a DIV using jQuery

How can I use jQuery to show an "alert" when text inside a DIV is clicked, excluding images and other tags? The texts are contained within various DIVs along with images and tags. Is there a way to achieve this using jQuery? Check out the jsFiddle example ...

What is the best way to create a flexible grid that automatically adjusts to either two columns or one column depending on the width of the

My challenge lies in arranging items into two columns or one, with a specific layout for mobile devices. The order of the items changes between the two column and single column layouts. The number and size of items vary dynamically. For example, in a tw ...

The Bootstrap-Select dropdown refuses to open

I've been attempting to create a menu using data-subtext, and while I have come across some suggestions on stackoverflow, I'm still having trouble getting the menu to function properly. The issue I'm facing is that the menu doesn't seem ...

Display the product image as radio buttons or checkboxes, with a tick mark appearing when selected

I am trying to add a background image to the <img> tag. My goal is to display the checkmark image in the center of the Twitter image. However, the checkmark image as a background image is not appearing here. img{ background-image:url(https://www ...

Unable to apply inset box-shadow to image

I've added a box-shadow to my image. box-shadow: 0 0 10px RED inset However, the shadow is not showing up on the image. When I use Firebug to change the image path, I can see that the shadow is behind the image. How can I apply the shadow directly ...

Arrange the two buttons in a vertical position

Excuse any weak code you may come across, as I am a backend developer. This is the current appearance of my form: The alignment of these two buttons in Chrome is not correct The issue only occurs in Chrome, with FireFox and Edge displaying the form as e ...

Mobile Chrome allows users to utilize the IFrame player API for enhanced video

Currently, I am working on creating a mobile website where I plan to include some YouTube videos using the IFrame player API (https://developers.google.com/youtube/iframe_api_reference). My main goal is to have the video start playing only after the user ...

Can anyone recommend a user-friendly JavaScript dialog box compatible with jQuery that is mobile-responsive?

Mobile browsers. Using Jquery dialog alert box. Avoiding the use of alert() due to its unattractive appearance in web browsers. In the process of creating a website for both mobile and web platforms, seeking a dialog box that is compatible with both. ...

Cancel an AJAX request without interfering with the subsequent request

When working with a number input and the "onChange" event, I have come across an issue with my ajax request. Upon aborting the request, it seems to have a negative impact on subsequent requests. If I send just one request without aborting, everything runs ...

Utilize a script on a div that is dynamically loaded via AJAX

Can anyone assist me with loading external content into a div? I attempted to use a code for this purpose, but now my scripts are not functioning correctly. Any help would be greatly appreciated - I'm new to this type of work. Thank you function aja ...

Implementing sorting functionality in a list with Vue.js by incorporating an arrow icon in the column using v-bind

Is there a way to implement sorting functionality in Vue.js by clicking on table headers and displaying an arrow to indicate the sorting order? I am struggling with creating a function in Vue for sorting and using v-bind to add the arrow. Can someone guide ...

Modifying the titles of posts with a tampermonkey script to change their background color in a vBulletin forum

I'm currently working on developing a Tampermonkey script to alter the theme of a website: which seems to be built on vBulletin. This is the script I have been experimenting with: // ==UserScript== // @name vBulletin Forum Dark Mode // @name ...