Expanding the dimensions of a bootstrap checkbox

After referring to the official bootstrap documentation, I have successfully implemented a simple check box using the following code:

<div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
    <label class="custom-control-label" for="customCheck">Check this custom checkbox</label>
</div>

I am currently exploring ways to enlarge the size of the checkbox. Despite adjusting the CSS properties of customCheck such as height and width, I have not been successful in increasing the size of the checkbox. Any suggestions on how to achieve this?

Answer №1

Introducing customized classes like *-sm, *-md, *-lg, *-xl specifically for the bootstrap switch component.

This implementation offers a single @mixin to handle switch behavior across all screen resolutions, akin to JS functions without return values.


Bootstrap 4 Implementation

custom-switch-sm, custom-switch-md, custom-switch-lg, custom-switch-xl

SCSS: https://codepen.io/nisharg/pen/VwLbYvv

CSS: https://codepen.io/nisharg/pen/mdJmywW

LIVE SNIPPET (CSS)

/* styling for sm screens */

.custom-switch.custom-switch-sm .custom-control-label {
    padding-left: 1rem;
    padding-bottom: 1rem;
}

.custom-switch.custom-switch-sm .custom-control-label::before {
    height: 1rem;
    width: calc(1rem + 0.75rem);
    border-radius: 2rem;
}

.custom-switch.custom-switch-sm .custom-control-label::after {
    // custom styles for after pseudo-element
}

.custom-switch.custom-switch-sm .custom-control-input:checked ~ .custom-control-label::after {
    // custom styles for checked input
}

/* additional styles for md, lg, and xl screens included in the original code snippet */


Bootstrap 5 Implementation

form-switch-sm, form-switch-md, form-switch-lg, form-switch-xl

SCSS: https://codepen.io/nisharg/pen/gOPLOYY

CSS: https://codepen.io/nisharg/pen/ExPNxYE

LIVE SNIPPET (CSS)

.form-check-input {
  clear: left;
}

// Styles for different screen size variants of the form-switch component 

Answer №2

For Bootstrap Enthusiasts Everywhere

No Need for Extra CSS or SCSS

The solution is actually quite straightforward - you can either apply custom CSS/SCSS to enlarge the button, OR

You have the option to utilize the transform: scale(); property in CSS to increase the size of the button. Trust me, this method is the simplest way to address the issue and it works like a charm.


<div class="form-check form-switch py-5">

  <!-- The default switch size will be increased by 1.8 times -->
  <input class="form-check-input mx-2" 
         type="checkbox" 
         role="switch" 
         id="flexSwitchCheckDefault" 
         style="transform: scale(1.8);">

  <label class="form-check-label" 
         for="flexSwitchCheckDefault" 
         id="AucTu">Autocomplete Turned Off</label>
</div>

PS. There's no need to add custom CSS/SCSS, just remember to include some margin around the switch, or padding within the div (or parts might get hidden) :)

Answer №3

By harnessing the power of Bootstrap 5, a sleek solution has been devised to resize checkboxes with ease. This can be achieved by utilizing the predefined font-size classes (fs-x) that come pre-packaged with the framework. Take a look at this code snippet for reference:

<div class="form-check fs-3">
        <input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
        <label class="form-check-label" for="flexRadioDefault1">
            Default radio
        </label>
    </div>
    <div class="form-check fs-4">
        <input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
        <label class="form-check-label" for="flexRadioDefault1">
            Default radio
        </label>
    </div>
    <div class="form-check fs-5">
        <input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
        <label class="form-check-label" for="flexRadioDefault1">
            Default radio
        </label>
    </div>
    <div class="form-check fs-6 mb-3">
        <input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
        <label class="form-check-label" for="flexRadioDefault1">
            Default radio
        </label>
    </div>

This implementation will yield the following result:

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

Answer №4

If you're looking to customize or enlarge the beautiful switch from iOS in Bootstrap 4, you'll need to delve into the framework's CSS file and adjust the dimensions of the custom-switch class. https://i.sstatic.net/GsT0k.png Here is an example of HTML code showcasing the custom switch:

