Skip to content

Latest commit

 

History

History
412 lines (296 loc) · 13.2 KB

Bootstrap.md

File metadata and controls

412 lines (296 loc) · 13.2 KB

Comprehensive Bootstrap Notes

Table of Contents

  1. Introduction to Bootstrap
  2. Bootstrap Grid System
  3. Bootstrap Components
  4. JavaScript Components
  5. Bootstrap Utilities
  6. Bootstrap Forms
  7. Bootstrap Modals
  8. Bootstrap Navigation
  9. Bootstrap Alerts
  10. Bootstrap Cards
  11. Accessibility Considerations
  12. Customizing Bootstrap
  13. Performance Optimization
  14. Common Pitfalls
  15. Best Practices
  16. References

1. Introduction to Bootstrap

  • Definition: Bootstrap is a widely-used open-source front-end framework designed for developing responsive and mobile-first websites. It provides a comprehensive set of CSS and JavaScript components to streamline the design and development process.

  • History: Created by Twitter in 2011, Bootstrap has evolved into one of the most popular frameworks, thanks to its user-friendly nature and flexibility. The latest version, Bootstrap 5, introduces new features and eliminates jQuery as a dependency.

  • Purpose: Bootstrap aims to simplify web development with its pre-designed components, responsive utilities, and a robust grid system.

  • Real-World Applications: Notable websites, including Twitter and Spotify, utilize Bootstrap for its responsive design capabilities and rapid development process.


2. Bootstrap Grid System

  • Overview: Bootstrap's grid system is built on a flexbox layout, using a series of containers, rows, and columns to align and structure content responsively.

Basic Grid Structure

<div class="container">
    <div class="row">
        <div class="col">Column 1</div>
        <div class="col">Column 2</div>
        <div class="col">Column 3</div>
    </div>
</div>
  • Containers: Use .container for fixed-width layouts and .container-fluid for full-width layouts.

  • Rows: The .row class creates horizontal groups of columns.

  • Columns: Specify column sizes with .col, .col-<breakpoint>-<number>, or .col-<number>. Bootstrap supports a total of 12 columns per row.

Responsive Breakpoints

Bootstrap's responsive breakpoints allow you to control layout changes at various screen sizes:

  • xs: Extra small (less than 576px)
  • sm: Small (≥576px)
  • md: Medium (≥768px)
  • lg: Large (≥992px)
  • xl: Extra large (≥1200px)
  • xxl: Extra extra large (≥1400px)

Example of Responsive Grid

<div class="container">
    <div class="row">
        <div class="col-12 col-md-6">Column 1</div>
        <div class="col-12 col-md-6">Column 2</div>
    </div>
</div>

In this example, each column takes full width on extra small and small screens, but on medium screens and above, they will each take up half the width.


3. Bootstrap Components

Bootstrap includes a variety of pre-built components that are easy to integrate into your projects.

  • Buttons: Use the .btn class for styling buttons, with various contextual classes like .btn-primary, .btn-secondary, and .btn-success.
<button type="button" class="btn btn-primary">Primary Button</button>
  • Alerts: Create alert messages using the .alert class along with contextual classes.
<div class="alert alert-warning" role="alert">
    This is a warning alert—check it out!
</div>
  • Navbars: Build responsive navigation bars with the .navbar class.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Brand</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Features</a>
            </li>
        </ul>
    </div>
</nav>
  • Additional Components: Include examples of advanced components such as Carousels, Tooltips, and Dropdowns to showcase Bootstrap's versatility.

4. JavaScript Components

Bootstrap provides various JavaScript components that enhance interactivity.

  • Modals: Display content in a dialog overlay using the modal component.
<!-- Button to trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Launch demo modal</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                Modal body content here.
            </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>
  • Dropdowns: Implement dropdown menus for navigation or action options.
<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Dropdown button
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
    </div>
</div>

5. Bootstrap Utilities

Bootstrap provides a variety of utility classes for spacing, sizing, and alignment.

