Utilizing Radio buttons to Align Labels in Bootstrap 4

I'm experimenting with a Bootstrap 4 form and trying to get inline radio buttons with labels. It was working fine with Bootstrap 3, but I'm facing issues with Bootstrap 4 and its custom-radio styling.

Desired Output:
Gender : ( ) Male ( ) Female

Can someone guide me in the right direction? Here's my approach:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <form class="form-horizontal" method="post">
    <div class="row">
      <div class="form-group row col-lg-6">
        <label for="firstName" class="col-3 col-form-label">First Name</label>
        <div class="col-9">
          <input type="text" class="form-control" />
        </div>
        <div class="form-check row col-lg-6">
          <label class="col-3 col-form-label">Gender</label>
          <div class="col-9">
            <label class="custom-control custom-radio">
              <input name="cat" type="radio" class="custom-control-input" value="1">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Male</span>
            </label>
            <label class="custom-control custom-radio">
              <input id="mixed0" name="cat" type="radio" class="custom-control-input" value="0">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Female</span>
            </label>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>

JS Fiddle: https://jsfiddle.net/w7odmrtr/2/

Answer №1

If I were to tackle this task, I would approach it in the following manner:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
    <div class="container">
  <form class="form-horizontal" method="post">
        <div class="row">
          <div class="form-group col-lg-12">
            <label for="firstName" class="col-lg-3 col-form-label">First Name</label>
            <div class="col-lg-9">
              <input type="text" class="form-control">
            </div>
          </div>  
          <div class="form-group col-lg-12">
            <label class="custom-control custom-radio"><span class="custom-control-description">Some Label</span></label>
            <label class="custom-control custom-radio">
              <input name="cat" type="radio" class="custom-control-input" value="1">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description" th:text="#{bool.true}">Yes</span>
            </label>
            <label class="custom-control custom-radio">
              <input id="mixed0" name="cat" type="radio" class="custom-control-input" value="0">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">No</span>
            </label>
          </div>
        </div>
  </form>
</div>

Answer №2

Initially, the div tag was not properly closed. I made a simple adjustment by changing the class from form-check to form-group, and now it appears to be functioning correctly.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <form class="form-horizontal" method="post">
    <div class="row">
      <div class="form-group row col-lg-6">
        <label for="firstName" class="col-3 col-form-label">First Name</label>
        <div class="col-9">
          <input type="text" class="form-control" />
        </div>
      </div>
      <div class="form-group row col-lg-6">
          <label class="col-3 col-form-label">Gender</label>
          <div class="col-9">
            <label class="custom-control custom-radio">
              <input name="cat" type="radio" class="custom-control-input" value="1">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Male</span>
            </label>
            <label class="custom-control custom-radio">
              <input id="mixed0" name="cat" type="radio" class="custom-control-input" value="0">
              <span class="custom-control-indicator"></span>
              <span class="custom-control-description">Female</span>
            </label>
          </div>
        </div>
      </div>
    </div>
  </form>
</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

Ways to invoke a JavaScript function within a child window that is already open

I am working on a project where I have a main html file. In this file, I want the user to click on something that will trigger a new window or tab to open. This new window/tab should display the dynamically generated content of a hidden div in the main htm ...

ReactJs CSS - This file type requires a specific loader for processing. There are currently no loaders configured to handle this file

I've noticed that this issue has been raised multiple times before. Despite going through all the questions, I still can't seem to resolve it. The transition from Typescript to Javascript went smoothly until I reached the implementation of CSS. U ...

Using jQuery to make an AJAX request to a Java backend server in order to update and refresh an HTML list

As someone new to using jQuery / AJAX with a Java backend system, I'm looking for guidance on making my first call. I have an HTML list structured like this: <ul> <li>Test 1</li> <li>Test 2</li> <li>Te ...

Is it true that AngularJS is unable to update data attributes?

I'm working with an input element that utilizes the type-ahead plugin from bootstrap. My goal is to dynamically set the data-source attribute as the user types, creating a real-time search effect. <input id="test" data-provide="typeahead" data-sou ...