<div class="custom-control custom-checkbox">
 <input type="checkbox" class="custom-control-input" id="switchA" name="switch">
 <label class="custom-control-label" for="switchA">Custom checkbox</label>
</div>

In the boostrap.css (version 4.3.1 and unminified) file, locate the custom-switch class and adjust the parameters listed below. Be sure to remove all the comments included.

.custom-switch {
  padding-left: 2.25rem;
  padding-bottom: 1rem; // added for positioning
}

.custom-control-label { // added for alignment with the switch
  padding-top: 0.5rem;
  padding-left: 2rem;
  padding-bottom: 0.1rem;
}

.custom-switch .custom-control-label::before {
  left: -2.25rem;
  height: 2rem;
  width: 3.5rem;    // it was 1.75rem before. Slider length increased.
  pointer-events: all;
  border-radius: 1rem;
}

.custom-switch .custom-control-label::after {
  top: calc(0.25rem + 2px);
  left: calc(-2.25rem + 2px);
  width: calc(2rem - 4px);   // it was calc(1rem - 4px) before. Oval size enlarged.
  height: calc(2rem - 4px);  // it was calc(1rem - 4px) before. Oval size enlarged.
  background-color: #adb5bd;
  border-radius: 2rem; //   it was 0.5rem before. Oval size enlarged.
  transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
  transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
}
@media (prefers-reduced-motion: reduce) {
  .custom-switch .custom-control-label::after {
    transition: none;
  }
}

.custom-switch .custom-control-input:checked ~ .custom-control-label::after {
  background-color: #fff;
  -webkit-transform: translateX(1.5rem); //translateX(0.75rem);
  transform: translateX(1.5rem); //translateX(0.75rem);
}

Once you've integrated this code into your Bootstrap file, remember to delete all comments provided for the changes to take effect. Now enjoy your larger and more attractive switch!

Answer №5

Enhanced Bootstrap 5 Styling

I revamped the CSS initially created by Nisharg Shah to ensure that the text aligns perfectly with the switch in my customized Bootstrap V5 example. After carefully examining the Bootstrap 5 CSS files, I made adjustments to several attributes related to sizing such as: padding-left, height, margin-left, width, and padding-top. The updated styling provides a more cohesive and visually appealing appearance that blends seamlessly with the default switch design.

CSS and HTML:

.form-switch.form-switch-md {
    padding-left: 4.5em;
    height: 2.5em;
}

.form-switch.form-switch-md .form-check-input {
    margin-left: -4.5em;
    height: 2em;
    width: 4em;
}

.form-switch.form-switch-md .form-check-label {
    padding-top: 0.5em;
}

.form-switch.form-switch-lg {
    padding-left: 8.5em;
    height: 4.5em;
}

.form-switch.form-switch-lg .form-check-input {
    margin-left: -8.5em;
    height: 4em;
    width: 8em;
}

.form-switch.form-switch-lg .form-check-label {
    padding-top: 1.5em;
}
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
  <label class="form-check-label" for="flexSwitchCheckDefault">Original switch unchecked</label>
</div>

<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
  <label class="form-check-label" for="flexSwitchCheckChecked">Original switch checked</label>
</div>

<div class="form-check form-switch form-switch-md">
  <input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
  <label class="form-check-label" for="flexSwitchCheckDefault">Medium switch unchecked</label>
</div>

<div class="form-check form-switch form-switch-md">
  <input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
  <label class="form-check-label" for="flexSwitchCheckChecked">Medium switch checked</label>
</div>

<div class="form-check form-switch form-switch-lg">
  <input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
  <label class="form-check-label" for="flexSwitchCheckDefault">Large switch unchecked</label>
</div>

<div class="form-check form-switch form-switch-lg">
  <input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
  <label class="form-check-label" for="flexSwitchCheckChecked">Large switch checked</label>
</div>

Answer №6

To optimize your code customizations, it is recommended to have a separate file where you can modify the CSS variables for switch dimensions:

@import "bootstrap/scss/_functions.scss";
@import "bootstrap/scss/_variables.scss";

