Categories
Bootstrap HTML

Bootstrap 5 — Customize Modals

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to customize modals with JavaScript with Bootstrap 5.

Change Animation

We can change the animation with the $modal-fade-transform variable.

We just set it to the value we want.

To remove the animation, we remove the fade class from the modal markup:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal">
  Launch modal
</button>

<div class="modal" tabindex="-1" id='modal' data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit..</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Dynamic Heights

We can change the height dynamically with JavaScript by calling the myModal.handleUpdate() to change it.

Optional Sizes

We can change the size of the modal by applying a few classes.

.modal-sm makes it small.

.modal-lg makes it large.

.modal-xl makes it extra large.

So we can write:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal">
  Launch modal
</button>

<div class="modal modal-sm" tabindex="-1" id='modal' data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit..</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

to change the size of the modal by adding the modal-sm class in addition to the modal class.

Fullscreen Modal

We can make the modal full screen with a few classes.

.modal-fullscreen makes it always fullscreen.

.modal-fullscreen-sm-down makes it full screen if the screen is below 576px wide

.modal-fullscreen-md-down makes it full screen if the screen is below 768px wide.

.modal-fullscreem-lg-down makes it full screen if the screen is below 992px wide.

And .modal-fullscreen-xl-down makes it full screen if the screen is below 1200px wide.

We can make it always fullscreen by writing:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal">
  Launch modal
</button>

<div class="modal modal-fullscreen" tabindex="-1" id='modal' data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.   </p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

JavaScript

We can manipulate modals with JavaScript.

For instance, to show a modal with JavaScript, we can write:

const myModal = new bootstrap.Modal(document.getElementById('modal'), {
  backdrop: true
})
myModal.show();

given that we have the following modal:

<div class="modal" tabindex="-1" id='modal' data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit..</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

We create the modal object with the bootstrap.Modal constructor.

Then we call the show method on the returned object to show the modal.

There’s also the hide method to hide modals.

handleUpdate readjusts the modal position automatically.

dispose destroys the modal.

getInstance gets the modal instance.

Events

show.bs.modal is emitted when the modal is showing.

shown.bs.modal is emitted when the modal is shown.

hide.bs.modal is emitted when the modal is hiding.

hidden.bs.modal is emitted when the modal is hidden.

hidePrevented.bs.modal is emitted when the modal is shown, its backdrop is static and ca click outside the modal or with the escape key is performed or data-keyboard is set to false .

Conclusion

We can use JavaScript to manipulate modals.

Categories
Bootstrap HTML

Bootstrap 5 — More About Cards

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to add more to cards with Bootstrap 5.

Card Footers and List Groups

We can add a footer below list groups in a card.

To do that, we can write:

<div class="card" style="width: 18rem;">  
  <ul class="list-group list-group-flush">  
    <li class="list-group-item">Lorem ipsum dolor sit amet,</li>  
    <li class="list-group-item">Ut ac ante enim</li>  
    <li class="list-group-item">Phasellus ullamcorper tellus vitae magna vulputate egestas.</li>  
  </ul>  
  <div class="card-footer">  
    Featured  
  </div>  
</div>

We just added a div with the .card-footer class to add the footer.

Kitchen Sink

We can add images, titles, card text, list groups all in one card.

For instance, we can write:

<div class="card" style="width: 18rem;">  
  <img src="http://placekitten.com/200/200" class="card-img-top" alt="cat">  
  <div class="card-body">  
    <h5 class="card-title">Card title</h5>  
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>  
  </div>  
  <ul class="list-group list-group-flush">  
    <li class="list-group-item">Lorem ipsum dolor sit amet,</li>  
    <li class="list-group-item">Ut ac ante enim</li>  
    <li class="list-group-item">Phasellus ullamcorper tellus vitae magna vulputate egestas.</li>  
  </ul>  
  <div class="card-footer">  
    Featured  
  </div>  
</div>

to add them all in one card.

The div with the .card-body has the body.

.list-group has the list group.

.card-footer has the footer.

Header and Footer

We can add card headers and footers in a card.

For example, we can write:

<div class="card">  
  <div class="card-header">  
    Featured  
  </div>  
  <div class="card-body">  
    <h5 class="card-title">Special title</h5>  
    <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget ultricies enim, vitae rutrum magna. In sagittis volutpat justo, nec luctus metus auctor non. Vestibulum dictum pharetra libero in malesuada. Curabitur lacinia mauris quis molestie auctor. Suspendisse finibus posuere neque, vestibulum imperdiet arcu elementum quis. Nam eget purus ac ante egestas viverra. Fusce a bibendum neque, eu ornare mi. Curabitur convallis volutpat dolor, sed cursus odio consequat a. Proin sit amet dui nisi. Etiam eget turpis in augue pharetra consequat.  
    </p>  
    <a href="#" class="btn btn-primary">Go somewhere</a>  
  </div>  
