Position the custom checkbox icon to the right of the label in Bootstrap 4

No matter what I try, I just can't seem to get the checkbox positioned correctly next to the label. Moving the checkbox to the right of the label is easy when it's a standard checkbox, but for some reason, I can't achieve the same result with a custom-checkbox type.

<div class="custom-control custom-checkbox mb-3">
  <input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
  <label class="custom-control-label" for="customCheck">Label</label>
</div>

Answer №1

To create a custom checkbox, pseudo elements are used which require adjustments in their positioning relative to the label.

.custom-control.custom-checkbox{padding-left: 0;}

label.custom-control-label {
  position: relative;
  padding-right: 1.5rem;
}

label.custom-control-label::before, label.custom-control-label::after{
  right: 0;
  left: auto;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="custom-control custom-checkbox mb-3">
  <input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
  <label class="custom-control-label" for="customCheck">Label</label>
</div>

Answer №2

After trying the @serg-chernata solution and finding it didn't work with the custom-switch element, I came up with my own working example.

CSS

div.custom-control-right {
    padding-right: 24px;
    padding-left: 0;
    margin-left: 0;
    margin-right: 0;
}
div.custom-control-right .custom-control-label::after{
    right: -1.5rem;
    left: auto;
}
div.custom-control-right .custom-control-label::before {
    right: -2.35rem;
    left: auto;
}

HTML

<div class="custom-control custom-control-right custom-switch">
    <input type="checkbox" class="custom-control-input" disabled id="customSwitch2">
    <label class="custom-control-label" for="customSwitch2">Right switch element</label>
</div>

JsFiddle

https://jsfiddle.net/2cmbq9r0/

Screenshot

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

Edit: modified left:initial to left:auto for compatibility with IE11.

Answer №3

Accepting the following changes...

  • Moving the label and checkbox further apart
  • Opting for the basic form-check class over custom-control

an option is using a col-right class to position the checkbox to the right side of your form. In certain scenarios, aligning labels on the left and input elements on the right can create a visually appealing and consistent layout.

<div class="row form-row">
  <div class="col">
    <label class="form-check-label" for="customCheck">Label</label>
  </div>
  <div class="col-right">
    <input type="checkbox" class="form-check-input" id="customCheck" name="example1">
  </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

Issue with Bootstrap card "sub-components" such as card-heading not functioning

I'm facing an issue while trying to integrate BlazorStrap into an existing .NET6 Blazor web assembly app. It seems like there is a problem with Bootstrap that needs to be resolved before I can get BlazorStrap to function properly. Let's focus on ...

Ensure the dark mode switch CSS remains consistent when navigating to different pages

I have been working on a code to toggle between dark and light mode on a webpage. While it works fine within the current page, the issue arises when I navigate to another page or refresh the current one - the mode resets back to default (light). How can I ...

What is the best way to incorporate keyboard shortcuts into carousel operations using jQuery?

The example can be found here. I want to be able to navigate through the images using the left and right arrows on my keyboard. How can I achieve this using jQuery or CSS? This is the structure of the HTML: <div id="slider-code"> <a cla ...

Html flipcard malfunctioning and logo overlapping issue detected

Regarding the following code: <html> <style> .img { max-width: 100%; } .Headerstyle { color:Black; transition: transform .2s; text-align: center; margin-top: 80vh; } .Headerstyle:hover { transform: scale(1.5) ...

Scheduled Events and revising the date navigation in the calendar

https://developer.mozilla.org/fy-NL/demos/detail/html5-calendar/launch Among the codes provided, which specific code enables the use of the browser's back/forward buttons to change the day? I'm having trouble figuring it out. Additionally, do y ...

Enhanced border appears when hovering over the element

Take a moment to visit the following website: Navigate to the Business Lines section and hover over Property Development & Project Management. Notice how the menu changes to double line when hovering. Is there a way to prevent this? I prefer to keep it ...

Change the CSS hover menu to function as a click menu instead of a hover

I am working on a CSS/HTML menu that includes the following CSS code: #nav { background-color:#F36F25; margin:0 0 5px 0; width: 100%; height:35px; left:0; z-index:1; border-top:2px solid #FFFFFF; border-bottom:2px solid #FF ...

Tips on aligning a span inside a div to the right and ensuring the text is responsive

I want to enhance my bar designing skills. I managed to create one, but I'm facing some issues. How can I make this span align to the right? I tried using float but it didn't work. Any suggestions? Also, when I try to shrink the screen, the text ...

Avoid using sticky positioning if the element exceeds the height of the screen

I have a sidebar that is set to be sticky, but in some cases it is taller than the height of the screen. If the sidebar ends up being larger than the screen height, I don't want it to stay at the top. Instead, it should scroll along with the content ...

Use JavaScript to limit Google Analytics and Yandex.Metrica to track only the referral sources and screen sizes

I prefer not to include external JavaScript on my website for unnecessary tracking purposes. However, I do need to gather referrer and screen size information, which cannot be achieved with a regular HTML img tag alone. What is the most standard-complian ...

Checking the status of a checkbox in Ionic 2

Is there a way to check if an ion-checkbox is marked as checked in Ionic 2? <ion-item *ngFor="let a of q.Answers"> <ion-label>{{a.AnswerDescription}}</ion-label> <ion-checkbox (click)="addValue($event)"></ion-chec ...

Looking for advice on using media queries to resize my background image - any tips?

I'm struggling to resize my background image using media queries. Can anyone offer suggestions or solutions? I've tried applying the media query for my background image like I do with other elements, and it's not working properly. The issue ...

Tips for creating animated card designs with HTML, CSS, and JavaScript

As I delve into the realm of CSS animation, my primary focus is on replicating the captivating card shuffling animation showcased at Here's a glimpse of the progress I've made so far: https://youtu.be/GDIJ2K22cnY While I've successfully im ...

What are the steps to create two adaptable blocks and one immovable block?

Check out this HTML code snippet: <div style="border: 1px solid #000; width: 700px; line-height: 38px; display: block"> <div style="margin-left:10px;width: 222px; float: left; margin-right: 10px;"> Enter your question </div& ...

"Using jQuery to dynamically add and remove classes upon a click event on form labels

I am trying to implement a simple addClass and removeClass function that should execute on click. Here is the code snippet: $('#filter').click(function() { $(this).closest('fieldset').find('.selectedfilter').removeClass(&apos ...

The jQuery click event will only trigger once the mouse has been moved, but the CSS hover effect will work properly even when the hover state is not active

I seem to be facing a similar issue like the one mentioned here: jQuery click event only working after moving the mouse in Chrome, but specifically on Mac OS X (tested on Chrome, Firefox, and Safari). The functionality works fine on Windows (skipping IE fo ...

The div width set to 100% is not functioning properly upon the initial form loading in IE11, however, it is working

[![enter image description here][1]][1]Can anyone assist me in resolving this issue? When my Div with the id "Coverage" loads initially, its width is set to the content width. However, after a refresh, it expands to 100% width. This problem does not occu ...

Problem with Bootstrap-slider not loading correctly

I seem to be facing an issue with selecting DOM elements that are part of bootstrap-slider. When I try to target them with events like click, nothing happens. All events work fine when the page is refreshed. What could be causing this problem? $(document ...

Is it possible to adjust the height of the dropdown menu in a mat-select component in Angular 7?

How can I adjust the height of a mat-select in Angular7 to display all items properly? Here is my component file: import { Component, ViewEncapsulation } from "@angular/core"; import { FormControl } from "@angular/forms"; /** @title Select with multiple ...

What is the best way to center this image horizontally within its variable-sized parent container?

I am currently working on a webpage that has a responsive design. The page features a list of products, with the product listing adjusting in size as the page width changes. My goal is to center the product image within its container, ensuring that the i ...