$custom-control-indicator-size: 1.5rem;
$custom-switch-width: $custom-control-indicator-size * 1.75;
$custom-switch-indicator-border-radius: $custom-control-indicator-size / 2;
$custom-switch-indicator-size: subtract(
  $custom-control-indicator-size,
  $custom-control-indicator-border-width * 4
);

@import "bootstrap/scss/bootstrap";

Answer №7

Altering the dimensions of bootstrap components directly is not recommended, as it may require recompiling the library after modifying variables. However, achieving your desired result can still be done by following this approach:

.custom-control-label::before ,.custom-control-label::after{width:20px; height:20px}

Answer №8

This particular .custom-switch-adaptive class enables the switch to adjust according to text size and integrates the label text within it.

Check out the Demo here

P.S. Bootstrap v5 will soon incorporate a similar feature

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

SCSS

.custom-switch.custom-switch-adaptive {
  padding-left: 0;

  .custom-control-label {
    padding: 0 1.5em;
    position: relative;
    border-radius: 1em;
    line-height: 1.4em;
    color: $text-muted;
    border: 1px solid $text-muted;
    background-color: $light;
    transition: background-color 200ms;

    &::before {
      content: none;
    }
    &::after {
      position: absolute;
      height: 1em;
      top: 0.2em;
      left: 0.2em;
      width: 1em;
      border-radius: 1em;

      transition: left 200ms;
    }
  }


  .custom-control-input:checked ~ .custom-control-label {
    color: $white;
    background-color: $success;
    border-color: $success;

    &::after {
      background-color: $white;
      left: calc(100% - 1.2em);

      transform: none;
      -webkit-transform: none;
    }
  }
}

HTML

<div class="form-group h1">
  <div class="custom-control custom-switch custom-switch-adaptive">
    <input id="test4" type="checkbox" class="custom-control-input">
    <label for="test4" class="custom-control-label">h1 text size</label>
  </div>
</div>

Answer №9

If you're working with bootstrap, completing this task is a breeze. Begin by adding a div container with the class btn-group-toggle if you want to use buttons in a group.

You can expand the label by applying either the p-3 or p-5 classes to it.

<div class="btn-group-toggle" data-toggle="buttons">
        <label class="btn btn-secondary p-3">
            <input type="checkbox" id="customCheck" name="example1"> Check this custom checkbox
        </label>
    </div>

Answer №10

One possible solution is as follows:

.custom-control-label::before, 
.custom-control-label::after {
  width: 2.25rem !important;
  height: 2.25rem !important;
  margin: 1rem;
}
.custom-control-label{
  margin: 1.5rem 2rem;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">


<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
  <label class="custom-control-label" for="customCheck">Check this custom checkbox</label>
</div>

Answer №11

Below is a practical illustration utilizing personalized css styles

.custom-checkbox-lg .custom-control-label::before,
.custom-checkbox-lg .custom-control-label::after {
  width: 2rem;
  height: 2rem;
}

.custom-checkbox-lg .custom-control-label {
  margin: .5rem 1rem;
}
<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">
  <input type="checkbox" class="custom-control-input" id="customCheck1" name="example1">
  <label class="custom-control-label" for="customCheck1">Tick this customized checkbox</label>
</div>

<div class="custom-control custom-checkbox custom-checkbox-lg">
  <input type="checkbox" class="custom-control-input" id="customCheck2" name="example1">
  <label class="custom-control-label" for="customCheck2">Tick this custom checkbox</label>
</div>

Answer №12

Here is my creative solution utilizing Bootstrap and FontAwesome:

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284a47475c5b5c5a4958681d0619061b">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cfef3f3e8efe8eefdecdca9b2adb2af">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="ps-3">
  <div class="h3"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2c0cdcdd6d1d6d0c3d2e2978c938c91">[email protected]</a> &  font-awesome/6.2.0</div>
  <div class="col-12">
    <label>Label Top</label>
    <span class="form-check form-switch">
      <input class="form-check-input" type="checkbox" role="switch">
      <label class="form-check-label">Label Side</label>
    </span>
  </div>
  <div class="col-12">
    <label>Label Top</label>
    <span class="form-check form-switch fa-2x">
      <input class="form-check-input" type="checkbox" role="switch">
      <label class="form-check-label">Label Side</label>
    </span>
  </div>
  <div class="col-12">
    <label>Label Top</label>
    <span class="form-check form-switch fa-2x">
      <input class="form-check-input" type="checkbox" role="switch">
      <label class="form-check-label">Label Side</label>
    </span>
  </div>
  <div class="col-12">
    <label>Label Top</label>
    <span class="form-check form-switch fa-3x">
      <input class="form-check-input" type="checkbox" role="switch">
      <label class="form-check-label">Label Side</label>
    </span>
  </div>
  <div class="col-12">
    <label>Label Top</label>
    <span class="form-check form-switch fa-4x">
      <input class="form-check-input" type="checkbox" role="switch">
      <label class="form-check-label">Label Side</label>
    </span>
  </div>
</div>

Answer №13

After struggling to find a solution to my issue, none of the previous answers seemed to work well for me, especially when dealing with checkboxes/switches that have labels attached.

I decided to experiment with margins and line heights, but ultimately found that the solution was much simpler than expected - With Bootstrap V5, all sizes and margins are set relatively using "em", so adjusting the font size on the base form-check element can effectively resize and align everything neatly, regardless of whether there is a label present or not.

UPDATE: I did notice a minor issue with the bottom margin when no label is included, but this can be easily fixed by either adding an empty label or manually tweaking the margins/padding as necessary.

.form-check-lg {
  font-size: 150%;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10727f7f64636462716050253e203e22">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<h3>default font size:</h3>
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox" id="checkbox_1">
  <label class="form-check-label" for="checkbox_1">default</label>
</div>
<div class="form-check"><input class="form-check-input" type="checkbox" id="checkbox_2">
  <label class="form-check-label" for="checkbox_2">default</label>
</div>
<div class="form-check form-switch">
  <input class="form-check-input" type="checkbox">
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox">
</div>

<h3>larger font size:</h3>
<div class="form-check form-switch form-check-lg">
  <input class="form-check-input" type="checkbox" id="checkbox_3">
  <label class="form-check-label" for="checkbox_3">large</label>
</div>
<div class="form-check form-check-lg">
  <input class="form-check-input" type="checkbox" id="checkbox_4">
  <label class="form-check-label" for="checkbox_4">large</label>
</div>
<div class="form-check form-switch form-check-lg">
  <input class="form-check-input" type="checkbox">
</div>
<div class="form-check form-check-lg">
  <input class="form-check-input" type="checkbox">
</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

Is there a way to get an iframe to mimic the behavior of other media elements within a horizontal scrolling container?

Take a look at this code snippet: $(document).ready(function() { $('.scrollable-area').on('wheel', function(e) { var scrollLeft = $(this).scrollLeft(); var width = $(this).get(0).scrollWidth - $(this).width(); var delta ...

Uniquely crafted JSON Ajax loop for dynamic transformations

When making an Ajax JSON call to an API, a variable n is used. The higher the value of the variable and depending on the user, more results are fetched. If the API returns nothing, the JSON value response will be "Nothing found". To handle this scenario ...

Conflicting Issues between jQuery Toggle and Mouseup Function

I have a straightforward button that, when clicked, toggles a menu. If the menu is expanded/visible, I want it to be hidden when clicking anywhere on the page (except for the menu itself). var menuBtn = $(".btn-menu"), menuContainer = $(".menu"), menuChi ...

The color of the Bootstrap input button does not display correctly

I'm looking to enhance my input button by adding a font awesome icon next to it. After some research, I came across this helpful answer and realized that option 1 is the best fit for my situation as I need to use it in a form to delete a repository. ...

Using jQuery UI datepicker - how to set a parameter based on a specific condition

New to the world of JavaScript, I am trying my hand at implementing the jQuery UI datepicker widget. My goal is to add an additional line to the widget parameters if the variable selectedDate has a value. However, my current approach seems to be ineffecti ...

Creating a JSX syntax for a simulated component and ensuring it is fully typed with TypeScript

Looking for some innovative ideas on how to approach this challenge. I have a test helper utils with added types: import { jest } from '@jest/globals' import React from 'react' // https://learn.reactnativeschool.com/courses/781007/lect ...

Attempting to incorporate a variable into the URL while making an Ajax POST request using jQuery

Currently, I am attempting to create a post using jQuery.ajax instead of an html form. Below is the code I am using: $.ajax({ type: 'POST', // GET is also an option if preferred url: '/groups/dissolve/$org_ID', data: data, success: fu ...

Pair items with identical names within an array

I am faced with a task where I need to merge filter elements with equal titles in order to avoid repetition. The goal is to display all inner elements together under one title if they have the same name. For example, if two filters both named FILTER-AB ha ...

Issue with CSS Transform Causing Rendering Errors in Both Chrome and Firefox

While a pixel here or there can be overlooked, in this case, precision is essential for the perfect rendering of this simple menu. The spacing between menu items must be impeccably equal to avoid any merging or disjointed appearance. Currently, the sep ...

What steps can I take to ensure a Websocket is only opened after connecting an onopen handler function?

I am struggling to grasp a certain aspect of the Websockets API. Typically, the onOpen event handler is utilized for initiating message sending to the server after ensuring that the socket is opened and ready. Based on various code examples I have come a ...

Utilizing Node.js for Gmail API: Extracting Inline/Embedded Images

When working with email data, one approach is to use the gmail.users.messages.get() method. After fetching the email data, there are two functions used to handle the payload. function getBody(message) { var encodedBody = ''; try{ i ...

What could be the reason that the auto complete feature in a jQuery Textbox is failing to work properly when utilized within

When implementing an auto complete jQuery in a Textbox on my web form, I encountered an issue. The jQuery works perfectly fine when used in a normal HTML page below the title tag and within the head tags, but it does not work when I try to use it within a ...

Eliminate the listener if the connected function contains a binding

Here is a code snippet to consider: class Test { constructor() { this.breakpoints = {}; } add(options) { // Register the media query this.breakpoints[options.breakpoint] = window.matchMedia(options.breakpoint); ...

Struggling with effectively executing chained and inner promises

It seems like my promises are not completing as expected due to incorrect handling. When using Promise.all(), the final result displayed with console.log(payload) is {}. Ideally, it should show something similar to this: { project1: { description: & ...

Tips on transferring information from one function to another in React.js

Can anyone help me figure out how to pass data from a radio button to a common function in a React project? I've been able to achieve this with a submit button, but struggling with the radio button. Here's what I have so far: App.js /* Extracti ...

Preventing Vimeo from continuing to play when the modal is closed by clicking outside of it

I am facing an issue with my Vimeo video embedded in a modal. The video stops playing when I click on the 'close' button, but it continues to play in the background when I click off the modal. I have tried solutions from similar questions without ...

Ways to determine if it's the current moment using JavaScript's moment

After doing some research, I discovered that in order to determine if an Object is a moment, the method moment.isMoment(obj) needs to be used. However, when attempting to confirm moment().toDate(), it returned false. Is there a way to verify if an Object ...

Stop Vue bootstrap-dropdown from automatically closing upon clicking

Is there a way to modify the functionality of a b-dropdown-item-button in a b-dropdown-component so that it doesn't automatically close when clicked? This is the structure of the dropdown: <b-dropdown> <b-dropdown-item-button> ...

initiate dynamic elements triggering in jQuery

function fetchTeacherAnnouncementDetails(){ var tCounter = 1; $.ajax({ url:'<%=contextPath%>/show/announcementsForTeacher', headers: { 'Authorization':'${sessionScope.token}' ...

`The post route in the Express app is encountering an issue where req.body is not

Here is my router config: router.post('/', async(req, res) => { const posts = await loadPostsCollection() await posts.insertOne({ text: req.body.text, createdAt: new Date() }) res.status(201).send() }) An error has occurred in the ...