Spacing Utilities

  • Margins: Use .m-* for margin and .p-* for padding utilities, where * represents the breakpoint and size.
<div class="mt-3 mb-4">Margin Top 3 and Margin Bottom 4</div>
  • Responsive Spacing: Specify responsive sizes using breakpoints (e.g., .mb-sm-2, .mt-lg-5).

Sizing Utilities

  • Width: Utilize .w-* classes for width (e.g., .w-50 for 50% width).
<div class="w-50">This div is 50% wide</div>
  • Height: Use .h-* classes for height (e.g., .h-100 for 100% height).

6. Bootstrap Forms

Bootstrap simplifies the creation of responsive forms with various styles and layouts.

Basic Form Structure

<form>
    <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
        <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>
  • Input Types: Bootstrap supports various input types, including text, email, password, and file.

Form Validation

Bootstrap provides built-in validation styles. Use the .was-validated class to style validated forms.

<form class="needs-validation" novalidate>
    <div class="form-group">
        <input type="text" class="form-control" required>
        <div class="invalid-feedback">Please provide a valid input.</div>
    </div>
    <button class="btn btn-primary" type="submit">Submit</button>
</form>

7. Bootstrap Modals

Modals serve as dialog boxes for various purposes, such as displaying content or forms.

Basic Modal Structure

<!-- Button to trigger modal -->
<button type="button" class="btn btn-primary"

 data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Modal Title</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                This is the modal body.
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

8. Bootstrap Navigation

Build responsive navigation bars and tabs using Bootstrap’s built-in classes.

Navbar Example

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Features</a>
            </li>
        </ul>
    </div>
</nav>

Tabs Example

<ul class="nav nav-tabs" id="myTab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Home</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Profile</a>
    </li>
</ul>
<div class="tab-content" id="myTabContent">
    <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">Home Content</div>
    <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">Profile Content</div>
</div>

9. Bootstrap Alerts

Alerts are used to display messages or notifications to users.

Alert Example

<div class="alert alert-success" role="alert">
    This is a success alert—check it out!
</div>
  • Dismissible Alerts: Use the .alert-dismissible class to create dismissible alerts.
<div class="alert alert-warning alert-dismissible fade show" role="alert">
    A simple warning alert—check it out!
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
</div>

10. Bootstrap Cards

Cards are flexible content containers that can include images, text, and links.

Basic Card Structure

<div class="card" style="width: 18rem;">
    <img src="image.jpg" class="card-img-top" alt="...">
    <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>
        <a href="#" class="btn btn-primary">Go somewhere</a>
    </div>
</div>

11. Accessibility Considerations

  • ARIA Roles: Utilize ARIA attributes to enhance accessibility for assistive technologies.

  • Keyboard Navigation: Ensure all interactive elements are navigable via keyboard.

  • Color Contrast: Maintain high color contrast ratios for better visibility.


12. Customizing Bootstrap

  • Sass Variables: Customize Bootstrap using Sass variables to change colors, fonts, and spacing.

  • Themes: Create custom themes to align with your brand identity.

  • Override Styles: Use custom CSS to override default Bootstrap styles.


13. Performance Optimization

  • Minification: Minify CSS and JavaScript files to reduce load times.

  • CDN Usage: Utilize Bootstrap’s CDN for faster loading and reduced server load.

  • Unused Components: Avoid including unused components and scripts.


14. Common Pitfalls

  • Not Using Container: Forgetting to wrap grid elements in a container can lead to layout issues.

  • Overusing Inline Styles: Relying on inline styles instead of Bootstrap classes can hinder maintainability.

  • Accessibility Issues: Neglecting accessibility can lead to an exclusionary experience for users with disabilities.


15. Best Practices

  • Consistent Design: Maintain a consistent design language across your application.

  • Mobile-First Approach: Start designing for the smallest screen sizes first.

  • Regular Updates: Keep Bootstrap and dependencies updated for the latest features and security patches.


16. References


Happy coding!