Can we guarantee that all buttons in a button group are identical?

How can I make sure that two radio buttons in a button group have the same width?

<div class="d-flex btn-group" role="group" aria-label="Basic radio toggle button group">
    <input type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off" checked>
    <label class="btn btn-outline-primary" for="btnradio1">Text</label>
    <input type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off">
    <label class="btn btn-outline-primary" for="btnradio2">More Text</label>
</div>

Is it possible to make this happen?

Currently, the button with more text is wider than the other one.

Thank you

Answer №1

To optimize your setup, I suggest enclosing each input and label pair in a container div. Utilize the existing d-flex class on your btn-group by leveraging flex properties.

In your CSS, target the btn-container divs and assign them a flex-basis: 100px (adjust pixel value as needed) for consistent width. Alternatively, use percentage values like flex-basis: 25% for even distribution with multiple buttons.

.d-flex {
  display: flex;
}

.btn-container {
  flex-basis: 100px;
}
<link href="https://cdn.bootstrap.css" rel="stylesheet"/>
<div class="d-flex btn-group" role="group" aria-label="Basic radio toggle button group">
    <div class="btn-container">
      <input type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off" checked/>
      <label class="btn btn-outline-primary" for="btnradio1">Text</label>
    </div>
    <div class="btn-container">
      <input type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off"/>
      <label class="btn btn-outline-primary" for="btnradio2">More Text</label>
    </div>
</div>

If you're hiding the radio input and displaying only the labels, consider this approach:

.d-flex {
  display: flex
}

input {
  position: absolute;
  width: 0.01px;
}

label {
  border: 1px solid black;
  padding: 1rem 2rem;
  flex-basis: 50%;
}

input:checked + label {
  background-color: blue;
}
<div class="d-flex btn-group" role="group" aria-label="Basic radio toggle button group">
        <input type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off">
        <label class="btn btn-outline-primary" for="btnradio1">Text</label>
        <input type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off">
        <label class="btn btn-outline-primary" for="btnradio2">More Text</label>
</div>

If you're using bootstrap, skip the extra styling on the btn group and focus on adding flex basis to the label.

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

Altering the appearance of a component that is currently selected and in use

Currently, I have incorporated a component with its selector within another component as shown below: <div class="col-xl-4" style="margin-bottom: 30px;"> <app-patient-info-accordion *ngIf="patient" [cardTitle]=&qu ...

What is the process for implementing transitions using SASS?

When the pointer hovers over it, the transition effect kicks in smoothly. However, it reverts back to its original size abruptly when the mouse pointer is no longer hovering over it. What I aim for is for the cardLink to scale smoothly ...

Creating header menus with section labels in Windows 8 Metro can be easily accomplished by utilizing Javascript

Is there a way to create a navigation menu in Windows 8 Metro JavaScript that includes header menus and section labels, similar to the example shown below? ...

Ways to create a see-through or non-blinking input cursor

Is there a way to make an input caret unwinking or transparent? I am aware of one method to achieve transparency: color: transparent; text-shadow: 0px 0px 0px #000; However, I am curious if there is another approach to accomplish this. ...

Press the "show additional content" button on the nytimes.com website using selenium

I'm experiencing difficulty scrolling through this webpage. Once I reach the bottom of the page, I am unable to locate and click on the "SHOW MORE" button using selenium. self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") ...

Modify the length of an array using a number input field

Currently, I am working with an array that contains objects and I want to dynamically change the number of objects in this array based on user input from a number type input box. Whenever the number in the input box is increased, I need to increase the len ...

Flask caches JSON files automatically

I am currently working on a visualization app using js and python. The functionality of my app is as follows: There is a textbox in the browser where I input an URL The URL is then sent to Python via Flask In Python, the URL is processed to create ...

Tips on causing a div to overflow instead of wrapping onto a new line

Are you looking to design a slider with 3 image containers all in the same row, where the third one overflows instead of wrapping to a new line? Here is an example: Desired Design: https://i.stack.imgur.com/dKyEg.png Current State: https://i.stack.imgu ...

Ways to solve the issue of the mobile dropdown menu in Bootstrap

Check Out the Expected Look Here (Image link) See How it Appears on Mobile Devices The dropdown menu causes an increase in the size of the navbar on the mobile version. Below is the code snippet: <div style=" background-image: url(https://i.imgu ...

When clicking on HTML input fields, they do not receive focus

I am facing a puzzling issue where I am unable to access the input fields and textareas on my HTML form. The JS, HTML, and CSS files are too large for me to share here. Could someone provide guidance on what steps to take when troubleshooting this unusual ...

Tips for reducing the JavaScript file size outputted by a liquid template generator

Currently, I am working on enhancing the performance of my Shopify website and GoogleSpeed Insights is suggesting that I minify css and js files. However, the recommended files are all created by the liquid template generator, making it challenging to uti ...

How do I eliminate excess space following the resizing of the Material UI Drawer component?

I adjusted the size of my MUI drawer to a specific width and now I am facing an issue where there is extra space left after resizing. This results in a margin-like space between the drawer and the Lorem Ipsum text content. My goal is to eliminate this rema ...

Save mathematical equations written in HTML text boxes to a MySQL database

How can I display mathematical formulas in a text input and store them in a database? I need to be able to display formulas like the ones shown in the image at this link: Currently, when I copy any formula into the text box, it gets converted to normal t ...

What is the best way to attach every sibling element to its adjacent sibling using jQuery?

I've been working with divs in Wordpress, where each div contains a ul element. <div class="list-item"> <div class="elimore_trim"> Lorem ipsum </div> <ul class="hyrra-forrad-size-list"> <li>Rent 1< ...

How to eliminate column padding in Bootstrap 4

Can you help me with a layout challenge I'm facing? I have two columns set up like this: <div class="row"> <div class="col-6"> hello </div> <div class="col-6"> hello ...

Struggling to incorporate a logo into the navigation bar for an HTML project

I'm a beginner in the world of HTML and CSS, trying my hand at creating a homepage. While I managed to add a logo to the Navigation bar, it seems to have messed up the link for the HOME page. Could this be due to not wrapping the logo in a div or imp ...

Is it possible for style sheets to malfunction due to the presence of the TITLE attribute on <link> tags?

We are currently involved in the process of upgrading an outdated corporate intranet. The challenge is that our users primarily rely on IE8 and IE9, while most of our sites were designed to function with compatibility for browsers between IE5 - IE9. While ...

Modify the variable to encompass a multitude of hues - scss

Hello, I am looking to modify the background of a page for dark mode. This is the current background styling: $orangeLight:#FFA500!default; $redLight:#FA8072!default; $blueLight:#B0C4DE!default; $greenLight:#90EE90!default; $list2: $blueLight 0%,$blueLi ...

Floating elements can be vertically aligned with spans

I am facing an issue with aligning three spans vertically inside a div. It seems simple to achieve, but vertical alignment is not working properly when I use float. My goal is to have the light blue bar vertically centered. Here is the code snippet: .co ...

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 ...