</div>

We add a header with the .card-header class.

Card headers can be styled by adding .card-header to h* elements.

For example, we can write:

<div class="card">  
  <h5 class="card-header">Featured</h5>  
  <div class="card-body">  
    <h5 class="card-title">Special title</h5>  
    <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget ultricies enim, vitae rutrum magna. In sagittis volutpat justo, nec luctus metus auctor non. Vestibulum dictum pharetra libero in malesuada. Curabitur lacinia mauris quis molestie auctor. Suspendisse finibus posuere neque, vestibulum imperdiet arcu elementum quis. Nam eget purus ac ante egestas viverra. Fusce a bibendum neque, eu ornare mi. Curabitur convallis volutpat dolor, sed cursus odio consequat a. Proin sit amet dui nisi. Etiam eget turpis in augue pharetra consequat.  
    </p>  
    <a href="#" class="btn btn-primary">Go somewhere</a>  
  </div>  
</div>

We have an h5 element instead of a div for the header.

The header will have a gray background.

We can also add block quotes in cards:

<div class="card">  
  <div class="card-header">  
    Quote  
  </div>  
  <div class="card-body">  
    <blockquote class="blockquote mb-0">  
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget ultricies enim, vitae rutrum magna. In sagittis volutpat justo, nec luctus metus auctor non. Vestibulum dictum pharetra libero in malesuada. Curabitur lacinia mauris quis molestie auctor. Suspendisse finibus posuere neque, vestibulum imperdiet arcu elementum quis. Nam eget purus ac ante egestas viverra. Fusce a bibendum neque, eu ornare mi. Curabitur convallis volutpat dolor, sed cursus odio consequat a. Proin sit amet dui nisi. Etiam eget turpis in augue pharetra consequat.</p>  
      <footer class="blockquote-footer">Someone famous <cite title="Source Title">person</cite></footer>  
    </blockquote>  
  </div>  
</div>

The content can also be centered:

<div class="card text-center">  
  <div class="card-header">  
    Featured  
  </div>  
  <div class="card-body">  
    <h5 class="card-title">Special title</h5>  
    <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget ultricies enim, vitae rutrum magna. In sagittis volutpat justo, nec luctus metus auctor non. Vestibulum dictum pharetra libero in malesuada. Curabitur lacinia mauris quis molestie auctor. Suspendisse finibus posuere neque, vestibulum imperdiet arcu elementum quis. Nam eget purus ac ante egestas viverra. Fusce a bibendum neque, eu ornare mi. Curabitur convallis volutpat dolor, sed cursus odio consequat a. Proin sit amet dui nisi. Etiam eget turpis in augue pharetra consequat.</p>  
    <a href="#" class="btn btn-primary">Go somewhere</a>  
  </div>  
  <div class="card-footer text-muted">  
    20 days ago  
  </div>  
</div>

We use the text-center class to center everything.

Conclusion

We can add classes with list groups and other content.

They can be centered and placed in different locations.

Categories
Bootstrap HTML

Bootstrap 5 — List Groups Customization

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to customize list groups with Bootstrap 5.

Contextual Classes and List Group Actions

We can add styling classes to list group actions.

For example, we can write:

<div class="list-group">
  <a href="#" class="list-group-item list-group-item-action">no style</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-primary">primary</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-secondary">secondary</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-success">success</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-danger">danger</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-warning">warning</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-info">info</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-light">light</a>
  <a href="#" class="list-group-item list-group-item-action list-group-item-dark">dark</a>
</div>

We have the list-group-item-action in each list group item and the list-group-item-primary classes to style them.

List Group Items With Badges

We can add list group items with badges.

For instance, we can write:

<ul class="list-group">
  <li class="list-group-item d-flex justify-content-between align-items-center">
    Cras justo odio
    <span class="badge bg-primary rounded-pill">22</span>
  </li>
  <li class="list-group-item d-flex justify-content-between align-items-center">
    Lorem ipsum dolor sit amet
    <span class="badge bg-primary rounded-pill">2</span>
  </li>
  <li class="list-group-item d-flex justify-content-between align-items-center">
    consectetur adipiscing elit
    <span class="badge bg-primary rounded-pill">1</span>
  </li>
</ul>

