Welcome to The Coding College! Bootstrap 4 provides built-in classes for creating and customizing forms, but what if you need more than the standard designs? This is where Custom Forms come into play. Bootstrap 4’s custom form controls allow you to design forms that are stylish, accessible, and consistent across devices.
In this guide, we’ll cover everything you need to know about Custom Forms in Bootstrap 4, including file inputs, checkboxes, radios, select menus, and more.
Why Use Custom Forms?
Custom form controls in Bootstrap 4 give you:
- A uniform design across all browsers and devices.
- Enhanced styling options, including colors, sizes, and more.
- Accessibility features to ensure inclusivity.
Getting Started with Custom Forms
To use custom forms, include Bootstrap 4’s CSS and JavaScript files in your project. Here’s a basic setup:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Forms</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h2>Bootstrap 4 Custom Forms</h2>
<!-- Custom form examples go here -->
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Custom Checkboxes and Radios
Example: Custom Checkboxes
<div class="container mt-5">
<h3>Custom Checkboxes</h3>
<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>
</div>
Example: Custom Radios
<div class="container mt-5">
<h3>Custom Radios</h3>
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" id="customRadio1" name="customRadio">
<label class="custom-control-label" for="customRadio1">Select this custom radio</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" id="customRadio2" name="customRadio">
<label class="custom-control-label" for="customRadio2">Or this one</label>
</div>
</div>
Custom File Inputs
Custom file inputs allow you to style file upload fields consistently across browsers.
Example: File Input
<div class="container mt-5">
<h3>Custom File Input</h3>
<div class="custom-file">
<input type="file" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
Custom Select Menus
Style dropdown select menus with the .custom-select
class.
Example: Custom Select
<div class="container mt-5">
<h3>Custom Select Menu</h3>
<select class="custom-select">
<option selected>Open this select menu</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
Custom Range Sliders
Custom range sliders are styled with the .custom-range
class.
Example: Range Slider
<div class="container mt-5">
<h3>Custom Range Slider</h3>
<label for="customRange1">Custom Range:</label>
<input type="range" class="custom-range" id="customRange1">
</div>
Custom Switches
Switches are a modern way to toggle between options.
Example: Custom Switch
<div class="container mt-5">
<h3>Custom Switch</h3>
<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</label>
</div>
</div>
Custom Inline Forms
Custom controls can also be styled inline for compact forms.
Example: Inline Form
<div class="container mt-5">
<h3>Inline Form</h3>
<form class="form-inline">
<div class="form-group mx-sm-3 mb-2">
<input type="text" class="form-control" placeholder="Enter text">
</div>
<button type="submit" class="btn btn-primary mb-2">Submit</button>
</form>
</div>
Conclusion
Bootstrap 4 custom forms give developers the flexibility to create stylish, responsive, and accessible form components. By leveraging classes like .custom-control
, .custom-file
, and .custom-select
, you can design intuitive forms tailored to your users’ needs.