Tips for preventing a bootstrap 5 button group from automatically centering after being clicked

I am facing an issue with a Bootstrap 5 button group inside a modal. The height is set to

style="overflow-y: auto; max-height: 290px; min-height: 290px;"

Whenever a button inside the modal div is clicked, the scroll centers to the middle button. I want to prevent this behavior and keep the scroll position where the button was clicked.

<div id="SelectLMSPlayer" class="modal fade" role="dialog">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <div class="row">
                    <label class="mx-auto d-block" style="font-weight: bold; font-size: medium;">Select this week's player</label>
                    <label class="mt-1"><label class="box lightgray me-1 align-bottom"></label>- Player has been used</label>
                </div>
                <br />

                <div class="form-group mb-4" style="overflow-y: auto; max-height: 290px; min-height: 290px;">
                    <label for="ddlPlayers">WGR - Player</label>
                 
                        <div id="playergroup" class="btn-group-vertical gap-1 btn-group d-flex m-1" role="group">
                                <input type="radio" class="btn-check" name="available" data-wgr="1" id="46046" alt="0" autocomplete="off" value="Scottie Scheffler"/>
                                <label class="btn btn-sm btn-outline-secondary rounded" id="46046" for="46046"><column>1</column> - <column>Scottie Scheffler</column></label>
                                <input type="radio" class="btn-check" name="available" data-wgr="2" id="28237" alt="0" autocomplete="off" value="Rory McIlroy"/>
                                <label class="btn btn-sm btn-outline-secondary rounded" id="28237" for="28237"><column>2</column> - <column>Rory McIlroy</column></label>
                                <input type="radio" class="btn-check" name="available" data-wgr="3" id="46970" alt="0" autocomplete="off" value="Jon Rahm"/>
                                <label class="btn btn-sm btn-outline-secondary rounded" id="46970" for="46970"><column>3</column> - <column>Jon Rahm</column></label>
                                ... More buttons here ...
                    </div>
                   
                </div>



                <div id="divSelectPlayer">
                    <div class="d-grid gap-2 mt-auto">
                        <button id="btnSelectPlayer" value="" name="" class="btn btn-success">Make Selection</button>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

This is the initial appearance before clicking on any button.

Before button group click

On clicking any button, the scroll position centers to the middle button.

After button group click

The scroll behavior centers the group of buttons vertically, and I'm looking to disable this centering action. I have tried different approaches but haven't found a solution yet.

Notably, changing the buttons to be displayed horizontally did not have the same scrolling behavior. I'm developing this on .NET Core 6 and Razor pages.

EDIT: I confirmed the issue by testing the button group on the W3Schools Bootstrap Button Group tester and encountered the same problem.

Answer №1

The issue seems to stem from the absolute positioning of the radio buttons. Because they are not tied to the labels, they all remain fixed at the top. This causes the container to scroll upwards when the label is clicked, as the focus is still on the radio buttons, even though they are visually "hidden".

Consider using an alternative method to hide them that doesn't rely on absolute positioning. It's important to ensure that this change doesn't render the radio buttons inaccessible for users who rely on screen readers. More information on this topic can be found at .

The following code, slightly adjusted based on the suggested margins, appears to function correctly:

.btn-check {
  clip: rect(1px, 1px, 1px, 1px);
  clip-path: inset(50%);
  height: 1px;
  width: 1px;
  margin: -3px 0 -2px 0;
  overflow: hidden;
  padding: 0;
  position: relative;
}

For a demonstration, visit https://jsfiddle.net/o3nsjywx/

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 CSS Clip Path to conceal elements

My current design uses clip-path for a gradient effect at the top of the page. Ideally, I want the two angles to meet seamlessly without any visual issues. I initially positioned the top so that they touch perfectly and nothing interferes with the angled ...

Avoid unnecessary re-renders in ReactJS Material UI tabs when pressing the "Enter

I've created a user interface with tabs using material-ui in reactJS. The issue I'm facing is that every time a tab is selected, the content under that tab reloads, causing performance problems because there's an iFrame displayed in one of t ...

What other function can I combine with the foreach function?

I am working on populating the empty array named $Errors with specific items to enforce restrictions on my file upload form. My approach involves adding a <div> element as an item within the array, containing certain strings. I aim to concatenate t ...

What is the best way to store the JSON data that is returned in a JavaScript function as a variable