We add the badge to the right of the text by adding a span with the badge class.

rounded-pill make them more rounded than the default style.

Custom Content

Also, we can add any content we want.

The layout can be added with Bootstrap 5’s flexbox utility classes.

For example, we can write:

<div class="list-group">
  <a href="#" class="list-group-item list-group-item-action active" aria-current="true">
    <div class="d-flex w-100 justify-content-between">
      <h5 class="mb-1">item</h5>
      <small>10 days ago</small>
    </div>
    <p class="mb-1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
    <small>Donec id elit non mi porta.</small>
  </a>

  <a href="#" class="list-group-item list-group-item-action">
    <div class="d-flex w-100 justify-content-between">
      <h5 class="mb-1">item</h5>
      <small class="text-muted">3 days ago</small>
    </div>
    <p class="mb-1">Vestibulum mauris mauris, faucibus in bibendum at, commodo nec nulla. </p>
    <small class="text-muted">Donec id elit non mi porta.</small>
  </a>

  <a href="#" class="list-group-item list-group-item-action">
    <div class="d-flex w-100 justify-content-between">
      <h5 class="mb-1">item</h5>
      <small class="text-muted">2 days ago</small>
    </div>
    <p class="mb-1">Fusce mi mi, viverra quis vehicula a, bibendum eu magna. </p>
    <small class="text-muted">Donec id elit non mi porta.</small>
  </a>
</div>

We put content in a tags and lay them out with the d-flex , w-100 , and justify-content-between classes.

d-flex makes the container display with a flexbox layout.

w-100 makes the content 100% wide.

justify-content-between put the items on the 2 ends of the container.

Checkboxes and Radios

Checkboxes and radio buttons can be added easily with the input element and classes the come with Bootstrap 5.

For example, we can write:

<ul class="list-group">
  <li class="list-group-item">
    <input class="form-check-input mr-1" type="checkbox" value="">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  </li>
  <li class="list-group-item">
    <input class="form-check-input mr-1" type="checkbox" value="">
    Proin non libero congue lectus pharetra dictum quis ut eros
  </li>
  <li class="list-group-item">
    <input class="form-check-input mr-1" type="checkbox" value="">
    Nunc ac eleifend risus.
  </li>
  <li class="list-group-item">
    <input class="form-check-input mr-1" type="checkbox" value="">
    In vitae arcu tincidunt, eleifend velit id
  </li>
  <li class="list-group-item">
    <input class="form-check-input mr-1" type="checkbox" value="">
    Vestibulum at eros
  </li>
</ul>

We added checkboxes and spaced them out with the mr-1 class to add some margins on the right.

Conclusion

We can add list groups with various styles and various layouts.

Checkboxes can also be added.

Categories
Bootstrap HTML

Bootstrap 5 — Form Validation

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to add form validation with Bootstrap 5.

Browser Defaults

We can use the browser default form validation.

For example, we can write:

<form class="row g-3">
  <div class="col-md-4">
    <label for="first-name" class="form-label">First name</label>
    <input type="text" class="form-control" id="first-name" value="james" required>
  </div>

 <div class="col-md-4">
    <label for="last-name" class="form-label">Last name</label>
    <input type="text" class="form-control" id="last-name" value="smith" required>
  </div>

 <div class="col-md-4">
    <label for="username" class="form-label">Username</label>
    <div class="input-group">
      <span class="input-group-text">@</span>
      <input type="text" class="form-control" id="username" required>
    </div>
  </div>

 <div class="col-md-6">
    <label for="city" class="form-label">City</label>
    <input type="text" class="form-control" id="city" required>
  </div>

 <div class="col-md-3">
    <label for="region" class="form-label">Region</label>
    <select class="form-select" id="region" required>
      <option selected disabled value="">Choose...</option>
      <option>...</option>
    </select>
  </div>

 <div class="col-md-3">
    <label for="postal-code" class="form-label">Postal Code</label>
    <input type="text" class="form-control" id="postal-code" required>
  </div>

 <div class="col-12">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="agree" required>
      <label class="form-check-label" for="agree">
        I agree
      </label>
    </div>
  </div>

 <div class="col-12">
    <button class="btn btn-primary" type="submit">Sign up</button>
  </div>
</form>

We add the fields with the require attribute to add checks for the form validation.

Since we don’t have the novalidate attribute on the form itself, browser validation will be used.

Validation Classes

The .is-invalid , .is-valid , .valid-feedback and .invalid-feedback classes can be applied to form fields to show different styles and messages to users to indicate validity.

For example, we can write:

<form class="row g-3">
  <div class="col-md-4">
    <label for="first-name" class="form-label">First name</label>
    <input type="text" class="form-control is-valid" id="first-name" value="james" required>
    <div class="valid-feedback">
      Looks good!
    </div>
  </div>

  <div class="col-md-4">
    <label for="last-name" class="form-label">Last name</label>
    <input type="text" class="form-control is-valid" id="last-name" value="smith" required>
    <div class="valid-feedback">
      Looks good!
    </div>
  </div>

  <div class="col-md-4">
    <label for="username" class="form-label">Username</label>
    <div class="input-group">
      <span class="input-group-text">@</span>
      <input type="text" class="form-control is-invalid" id="username" required>
      <div class="invalid-feedback">
        Username is required.
      </div>
    </div>
  </div>

  <div class="col-md-6">
    <label for="city" class="form-label">City</label>
    <input type="text" class="form-control is-invalid" id="city" required>
    <div class="invalid-feedback">
      City is requird
    </div>
  </div>

  <div class="col-md-3">
    <label for="region" class="form-label">Region</label>
    <select class="form-select is-invalid" id="region" required>
      <option selected disabled value="">Choose...</option>
      <option>...</option>
    </select>
    <div class="invalid-feedback">
      Region is required
    </div>
  </div>

  <div class="col-md-3">
    <label for="postal-code" class="form-label">Postal Code</label>
    <input type="text" class="form-control is-invalid" id="postal-code" required>
    <div class="invalid-feedback">
      Postal code is required
    </div>
  </div>

  <div class="col-12">
    <div class="form-check">
      <input class="form-check-input is-invalid" type="checkbox" value="" id="agree" required>
      <label class="form-check-label" for="agree">
        I agree
      </label>
      <div class="invalid-feedback">
        You must agree before submitting.
      </div>
    </div>
  </div>

  <div class="col-12">
    <button class="btn btn-primary" type="submit">Sign up</button>
  </div>
</form>

to add form validation styles.

We have the invalid-feedback class to display a message if the field isn’t valid.

The valid-feedback class is used to display a message for valid fields.

The is-valid class is used on inputs to indicate that they’re valid.

is-invalid is used to display fields that are invalid.

Supported Elements

Validation styles can be added to inputs, textareas with the form-control class.

Select elements with the .form-select class can also have validation styles applied.

.form-check s and .form-file can also have the styles applied.

For example, we can write:

<form class="was-validated">
  <div class="mb-3">
    <label for="textarea" class="form-label">Textarea</label>
    <textarea class="form-control is-invalid" id="textarea" placeholder="Required example textarea" required></textarea>
    <div class="invalid-feedback">
      text is required
    </div>
  </div>

<div class="form-check mb-3">
    <input type="checkbox" class="form-check-input" id="checkbox" required>
    <label class="form-check-label" for="checkbox">agree</label>
    <div class="invalid-feedback">invalid checkbox</div>
  </div>

<div class="form-check">
    <input type="radio" class="form-check-input" id="apple" name="radio-stacked" required>
    <label class="form-check-label" for="radio">apple</label>
  </div>

  <div class="form-check mb-3">
    <input type="radio" class="form-check-input" id="orange" name="radio-stacked" required>
    <label class="form-check-label" for="orange">orange</label>
    <div class="invalid-feedback">More example invalid feedback text</div>
  </div>

  <div class="mb-3">
    <select class="form-select" required>
      <option value="">Open this select menu</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <div class="invalid-feedback">invalid select</div>
  </div>

  <div class="form-file mb-3">
    <input type="file" class="form-file-input" id="file" required>
    <label class="form-file-label" for="file">
      <span class="form-file-text">Choose file...</span>
      <span class="form-file-button">Browse</span>
    </label>
    <div class="invalid-feedback">invalid file</div>
  </div>

  <div class="mb-3">
    <button class="btn btn-primary" type="submit" disabled>Submit</button>
  </div>
</form>

The was-validated is applied to the form.

Then we add the is-invalid and invalid-feedback classes to the fields and messages.

Everything will be red.

Inputs and textareas will have the exclamation mark.

Conclusion

We can add validation styles with various classes provided by Bootstrap 5.

Categories
Bootstrap HTML

Bootstrap 5 — Form Layouts

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to add layouts to forms with Bootstrap 5.

Form Layout

We can use Bootstrap 5 to add some layouts to our forms.

It provides us with margin utilities to add some structure.

For example, we can write:

<div class="mb-3">
  <label for="first-name" class="form-label">first name</label>
  <input type="text" class="form-control" id="first-name" placeholder="first name">