Guide to Helping Users Customize JavaScript and CSS Files before Exporting as Embed Code

I have a vision to create a unique platform where users have the ability to log in to a customized dashboard, tailor their preferences, and then generate an embeddable code for age verification pop-ups on their websites. To kickstart this project, I'v ...

What is the best way to manage classNames dynamically in React with Material-UI?

I am wondering how to dynamically add and remove classes from an img tag. My goal is to change the image automatically every 2 seconds, similar to Instagram's signup page. I am struggling to achieve this using the material-ui approach. Below is a snip ...

Guide for displaying information as a grid or table format using Angular 4

I am tasked with handling data from an external microservice that is not always in a standard format. The data is dynamic and can include table data, along with metadata information above the grid. My challenge is to render or identify the table data, disp ...

Easily Update Your Div Content by Simply Clicking a Single Link/Button

I am in need of assistance here. My goal is to display dynamic content within a div. Below you will find the code I currently have: <script type="text/javascript"><!-- function AlterContentInContainer(id, content) { var container = documen ...

Tips for preventing multiple counter buttons from conflicting with one another

Currently, I am in the process of creating an online restaurant platform that allows customers to place food orders. To streamline this process, I am developing individual cards for each food item available on the menu. In addition, I am implementing butto ...

Steps for selecting a radio button based on a MySQL table value:

My table has two fields, name and gender. When I retrieve the data and attempt to display it in input fields, everything works fine for the Name field. However, I am curious if there is a way to automatically select the radio button based on the gender d ...

How can I attach an event listener to an element that is added dynamically with jQuery?

I added a new div element dynamically using jQuery's on method. However, I'm having trouble getting a listener to work for the newly created element. HTML <input class="123" type="button" value="button" /> <input class="123" type="butt ...

Tips for utilizing the Delete function in Python Flask

Need help with the Delete method in Python Flask. I'm encountering some difficulties in my code and it keeps displaying Method Not Allowed error when trying to load the URL http://localhost:8082/api/users/remove_group/5. @app.route('/api/users/re ...

Looking to retrieve the dynamic "Publish Date" value from a webpage using HtmlUnit in Java?

As part of a coding exercise, I am currently working on a project that involves comparing the current system date with dates on various web pages to determine if there are any new updates. While most of the pages work as expected, there is one particular p ...

Creating a weather format user interface in HTML can be accomplished by following a few

I am currently utilizing the open weather API to showcase weather data, however, I am facing difficulties in arranging the data in a format similar to the one shown below. Example output: I attempted to present the data in the following structure: ...

Enable scrolling feature for the table

I have integrated a bootstrap table into my React application, but I am facing an issue where the table is not scrollable. Whenever new content is loaded, it increases the height of the table instead of making it scrollable. Below is the code for my table ...

Issue with passing object to HTML file causing it to not display on webpage

Trying to display the username of the logged-in user on my webpage, but only getting a generic Welcome message. No errors are showing up in either the console or web browser console. Should I use the Principal class instead or look for the username elsewh ...

What is the best way to make a selected link stand out with a highlight?

I'm having an issue with the code below that is supposed to keep the selected link highlighted, but it only flashes the green color on click. Can someone help me figure out what's going wrong here? #sidebarContent a:active{ background-colo ...

Displaying text on an image when hovering over it

I have tried various solutions that I came across, but none of them have been successful so far. My goal is to display dynamic text over an image when the user hovers over it. Additionally, I would like to change the hover color. Currently, the text is alw ...

Is there a way to send an array with data to a different component using react-redux within a Higher Order Component (H

It seems like I'm missing something because I can't seem to find any examples of how to pass an array with data from a Higher Order Component (HOC) to another component. Here is the code snippet: import React from 'react' import NoAc ...

Deactivate a dropdown option in Angular's uib-dropdown

Currently in my angular template, I am working on a dropdown menu using angular-ui. The requirement is to disable certain list items based on a property of the "company" object defined in the ng-repeat. I've already experimented with the disabled tag ...