Page Stats
Visitor: 337
Bootstrap 4 Custom Form
For even more customization and cross browser consistency, use custom form elements to replace the browser defaults styling.
Checkboxes
Each checkbox input and label pairing is wrapped in a div to create our custom control.
Example 1: Custom Checkboxes
<div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="customCheck1"> <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label> </div>
Custom checkboxes can also utilize the :indeterminate pseudo class when manually set via JavaScript
Example 2: Checkboxes with indeterminate state
<script> $(document).ready(function(){ $('.c2').prop('indeterminate', true); }); </script> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input c2" id="customCheck2"> <label class="custom-control-label" for="customCheck2">Check this custom checkbox</label> </div>
Radio Button
<div class="custom-control custom-radio"> <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input"> <label class="custom-control-label" for="customRadio1">Toggle this custom radio</label> </div> <div class="custom-control custom-radio"> <input type="radio" id="customRadio2" name="customRadio" class="custom-control-input"> <label class="custom-control-label" for="customRadio2">Or toggle this other custom radio</label> </div>
Inline Radio Button
<div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="customRadioInline1" name="customRadioInline1" class="custom-control-input"> <label class="custom-control-label" for="customRadioInline1">Toggle this custom radio</label> </div> <div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="customRadioInline2" name="customRadioInline1" class="custom-control-input"> <label class="custom-control-label" for="customRadioInline2">Or toggle this other custom radio</label> </div>
Switches
A switch has the markup of a custom checkbox but uses the .custom-switch class to render a toggle switch.
<div class="custom-control custom-switch"> <input type="checkbox" class="custom-control-input" id="customSwitch1"> <label class="custom-control-label" for="customSwitch1">Toggle this switch element</label> </div> <div class="custom-control custom-switch"> <input type="checkbox" class="custom-control-input" disabled id="customSwitch2"> <label class="custom-control-label" for="customSwitch2">Disabled switch element</label> </div>
Select menu
Custom select menus need only a custom class, .custom-select to trigger the custom styles. Custom styles are limited to the select initial appearance and cannot modify the option due to browser limitations.
<select class="custom-select"> <option selected>Open this select menu</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select>
Range
Create custom <input type="range"> controls with .custom-range. The track (the background) and thumb (the value) are both styled to appear the same across browsers.
<label for="customRange1">Example range</label> <input type="range" class="custom-range" id="customRange1">
Range inputs have implicit values for min-0 and max-100, respectively. You may specify new values for those using the min and max attributes.
<label for="customRange2">Example range</label> <input type="range" class="custom-range" min="0" max="5" id="customRange2">
File browser
<div class="custom-file"> <input type="file" class="custom-file-input" id="customFile"> <label class="custom-file-label" for="customFile">Choose file</label> </div>
Add the following code if you want the name of the file appear on select
$(".custom-file-input").on("change", function() { var fileName = $(this).val().split("\\").pop(); $(this).siblings(".custom-file-label").addClass("selected").html(fileName); });