The custom radiobox in Bootstrap is misaligned when there is no label attached

When using Bootstrap4 custom radiobuttons, they work fine with label text. However, sometimes it may not be necessary to have text, resulting in a misaligned radiobutton. Is there a way to fix this alignment issue?

<div class="custom-control custom-radio">
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
  <label class="custom-control-label" for="customRadio1"></label>
</div>

Result:

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

Fiddle: https://jsfiddle.net/SchweizerSchoggi/51vrL8px/5/

<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<table class="table table-bordered">
<thead>
  <th>Select</th>
  <th>Name</th>
</thead>
<tr>
  <td>
<div class="custom-control custom-radio">
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
  <label class="custom-control-label" for="customRadio1"></label>
</div>
  </td>
  <td>Sample option 1</td>
</tr>
<tr>
  <td>
<div class="custom-control custom-radio">
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
  <label class="custom-control-label" for="customRadio1">with label</label>
</div>
  </td>
  <td>Sample option 2</td>
</tr>
</table>

Answer №1

By using a special character that doesn't display anything, you can achieve proper alignment without any text appearing next to the radio button.

&zwnj;

I believe the reason it disrupts the styling is because of the ::before and ::after code that is applied to the text. Without any text present, there is nothing for these tags to be added to.

<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<table class="table table-bordered">
  <thead>
    <th>Select</th>
    <th>Name</th>
  </thead>
  <tr>
    <td>
      <div class="custom-control custom-radio">
        <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
        <label class="custom-control-label" for="customRadio1">&zwnj;</label>
      </div>
    </td>
    <td>Sample option 1</td>
  </tr>
  <tr>
    <td>
      <div class="custom-control custom-radio">
        <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
        <label class="custom-control-label" for="customRadio1">with label</label>
      </div>
    </td>
    <td>Sample option 2</td>
  </tr>
</table>

Answer №2

Make sure to include the mb-3 class on the empty label element.

<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">

<table class="table table-bordered">
<thead>
  <th>Select</th>
  <th>Name</th>
</thead>
<tr>
  <td>
<div class="custom-control custom-radio">
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
  <label class="custom-control-label mb-3" for="customRadio1"></label>
</div>
  </td>
  <td>Sample option 1</td>
</tr>
<tr>
  <td>
<div class="custom-control custom-radio">
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
  <label class="custom-control-label" for="customRadio1">with label</label>
</div>
  </td>
  <td>Sample option 2</td>
</tr>
</table>

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

What is the best way to make multiple carousels work simultaneously?

For example, imagine two carousels occupying the same location but one is always hidden. There are two buttons above to toggle either carousel on: <div class="row"> <div class="col-lg-12"> <div class="ca ...

Guide to adding a Scrollable Navbar

Is the term "scroll bar" really accurate? I want to create a scrollable feature where users can easily select a city using their mouse wheel (or swipe on a smartphone) and then choose from possible cities within that country in a second window. Something s ...

Looking to discover how to optimize a website for various Android resolutions?

Currently, I am working on designing websites specifically for Android phones. However, I have encountered a major challenge due to the wide range of screen resolutions found on these devices. Some phones boast a height of 800px while others are limited to ...

Is it possible to adjust the element's position using the code "element.style.top = window.scrollY"?

Recently, I attempted to create a floating input element that would follow the scroll. After much effort, I managed to achieve it and the code is provided below. However, I encountered an issue when trying to change "setText(window.scrollY)" to ...

What prevents Popper from functioning on its own when I incorporate bootstrap.bundle.js?

Upon my understanding, Bootstrap 4 relies on Popper for its functionalities. When utilizing bootstrap.bundle.js, Bootstrap can smoothly access Popper for various operations. Nevertheless, when attempting to use Popper independently, an error message is enc ...

Provide spacing on the sides for a background position

Is there a way to evenly space my background image with 10px margins on all sides instead of just the left side? Currently, it's only working on the left side. .login-page { display: flex; flex-direction: column; background: url("../src/As ...

Div - containing images removed from slideshow

Currently, I am in the process of constructing a small website that includes a slideshow beneath my navigation bar. My aim is to utilize flexbox while making the slideshow "responsive." By this, I mean that when altering the dimensions of my browser window ...

On a desktop screen, there is a single tall div positioned next to two shorter divs. On mobile devices, the divs

Similar to the challenge posed in this thread, my query is slightly different. I am looking to rearrange three divs on mobile devices while having them adjacent on desktop screens. Please refer to the linked images for a visual representation of the desir ...

Looking to achieve a scroll effect with mix-blend-mode using JavaScript?

I am seeking a method to adjust the background color of a specific element based on the background itself. While CSS offers the mix-blend-mode property, I am looking to specify the color manually. Ideally, I would like to achieve the same effect using an ...

I'm having trouble changing the background color of a blank document in Brackets using external CSS

I have been utilizing Brackets for a project at school and attempting to align div tags side by side, but I am not seeing any changes in the visual display of the code. I started to suspect an issue with the external CSS, so I tried altering the background ...

"Develop a mobile application using PhoneGap designed specifically for use on iPad and

Looking to create an app for iPad and iPad Mini using PhoneGap. While I have some basic knowledge of HTML and CSS, I am struggling with adjusting the image proportion, width, and height of the app. I want to write code that will adapt properly to the vary ...

How can you craft a dynamic radial mask to animate layered images?

My goal is to have two circular SVG images layered on top of each other, with the top image in grayscale and the bottom image in full color. I want to be able to gradually remove the top image based on a percentage from 1-100, similar to how hands sweep a ...

Is it possible that the grid gutter column in Bootstrap 4 might be disrupted if it is defined as to32 (2x

My goal is to create a design system where all sizes and components are multiples of 4, rather than using two margins of 15. This will ensure a more consistent and proportional design throughout. ...

Tips for tailoring content based on screen size

I am looking for a way to display different content depending on whether the user is viewing my website on a large screen (PC/tablet) or a small screen (mobile). While my site is responsive and uses bootstrap, I have a lot of content that is only suitable ...

Issue with Jquery addClass method in Firefox

I am encountering an issue with the jQuery addClass() function in Firefox where the class is not being added to the current element. Interestingly, this code works perfectly in Chrome and Safari (IE has not been tested). Here is the CSS snippet : $(doc ...

What is the best way to align a span directly below the email input box in this specific design?

I need to position the span element with the ID email-status directly below the email input field, regardless of the viewport size, to alert users when the email is incorrect. MY HTML STRUCTURE: <!DOCTYPE html> <html lang="en"> <head> ...

Next.js experiencing issues with applying styles from .modules.scss files

Recently, I initiated a new Next.js 13 application, Within this project, there exists a file named main.modules.scss .heading { font-size: 4rem; color: #000; } .dark .heading { color: #fff; } My intention is to utilize this file for styling ...

When running `node compile.js`, no combined CSS file is being created

I have finally mastered the art of setting up and executing the "node compile.js" file in the claro theme folder to compile each *.less file into its corresponding *.css file. However, I noticed that the old claro.css file remains unchanged. Is this the in ...

How can I ensure a div always moves to the right by using margin-right auto?

How can I align a div to the right using margin-right auto? I attempted margin-right: auto; and margin: 0 0 0 auto; but it was not successful. ...

Looking to enlarge a few CSS classes, but unsure of the best approach

My app has some CSS code that I found, but it's too small. Can anyone help me increase the size by 150%? I am using React-Bootstrap for styling, so I'm looking for a way to enlarge the element without adjusting every attribute in the CSS classes. ...