BS Forms

HTML
CSS
C#
SQL

Forms

Bootstrap, known for its responsive design approach, offers a comprehensive set of form controls that are both stylish and easy to use. This module will explore the range of form components available in Bootstrap, detailing how to create and customize forms for a variety of applications.

 Objectives

– Understand and utilize Bootstrap’s form components.
– Learn to create responsive forms that adapt to various devices.
– Explore advanced form layouts and customizations for unique designs.

Here’s a table listing some of the commonly used Bootstrap classes specifically for forms, along with their descriptions:

Bootstrap Class

Description

.form-group

Used for wrapping labels and controls in a form. Provides proper spacing and alignment.

.form-control

Applied to <input>, <textarea>, and <select> elements for standard Bootstrap styling. Makes the form control take full width of its parent.

.form-row

A row container for form elements, allowing for a tighter grouping of form elements than a standard .row.

.col-md-*

Used within a .form-row to create responsive grid layouts for form elements. The ‘*’ can be a number from 1 to 12, determining the column’s width.

.form-control-plaintext

Used to style the output of <input> as plain text.

.form-check, .form-check-input, .form-check-label

Used for styling checkboxes and radio buttons in a more standardized and readable format.

.form-inline

Used to display a series of labels, form controls, and buttons on a single horizontal row. Useful for creating compact forms.

.form-text

A class used for adding small help text under a form control. It’s displayed in a smaller font size and aligns with the input.

.is-invalid, .is-valid

Classes used for displaying validation feedback. They are used in conjunction with form controls to indicate validation states.

.invalid-feedback, .valid-feedback

Used to display validation feedback messages for form controls. They show custom messages based on validation states.

.form-check-inline

Used with .form-check to display checkboxes or radio buttons inline rather than stacked.

These classes are part of Bootstrap’s form component and are designed to create forms that are both visually appealing and user-friendly. They take advantage of Bootstrap’s responsive design features, ensuring that forms look good on devices of all sizes.

Bootstrap Form Basics

Bootstrap forms are designed to provide a uniform, attractive look and feel across different browsers and devices. Key elements include:

– Form Controls: Standard text inputs, selects, checkboxes, and radios.

– Input Groups: Enhance form inputs with text or buttons before, after, or on both sides of form controls.

– Form Layout: Options for inline and horizontal forms.

– Validation States: Provide visual feedback to users about the validity of their input.

Detailed Explanation of Bootstrap Form Elements
Form Controls

Form controls in Bootstrap are the basic building blocks of forms. They include standard text inputs, selects, checkboxes, and radios. Each of these elements is designed to ensure consistency across various browsers and devices.

Text Inputs: Used for receiving text input from the user. They can be of different types like text, password, email, etc. Bootstrap’s `.form-control` class is applied to these inputs for consistent styling.

<input type=”text” class=”form-control” placeholder=”Text input”>

 Selects: Dropdown menus for selecting from a list of options. The `.form-control` class is also applied to `<select>` elements.

  Example:

Checkboxes and Radios: For options that users can toggle on or off. Bootstrap styles these elements in a more user-friendly way, making them more visually appealing and easier to interact with.

Input Groups

Input groups in Bootstrap are used to extend the standard form controls by adding text or buttons either before, after, or on both sides of the form controls. This is useful for providing additional context or specific actions.

– Basic Structure: An input group is created using the `.input-group` class. Inside this, you use `.input-group-prepend` or `.input-group-append` to add elements to the beginning or end of the form control.

3. Form Layout

Bootstrap provides different form layout options, like inline and horizontal forms, to align form elements in various ways according to the need.

– Inline Forms: Used for aligning form controls and labels in a single horizontal row. This is done by adding the `.form-inline` class to the `<form>` tag.

– Horizontal Forms: For aligning form labels and controls side-by-side, Bootstrap uses a grid system. Labels and controls are placed inside columns.

4. Validation States

Bootstrap provides visual feedback on the validation state of form controls. This can be useful for showing users whether the input they’ve provided is valid or needs to be corrected.

– Classes for Validation: `.is-valid` and `.is-invalid` are used to indicate the validity of form controls. Additionally, `.valid-feedback` or `.invalid-feedback` classes are used to display validation messages.

Example:

Each of these components and classes enhances the functionality and user experience of forms in a Bootstrap-based website, ensuring that forms are not only functional but also aesthetically pleasing and user-friendly.

Basic Form Example:
Responsive Form Layouts

Bootstrap’s grid system can be utilized to create responsive form layouts that adapt to the screen size.

– Grid System: Use grid classes to control the layout of form elements.

– Flex Utilities: Flex classes can align and distribute form controls as needed.

Responsive Form Layout Example:
Form Validation

Bootstrap provides built-in validation styles for error, warning, and success states in forms.

– Client-side Validation: Use HTML5 and CSS to provide immediate feedback on the validity of form inputs.
– Server-side Validation: Style forms based on feedback from server-side processing.

Real Example: Bootstrap Registration Form

<!DOCTYPE html>

<html lang=”en”>

<head>

 <meta charset=”UTF-8″>

 <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

 <title>Bootstrap Registration Form</title>

 <link href=”https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css” rel=”stylesheet”>

</head>

<body>