Creating a responsive button group with Bootstrap 4

Hello there,

I'm encountering a minor issue with bootstrap button groups. It seems like I may be missing something or not doing it correctly. I've hit a roadblock and could really use some guidance on this matter.

Inside a bootstrap group, I have multiple buttons that are aligned perfectly and look great, except for mobile devices.

Upon resizing the window, I noticed that the buttons remain horizontally aligned instead of forming a column layout as bootstrap usually does.

Ideally, I would like the buttons to rearrange themselves in a row, making them more mobile-friendly and creating a toolbar effect.

<div class="container">
  <div class="row">
    <div class="col-md-12">
            <div class="btn-group mr-2" role="group" aria-label="button group">
            <button type="button" class="btn btn-default">button one</button>
        <button type="button" class="btn btn-default">button two</button>
        <button type="button" class="btn btn-default">button three</button>
        <button type="button" class="btn btn-default">button four</button>
        <button type="button" class="btn btn-default">button five</button>
        <button type="button" class="btn btn-default">button six</button>
      </div>
    </div>
  </div>
</div>

Looking at the code above, you can see how it appears :

https://i.sstatic.net/HRYVP.png

However, upon resizing the screen, it looks like this :

https://i.sstatic.net/g19y8.png

My goal is to have the buttons resize or reorganize themselves to function effectively as a toolbar on mobile phones.

Answer №1

Ensuring the buttons are properly sized within your container grid is essential.

https://getbootstrap.com/docs/4.3/layout/grid/

<div class="container">
  <div class="col-md-12">
    <div class="btn-group row" role="group" aria-label="button group">
      <button type="button" class="btn btn-default col-md-2">button one</button>
      <button type="button" class="btn btn-default col-md-2">button two</button>
      <button type="button" class="btn btn-default col-md-2">button three</button>
      <button type="button" class="btn btn-default col-md-2">button four</button>
      <button type="button" class="btn btn-default col-md-2">button five</button>
      <button type="button" class="btn btn-default col-md-2">button six</button>
    </div>
  </div>
</div>

Answer №2

The default behavior of the button-group class is what you currently see on the screen. To customize when the scroll starts appearing, you can set a width breakpoint using this CSS:

@media screen and (max-width:576px){
.btn-group{display:flex;    flex-direction: column;}
}

Check out the complete snippet below:

@media screen and (max-width:576px) {
  .btn-group {
    display: flex;
    flex-direction: column;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<div class="container">
  <div class="row">
    <div class="col-md-12">
      <div class="btn-group mr-2 " role="group" aria-label="button group">
        <button type="button" class="btn btn-default">button one</button>
        <button type="button" class="btn btn-default">button two</button>
        <button type="button" class="btn btn-default">button three</button>
        <button type="button" class="btn btn-default">button four</button>
        <button type="button" class="btn btn-default">button five</button>
        <button type="button" class="btn btn-default">button six</button>
      </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

The wrapper is failing to extend the full height of the entire page

Initially, I thought setting the height of my wrapping div to 100% would be a quick task that I could complete in five minutes. However, it ended up taking me hours, so now I'm here hoping someone can assist me with this commonly asked question. The ...

insufficient coverage of wrapper around nested divs

http://jsfiddle.net/bobbyfrancisjoseph/2EfLz/1/ I recently created this code, but I am facing an issue where the wrapper div does not appear to contain the nested divs on the final page. ...

Changes in browser size will not affect the height

Is there a way to smoothly change the height of the navigation bar when resizing the browser? Currently, it snaps to the new height when the width goes below or above 1000px. Any suggestions? nav.cust-navbar { -webkit-transition: height 2s; tran ...

Determining the button's width relative to its parent's size using a

I'm trying to make my button take up 90% of the width of the screen. After testing my HTML code, I noticed that the button is only occupying around 10% of the screen. However, when I specify the width using pixels, it adjusts correctly. What could b ...

Styling Lists: How to Create a Second Line Indent with Counters

There seems to be an issue with the second line indent when using a custom counter. When attempting to use 'list-style-type: decimal', the numbering does not display as desired, for example: ol { counter-reset: ci-counter; list-style- ...

The RGB values of a dynamically loaded image being drawn to a canvas in JavaScript are slightly off

After hosting some .PNG images on my NGINX webserver, I noticed a discrepancy in the RGB values when loading and drawing them on a canvas using context.drawImage(img, 0, 0); and retrieving the image data using context.getImageData(x, y, 1, 1).data. Interes ...

Utilizing jQuery for a click event to create a random quote generator using a JSON API

Seeking guidance on my first attempt at using the JSON API for a challenge from freeCodeCamp. The task is to create a random quote machine. Upon clicking the "New Quote" button, the goal is to display a new random quote retrieved via jQuery looping throug ...

Vue is currently operating in development mode

I keep receiving this notification each time I visit the site: Vue is currently running in development mode. Remember to switch to production mode when deploying for production. Find more helpful tips at .... I came across a solution from someone suggest ...

Ways to incorporate physics into CSS animations

Creating a loading screen with CSS and aiming for realistic behavior can be a challenge. Utilizing the animation-timing-function: cubic-bezier(1, 0, 1, 1) property gives a decent result, but perfecting it requires understanding how the parameters in cubic- ...

Resize the div based on the width and height of the window

My goal is to create a system or website that can be fully resizable using CSS and the VW (viewport width) unit. Within this system, I have integrated a GoogleChart that functions with pixels. Now, I am looking for a way to scale the chart using Javascript ...

The button's height exceeds the height of the nested content

Currently, I am utilizing bootstrap 4 alpha version. <button class="btn btn-link p-0"> <div style="display:inline-block; background-color:#c2f5ff; width: 100px; height: 100px;"> </div> </button> Check out the Fiddle here In ...

Locating username and password elements using Python and Selenium

Attached are 2 screenshots displaying the html code of a website. I am curious about the "find driver element" formats needed to input my username and password into the proper boxes. View Image1 View Image2 Your assistance is greatly appreciated. ...

Issue: The error message "undefined variable 'angular'" appears when attempting to access offline files stored on a network drive

I have successfully developed offline forms using angular js v1.6.4, angular-ui-bootstrap, and angular-ui-router without the need for server hosting. When the package is saved on local storage, it functions perfectly on both IE and Chrome browsers. Howeve ...

Immersive full-screen dynamic backdrop

I am looking to create a dynamic full-size background with a smooth fade effect. Currently, I have implemented the following code: jsfiddle html: <img src="http://elegantthemes.com/preview/InStyle/wp-content/uploads/2008/11/s-1.jpg" /> <img src= ...

I am facing issues with the CSS inline display property and unable to get it to

I am having trouble aligning the section element in a row using the display: inline-block; property. Here is what I have tried: https://i.sstatic.net/JB1Y2.jpg Despite my efforts, I can't seem to get it to work as expected. Below are snippets of bot ...

Guide to positioning an image absolutely within a div

I am struggling to position a small close image within a div without success. The goal is for the image to always appear on the left side of the div, regardless of the content. The content consists of a text label with a maximum length of 8 characters. Iss ...

Is there a way for me to bring up the menu with a captivating animation, seamlessly sliding downwards into position?

I'm struggling to figure out how to create a sliding animation for the menu. I want it to slide down and back up when toggled. Any solution or advice would be greatly appreciated! const toggleButton = document.getElementById('toggleButton&apos ...

Presenting XML data on a webpage using AJAX technology

I am currently working on setting up a web radio station with a program that allows streaming (jazler) to send xml files via ftp to a web server to automatically update information such as the current song playing and previous songs. The code for NowOnAir ...

Javascript auto submission fails to execute following the completion of the printer script

As someone new to javascript, I have a question. I have a function called sendToQuickPrinter() that prints correctly, but after it finishes executing, I need to automatically submit a form to return to my "cart.php" page. I feel like I'm almost there, ...

Adjust the dimensions of all images using JavaScript

Is there a way to resize images using javascript? I attempted the following: var images = document.getElementByTagName('img') images.max_width = '100px' images.max_height = '100px' Unfortunately, it did not have the desired e ...