</div>

<div class="mb-3">
  <label for="last-name" class="form-label">last name</label>
  <input type="text" class="form-control" id="last-name" placeholder="last name">
</div>

We have the mb-3 class to ad some margins below the form.

Labels have the form-label class.

And inputs have the form-control class to apply Bootstrap styles.

Form Grid

We can put our form fields in a grid if we need forms fields side by side.

For example, we can write:

<div class="row">
  <div class="col">
    <input type="text" class="form-control" placeholder="First name">
  </div>

 <div class="col">
    <input type="text" class="form-control" placeholder="Last name">
  </div>
</div>

To have 2 input boxes side by side.

We have one div with row class and 2 divs with the col classes to add the columns.

The $enable-grid-classes SASS variables to be enabled and it’s on by default.

Gutters

We can add gutters with the g-* classes.

For example, we can write:

<div class="row g-3">
  <div class="col">
    <input type="text" class="form-control" placeholder="First name">
  </div>
  <div class="col">
    <input type="text" class="form-control" placeholder="Last name">
  </div>
</div>

We added some gutters with the g-3 class.

The gap is added to the div.

We can add more complex forms using the grid:

<form class="row g-3">
  <div class="col-md-6">
    <label for="email" class="form-label">Email</label>
    <input type="email" class="form-control" id="email">
  </div>

 <div class="col-md-6">
    <label for="password" class="form-label">Password</label>
    <input type="password" class="form-control" id="password">
  </div>

 <div class="col-12">
    <label for="address" class="form-label">Address</label>
    <input type="text" class="form-control" id="address" placeholder="Address">
  </div>

 <div class="col-12">
    <label for="address2" class="form-label">Address 2</label>
    <input type="text" class="form-control" id="address2" placeholder="Apartment, studio, or floor">
  </div>

 <div class="col-md-6">
    <label for="city" class="form-label">City</label>
    <input type="text" class="form-control" id="city">
  </div>

 <div class="col-md-4">
    <label for="inputState" class="form-label">Region</label>
    <select id="inputState" class="form-select">
      <option selected>Choose...</option>
      <option>...</option>
    </select>
  </div>

 <div class="col-md-2">
    <label for="postal" class="form-label">Postal Code</label>
    <input type="text" class="form-control" id="postal">
  </div>

 <div class="col-12">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" id="agree">
      <label class="form-check-label" for="agree">
        I agree
      </label>
    </div>
  </div>

 <div class="col-12">
    <button type="submit" class="btn btn-primary">Sign up</button>
  </div>
</form>

We add multiple fields to our form.

Some are wider than others.

Those are added with the col-* classes

Horizontal Form

We can add the .row class to add form groups with the .col-*-* classes to set the width of the labels and controls.

Also, we’ve to add the .col-form-label class to the labels so they’re vertically centered with their form controls.

For example, we can write:

<form>
  <div class="row mb-3">
    <label for="email" class="col-sm-2 col-form-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="email">
    </div>
  </div>

 <div class="row mb-3">
    <label for="password" class="col-sm-2 col-form-label">password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="password">
    </div>
  </div>

 <fieldset>
    <div class="row mb-3">
      <legend class="col-form-label col-sm-2 pt-0">Fruit</legend>

 <div class="col-sm-10">
        <div class="form-check">
          <input class="form-check-input" type="radio" name="fruit" id="apple" value="apple" checked>
          <label class="form-check-label" for="apple">
            apple
          </label>
        </div>

<div class="form-check">
          <input class="form-check-input" type="radio" name="fruit" id="banana" value="banana">
          <label class="form-check-label" for="banana">
            Second radio
          </label>
        </div>

 <div class="form-check disabled">
          <input class="form-check-input" type="radio" name="fruit" id="orange" value="orange" disabled>
          <label class="form-check-label" for="orange">
            Third disabled radio
          </label>
        </div>
      </div>
    </div>
  </fieldset>

 <div class="row mb-3">
    <div class="col-form-label col-sm-2 pt-0">I agree</div>
    <div class="col-sm-10">
      <div class="form-check">
        <input class="form-check-input" type="checkbox" id="agree">
        <label class="form-check-label" for="agree">
          I agree
        </label>
      </div>
    </div>
  </div>

 <button type="submit" class="btn btn-primary">Sign up</button>
</form>

We have a form with the labels and form controls side by side.

Also, we added the mb-3 class to add some margins to the bottom.

And pt-0 removes the top padding.

Conclusion

We can add form layouts with various utility classes provided by Bootstrap.