My goal is to save the client's IP address in a variable after fetching it in JSON format from api.ipify.org. I have managed to display the IP if I alert the result, but I am struggling to store it in a variable. This code snippet works: <script& ...

Responsive on Mobile Devices

I'm seeking assistance in brainstorming a creative idea or method to convert the carousel indicators into a menu, or any alternative approach to ensure the entire section is mobile responsive while keeping all the indicators visible within the mobile ...

Click to copy: Utilizing Italics in React Components

I've successfully implemented a way to copy text to the clipboard using React. Now, I'm facing the challenge of making only the content of this.state.parties italicized, while keeping the content of this.state.citation non-italicized when pasting ...

The JSON array retrieved from the $http.GET request is coming back as undefined, which is not expected

Presenting my code below: function gatherDataForGraph(unit, startTs, endTs, startState, endState){ points = []; console.log('/file/tsDataPoints/'+unit+"?startDate="+startTs+"&endDate="+endTs+"&startState="+startState+"&endState="+en ...

Is there a way to use JavaScript to convert HTML code into plain text?

I am looking to convert an input from an API in the following format: <p>Communication that doesn&#8217;t take a chance doesn&#8217;t stand a chance.</p> into something like this "Communication that doesn’t take a chance do ...

After creating a new Packery using AngularJS, the getElementById function may return null

Alright, so I've got this HTML markup: <button ng-click="create()">create list</button> Every time I click it, I create a new list using the Packery jQuery plugin. app.directive('packery', ['$compile', function($com ...

Is there a way to configure the text box to allow for range values input?

Is there a way to create an input box that only accepts values between 20 and 40? I want to restrict single numbers within that range. How can I define the range for this specific input box? If anyone can help me achieve this, it would be greatly apprecia ...

Having trouble getting a dropdown menu to function properly with jQuery

Within my project, I have implemented a menu that transforms into a dropdown menu for smaller screens. Below is the HTML code for this functionality: <select> <option value="index.php" id="home">Home</option> <option value="about ...

Interactive tooltip feature in Apexchart

I need to include % in my Apexcharts tooltip after the Y value. Working with vue.js and without official documentation from apexchart, I managed to make it function correctly. This is what I have accomplished so far: data: function () { return { ...

Issues with angular-strap popover's onBeforeShow function not functioning as expected in Angular

I am currently utilizing the angular-strap popover feature. To learn more about it, visit According to the documentation, when an onBeforeShow function is provided, it should be called before the popover is displayed. However, I am experiencing issues wit ...

Enable Component to Exceed to the Following Line

I'm having trouble getting a map of a component to move to the next line (as indicated by the yellow arrow). Currently, instead of moving below, it is squeezing the Component (the Cards). I've added background colors for clarity. Any advice would ...

Why is the div still displaying with the same id and class even after postback in a partial view using MVC?

Encountering a issue where the partial view is repeating after post back, despite the functionality working correctly. Here is the code snippet from the View: @using (Ajax.BeginForm("MarkDefiniteDischargePatient", "PDDD",new { }, new Aja ...

Completing a third-party htaccess file

What's the best way to create a nodejs script that periodically monitors a 3rd-party website, but encounters issues with a username and password prompt generated by .htpasswd/.htacces authentication? How can nodejs handle this situation? ...

Ways to ensure text fits nicely within a container: HTML & CSS

Below is a code snippet: @import url('https://fonts.googleapis.com/css?family=Merriweather|Open+Sans'); .square { max-width: 460px; background: white; border-radius: 4px; box-shadow: 0px 5px 20px #D9DBDF; -webkit-transition: all 0. ...

The GitHub Pages website is not displaying exactly like it does when running locally with a live server

Currently in the process of creating a compact website where users can input an anagram and receive all potential words that can be formed from it. Additionally, upon receiving these words, there is an option to click on one for its definitions. The site ...

PhoneGap and jQuery prove ineffective in fetching json results from Twitter

I've been working on a project to fetch the most recent 50 tweets with a specific hash tag. The project is built for mobile devices using PhoneGap (0.9.6) and jQuery (1.6.1). Below is my code: function retrieveTweets(hash, numOfResults) { var uri ...

Tips for concealing a component in the DOM using ng-if in angular.js and angularmaterial:

Currently, I'm utilizing angular.js 1.5 along with angular material on the frontend to populate the DOM with objects retrieved from the Database. Within one of the repeaters, I am facing an issue where I need to determine if an object is already prese ...