Bootstrap 5 Examples with Code Explanation
Introduction:
Bootstrap 5, the latest version of the popular front-end framework, has revolutionized web development with its powerful features and intuitive design. With a wide range of components and utilities, Bootstrap 5 enables developers to create responsive, visually appealing websites efficiently. In this article, we will explore the top 6 Bootstrap 5 examples that demonstrate the framework’s versatility and showcase its potential. Whether you’re a beginner or an experienced developer, these examples will inspire you to leverage Bootstrap 5’s capabilities in your own projects.
Bootstrap 5 Navbar Examples:
Bootstrap 5 Example: How to create Basic Navbar with a Brand logo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Navbar Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container"> <a class="navbar-brand" href="#">Logo</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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"> <a class="nav-link" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Services</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Contact</a> </li> </ul> </div> </div> </nav> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code Explanation:
1 |
<nav class="navbar navbar-expand-lg navbar-light bg-light"> |
This line creates the main container for the navbar. The navbar class specifies that it’s a navbar, while navbar-expand-lg makes it expand to the full width on large screens. navbar-light sets a light color scheme, and bg-light applies a light background color.
1 |
<div class="container"> |
This div sets the maximum width of the navbar content. It acts as a container for the navbar items, providing a consistent layout.
1 |
<a class="navbar-brand" href="#">Logo</a> |
This line defines the brand or logo of the website, which is displayed on the navbar. It is a link (<a>) with the navbar-brand class. The “#” in the href attribute represents a placeholder link and should be replaced with the actual URL of your logo or home page.
1 2 3 4 |
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> |
This button is the navbar toggler that appears on small screens. It triggers the collapse and expand behavior of the navbar. The data-bs-toggle and data-bs-target attributes control the collapse functionality by targeting the collapsible element with the ID “navbarNav”.
1 |
<div class="collapse navbar-collapse" id="navbarNav"> |
This div is the container for the collapsible navbar items. It is initially collapsed and expands when triggered by the toggler button. The “navbar-collapse” class is responsible for the collapsing behavior, and the “id” attribute should match the “data-bs-target” attribute of the toggler button.
1 |
<ul class="navbar-nav"> |
This unordered list contains the navbar items, such as navigation links and dropdown menus.
1 |
<li class="nav-item"> and <a class="nav-link" href="#"> |
These lines define each navbar item. Each item is wrapped in an <li> element with the “nav-item” class, and the link itself is an <a> element with the “nav-link” class. You can add or modify these items as needed. The “#” in the href attribute represents a placeholder link, which should be replaced with the actual URLs of your pages.
In summary, this code creates a responsive navbar with a light background color. It includes a brand/logo on the left side and a collapsible menu with navigation links on the right side. The navbar toggler button appears on small screens to expand and collapse the menu.
Bootstrap 5 Example: How to create navbar with Dropdown Menu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Navbar Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container"> <a class="navbar-brand" href="#">Logo</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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"> <a class="nav-link" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Services </a> <ul class="dropdown-menu" aria-labelledby="navbarDropdown"> <li><a class="dropdown-item" href="#">Service 1</a></li> <li><a class="dropdown-item" href="#">Service 2</a></li> <li><a class="dropdown-item" href="#">Service 3</a></li> </ul> </li> <li class="nav-item"> <a class="nav-link" href="#">Contact</a> </li> </ul> </div> </div> </nav> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code Explanation:
1 |
<nav class="navbar navbar-expand-lg navbar-light bg-light"> |
This line creates the main container for the navbar. The navbar class specifies that it’s a navbar, while navbar-expand-lg makes it expand to the full width on large screens. navbar-light sets a light color scheme, and bg-light applies a light background color.
1 |
<div class="container"> |
This div sets the maximum width of the navbar content. It acts as a container for the navbar items, providing a consistent layout.
1 |
<a class="navbar-brand" href="#">Logo</a> |
This line defines the brand or logo of the website, which is displayed on the navbar. It is a link (<a>) with the navbar-brand class. The “#” in the href attribute represents a placeholder link and should be replaced with the actual URL of your logo or home page.
1 2 3 4 |
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> |
This button is the navbar toggler that appears on small screens. It triggers the collapse and expand behavior of the navbar. The data-bs-toggle and data-bs-target attributes control the collapse functionality by targeting the collapsible element with the ID “navbarNav”.
1 |
<div class="collapse navbar-collapse" id="navbarNav"> |
This div is the container for the collapsible navbar items. It is initially collapsed and expands when triggered by the toggler button. The “navbar-collapse” class is responsible for the collapsing behavior, and the “id” attribute should match the “data-bs-target” attribute of the toggler button.
1 |
<ul class="navbar-nav"> |
This unordered list contains the navbar items, such as navigation links and dropdown menus.
1 |
<li class="nav-item"> and <a class="nav-link" href="#"> |
These lines define each navbar item. Each item is wrapped in an <li> element with the “nav-item” class, and the link itself is an <a> element with the “nav-link” class. You can add or modify these items as needed. The “#” in the href attribute represents a placeholder link, which should be replaced with the actual URLs of your pages.
1 2 |
<li class="nav-item dropdown"> and <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> |
This code represents a dropdown menu item. It has the “nav-item dropdown” classes for styling, and the “dropdown-toggle” class indicates that it triggers a dropdown menu. The data-bs-toggle, data-bs-target, and aria-expanded attributes control the dropdown functionality. The “#” in the href attribute represents a placeholder link.
1 |
<ul class="dropdown-menu" aria-labelledby="navbarDropdown"> |
This unordered list represents the dropdown menu itself. It has the “dropdown-menu” class and the aria-labelledby attribute to associate it with the dropdown toggle button.
1 |
<li><a class="dropdown-item" href="#">Service 1</a></li> |
These lines represent individual items within the dropdown menu. Each item is a list item (<li>) containing an anchor link (<a>) with the “dropdown-item” class. You can add or modify these items as needed. The “#” in the href attribute represents a placeholder link.
1 |
<li class="nav-item"><a class="nav-link" href="#">Contact</a></li> |
This line represents a regular navbar item for the “Contact” link. It has the “nav-item” class for styling, and the link itself has the “nav-link” class. The “#” in the href attribute represents a placeholder link.
In summary, this code creates a Bootstrap 5 navbar with a light background color. It includes a brand/logo on the left side and a collapsible menu with navigation links on the right side. The navbar toggler button appears on small screens to expand and collapse the menu. The code also demonstrates the usage of a dropdown menu for the “Services” item, which expands to show additional options when clicked.
You can modify the content, styling, and functionality of this code to suit your specific needs by replacing the placeholder links, adding or removing navbar items, and customizing the appearance using Bootstrap’s CSS classes.
Bootstrap 5 Example: How to create Navbar with Search From
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Navbar Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container"> <a class="navbar-brand" href="#">Logo</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 me-auto"> <li class="nav-item"> <a class="nav-link" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Services </a> <ul class="dropdown-menu" aria-labelledby="navbarDropdown"> <li><a class="dropdown-item" href="#">Service 1</a></li> <li><a class="dropdown-item" href="#">Service 2</a></li> <li><a class="dropdown-item" href="#">Service 3</a></li> </ul> </li> <li class="nav-item"> <a class="nav-link" href="#">Contact</a> </li> </ul> <form class="d-flex"> <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </nav> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code Explanation:
1 |
<nav class="navbar navbar-expand-lg navbar-light bg-light"> |
This line creates the main container for the navbar. The navbar class specifies that it’s a navbar, while navbar-expand-lg makes it expand to the full width on large screens. navbar-light sets a light color scheme, and bg-light applies a light background color.
1 |
<div class="container"> |
This div sets the maximum width of the navbar content. It acts as a container for the navbar items, providing a consistent layout.
1 |
<a class="navbar-brand" href="#">Logo</a> |
This line defines the brand or logo of the website, which is displayed on the navbar. It is a link (<a>) with the navbar-brand class. The “#” in the href attribute represents a placeholder link and should be replaced with the actual URL of your logo or home page.
1 2 3 4 |
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> |
This button is the navbar toggler that appears on small screens. It triggers the collapse and expand behavior of the navbar. The data-bs-toggle and data-bs-target attributes control the collapse functionality by targeting the collapsible element with the ID “navbarNav”.
1 |
<div class="collapse navbar-collapse" id="navbarNav"> |
This div is the container for the collapsible navbar items. It is initially collapsed and expands when triggered by the toggler button. The “navbar-collapse” class is responsible for the collapsing behavior, and the “id” attribute should match the “data-bs-target” attribute of the toggler button.
1 |
<ul class="navbar-nav me-auto"> |
This unordered list contains the navbar items, such as navigation links and dropdown menus. The “me-auto” class is used to align the items to the left side of the navbar.
1 |
<li class="nav-item"> and <a class="nav-link" href="#"> |
These lines define each navbar item. Each item is wrapped in an <li> element with the “nav-item” class, and the link itself is an <a> element with the “nav-link” class. You can add or modify these items as needed. The “#” in the href attribute represents a placeholder link, which should be replaced with the actual URLs of your pages.
1 2 3 |
<li class="nav-item dropdown"> and <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> |
This code represents a dropdown menu item. It has the “nav-item dropdown” classes for styling, and the “dropdown-toggle” class indicates that it triggers a dropdown menu. The data-bs-toggle, data-bs-target, and aria-expanded attributes control the dropdown functionality. The “#” in the href attribute represents a placeholder link.
1 |
<ul class="dropdown-menu" aria-labelledby="navbarDropdown"> |
This unordered list represents the dropdown menu itself. It has the “dropdown-menu” class and the aria-labelledby attribute to associate it with the dropdown toggle button.
1 |
<li><a class="dropdown-item" href="#">Service 1</a></li> |
These lines represent individual items within the dropdown menu. Each item is a list item (<li>) containing an anchor link (<a>) tag (class=”dropdown-item”) with the placeholder link (href=”#”). You can modify these items and their links to match your specific dropdown menu options.
1 |
<li class="nav-item"><a class="nav-link" href="#">Contact</a></li> |
This line represents a regular navbar item for the “Contact” link. It has the “nav-item” class for styling, and the link itself has the “nav-link” class. The “#” in the href attribute represents a placeholder link.
1 |
<form class="d-flex"> |
This line starts the search form section of the navbar. The “d-flex” class is a utility class that applies flexbox properties to the form, allowing its elements to be flexible and responsive.
1 2 |
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">: |
This line represents the search input field. It has the “form-control” class for styling, the “me-2” class for adding a margin on the right side, and the placeholder text “Search”. The type=”search” attribute specifies that it’s a search input field, and the aria-label attribute provides an accessible label for screen readers.
1 |
<button class="btn btn-outline-success" type="submit">Search</button> |
This line creates the search button. It has the “btn” class for styling, “btn-outline-success” class for a button with an outline and a success color scheme. The type=”submit” attribute specifies that it’s a submit button for the search form.
In summary, this code creates a Bootstrap 5 navbar with a light background color. It includes a brand/logo on the left side, a collapsible menu with navigation links, a dropdown menu for the “Services” item, and a search form on the right side. You can customize the content, styling, and functionality of this code to fit your specific needs by replacing the placeholder links, adding or removing navbar items, modifying the dropdown menu options, and adjusting the search form properties.
Bootstrap 5 Grid System Examples:
Bootstrap 5 Example: how to Create Image Grid With Caption:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Grid System Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-4"> <div class="image-container"> <img src="image1.jpg" alt="Image 1"> <div class="caption">Caption 1</div> </div> </div> <div class="col-lg-4"> <div class="image-container"> <img src="image1.jpg" alt="Image 2"> <div class="caption">Caption 2</div> </div> </div> <div class="col-lg-4"> <div class="image-container"> <img src="image1.jpg" alt="Image 3"> <div class="caption">Caption 3</div> </div> </div> </div> <div class="row"> <div class="col-lg-4"> <div class="image-container"> <img src="image1.jpg" alt="Image 4"> <div class="caption">Caption 4</div> </div> </div> <div class="col-lg-4"> <div class="image-container"> <img src="image1.jpg" alt="Image 5"> <div class="caption">Caption 5</div> </div> </div> <div class="col-lg-4"> <div class="image-container"> <img src="image1.jpg" alt="Image 6"> <div class="caption">Caption 6</div> </div> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code explanation:
This code creates a grid layout using the Bootstrap 5 grid system. Let’s break down the code and understand its structure:
The code starts with a “<div>” element with the class “container”. This class is provided by Bootstrap to create a responsive container that centers the content and adds some padding to the sides.
Inside the container, there is a “<div>” element with the class “row”. The “row” class is used to create a horizontal row to hold the columns.
Within the row, there are three “<div>” elements with the class “col-lg-4”. The “col-lg-4” class specifies that each column should take up four units out of twelve on large screens (lg).
Inside each column, there is a “<div>” element with the class “image-container”. This element acts as a container for the image and caption.
Within the image container, there is an “<img>” element with the “src” attribute pointing to an image file and the alt attribute providing alternative text for the image.
After the image, there is a “<div>” element with the class “caption”. This element holds the caption text for the image.
The code repeats this structure six times to create a grid layout with two rows and three columns. Each column contains an image with a corresponding caption.
By using the Bootstrap grid system classes, the code creates a responsive layout that adjusts based on the screen size. On larger screens, the columns span four units out of twelve, allowing three columns to fit in a row. On smaller screens, the columns will stack vertically, one on top of the other, to ensure optimal viewing experience.
Overall, this code demonstrates how to use the Bootstrap grid system to create a grid layout with images and captions, allowing for easy responsiveness and alignment of content.
Bootstrap 5 Example: how to create Image Carousel using Bootstrap 5 Grid:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Grid System Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <div id="carouselExampleIndicators" class="carousel slide" data-bs-ride="carousel"> <div class="carousel-indicators"> <button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button> <button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="1" aria-label="Slide 2"></button> <button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="2" aria-label="Slide 3"></button> </div> <div class="carousel-inner"> <div class="carousel-item active"> <img src="image1.jpg" class="d-block w-100" width="500" height="500" alt="Image 1"> <div class="carousel-caption"> <h3>Image 1</h3> <p>Description for Image 1</p> </div> </div> <div class="carousel-item"> <img src="image2.jpg" class="d-block w-100" width="500" height="500" alt="Image 2"> <div class="carousel-caption"> <h3>Image 2</h3> <p>Description for Image 2</p> </div> </div> <div class="carousel-item"> <img src="image3.jpg" class="d-block w-100" width="500" height="500" alt="Image 3"> <div class="carousel-caption"> <h3>Image 3</h3> <p>Description for Image 3</p> </div> </div> </div> <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="visually-hidden">Previous</span> </button> <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="visually-hidden">Next</span> </button> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code Explanation:
This code creates an image carousel using Bootstrap 5’s Carousel component. Let’s break down the code and understand its structure:
The code starts with the opening “<body>” tag, indicating the beginning of the HTML body section.
Inside the body, there is a “<div>” element with the ID “carouselExampleIndicators” and a class “carousel slide”. This <div> acts as the main container for the carousel component.
Within the main container, there is a “<div>” element with the class “carousel-indicators”. Inside this “<div>”, there are three “<button>” elements. These buttons serve as indicators for each slide in the carousel. Each button has the data-bs-target attribute set to the ID of the main container, and the data-bs-slide-to attribute set to a specific index value, indicating which slide it corresponds to.
Next, there is another “<div> element with the class “carousel-inner”. This “<div>” holds the actual slides of the carousel. It contains three “<div>” elements with the class “carousel-item”. The first “<div>” has the additional class “active” to indicate that it is the initially displayed slide. Each slide consists of an “<img>” element with a source file specified by the src attribute, a class “d-block w-100” for proper styling, and width and height attributes to define the dimensions of the image. Additionally, each slide has a “<div>” element with the class “carousel-caption” that contains a heading (<h3>) and a paragraph (<p>) providing a description for the image.
After the carousel slides, there are two “<button>” elements with the classes “carousel-control-prev” and “carousel-control-next”. These buttons enable navigation to the previous and next slides in the carousel. They have the data-bs-target attribute set to the ID of the main container, and the data-bs-slide attribute set to “prev” and “next” respectively.
Within the buttons, there are “<span>” elements with the classes “carousel-control-prev-icon” and “carousel-control-next-icon”. These spans are responsible for displaying the previous and next icons.
The code then includes a “<script>” tag that links the Bootstrap JavaScript bundle file (bootstrap.bundle.js). This file provides the necessary functionality for the carousel component to work.
Overall, this code sets up a responsive image carousel with three slides, indicators, and navigation controls using Bootstrap 5’s Carousel component.
Bootstrap 5 Example: how to create Masonry Grid in bootstrap 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Grid System Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> <style> .custom-card { background-color: #f5f5f5; padding:20px; margin-bottom: 20px; } .custom-image { width: 100%; height: auto; border-radius: 5px; } .custom-heading { color: #333; font-size: 24px; font-weight: bold; margin-top: 10px; } .custom-paragraph { color: #777; font-size: 16px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-4"> <div class="custom-card"> <img src="image1.jpg" alt="Image 1" class="custom-image"> <h3 class="custom-heading">Image 1</h3> <p class="custom-paragraph">Welcome to Programming Digest, your go-to source for the latest updates, tutorials, and insights on programming languages, frameworks, and technologies.</p> </div> </div> <div class="col-md-4"> <div class="custom-card"> <img src="image2.jpg" alt="Image 2" class="custom-image"> <h3 class="custom-heading">Image 2</h3> <p class="custom-paragraph">Welcome to Programming Digest, your go-to source for the latest updates, tutorials, and insights on programming languages, frameworks, and technologies.</p> </div> </div> <div class="col-md-4"> <div class="custom-card"> <img src="image3.jpg" alt="Image 3" class="custom-image"> <h3 class="custom-heading">Image 3</h3> <p class="custom-paragraph">Welcome to Programming Digest, your go-to source for the latest updates, tutorials, and insights on programming languages, frameworks, and technologies.</p> </div> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code explanation:
This is an HTML code that creates a web page with a Bootstrap 5 grid system. The code starts by defining the document type and HTML tags. The “head” section contains metadata such as character encoding, viewport settings, and page title. It also includes a link to the Bootstrap CSS stylesheet and custom CSS styling rules.
The “body” section contains the main content of the page wrapped inside a Bootstrap container. Inside the container, there is a row with three columns, each with an image, heading, and paragraph text. The grid system uses the “col-md-4” class to specify that each column should occupy four units out of 12 on medium-sized screens.
The custom CSS styling rules define the appearance of the “custom-card”, “custom-image”, “custom-heading”, and “custom-paragraph” classes used in the HTML code. The “custom-card” class sets the background color, padding, and margin of the card. The “custom-image” class sets the width, height, and border radius of the image. The “custom-heading” and “custom-paragraph” classes set the font size, color, and margin of the heading and paragraph text.
Finally, the code includes a link to the Bootstrap JavaScript bundle, which is used for interactive features such as collapsing navigation menus and modals.
Bootstrap 5 Form Examples:
Bootstrap 5 Example: how to create a basic form in Bootstrap 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 form Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <form> <div class="mb-3"> <label for="name" class="form-label">Name</label> <input type="text" class="form-control" id="name" placeholder="Enter your name"> </div> <div class="mb-3"> <label for="email" class="form-label">Email address</label> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Bootstrap 5 example: How to create Inline Form in Bootstrap 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 form Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <form class="row g-3 align-items-center"> <div class="col-auto"> <label for="name" class="col-form-label">Name</label> </div> <div class="col-auto"> <input type="text" class="form-control" id="name" placeholder="Enter your name"> </div> <div class="col-auto"> <label for="email" class="col-form-label">Email address</label> </div> <div class="col-auto"> </div> <div class="col-auto"> <button type="submit" class="btn btn-primary">Submit</button> </div> </form> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Bootstrap 5 example: how to create Horizontal Form in Bootstrap 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 form Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <form> <div class="mb-3 row"> <label for="name" class="col-sm-2 col-form-label">Name</label> <div class="col-sm-10"> <input type="text" class="form-control" id="name" placeholder="Enter your name"> </div> </div> <div class="mb-3 row"> <label for="email" class="col-sm-2 col-form-label">Email address</label> <div class="col-sm-10"> </div> </div> <div class="mb-3 row"> <div class="col-sm-10 offset-sm-2"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
These examples demonstrate different form styles using Bootstrap 5 classes. You can modify them according to your specific requirements and extend them with additional form controls and validation as needed.
Bootstrap 5 Card Examples:
Bootstrap 5 example: how to create Card with Header and Footer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Card Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <div class="col-md-4"> <div class="card"> <div class="card-header"> Featured </div> <img src="image1.jpg" class="card-img" alt="Image"> <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> <div class="card-footer"> <small class="text-muted">Last updated 3 mins ago</small> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Bootstrap 5 Example: how to create Card with Image Overlay:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Card Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <div class="col-md-4"> <div class="card"> <img src="image2.jpg" class="card-img" alt="Image"> <div class="card-img-overlay"> <h5 class="card-title text-white">Card Title</h5> <p class="card-text text-white">Some quick example text to build on the card title and make up the bulk of the card's content.</p> <p class="card-text text-white">Last updated 3 mins ago</p> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
These examples demonstrate some of the basic features and layouts you can achieve using Bootstrap 5 cards. Feel free to customize them further by adding additional classes or styles based on your specific requirements.
Bootstrap 5 Modal Examples:
Bootstrap 5 Example: how to create Basic Bootstrap 5 Modal:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Modal Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal"> Open Modal </button> <div class="modal" id="myModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Modal Title</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <p>This is the modal content.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Bootstrap 5 Example: how to create Basic Bootstrap 5 Centered Modal:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Modal Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal"> Open Modal </button> <div class="modal" id="myModal" tabindex="-1"> <div class="modal-dialog modal-dialog-centered"> <div class="modal-content"> <p>Welcome to Programming Digest, your go-to source for the latest updates, tutorials, and insights on programming languages, frameworks, and technologies.</p> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code Explanation:
1 |
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal"> |
This “<button>” element is used to trigger the modal. It has the class “btn btn-primary” to style it as a primary button, and the data-bs-toggle and data-bs-target attributes are used to specify that it will toggle a modal component with the ID “myModal”. Open Modal This is the text displayed on the button, indicating that clicking it will open the modal.
1 |
<div class="modal" id="myModal" tabindex="-1"> |
This <div> element represents the modal component. It has the class “modal” and an ID of “myModal”. The tabindex=”-1″ attribute is used to make the modal focusable.
1 |
<div class="modal-dialog modal-dialog-centered"> |
This “<div>” element is the container for the modal dialog. It has the classes “modal-dialog” and “modal-dialog-centered” to center the modal vertically and horizontally.
1 |
<div class="modal-content"> |
This “<div>” element contains the actual content of the modal.
1 |
<p>Welcome to Programming Digest, your go-to source for the latest updates, tutorials, and insights on programming languages, frameworks, and technologies.</p> |
This is a paragraph element representing the content of the modal. In this example, it displays a welcome message related to a programming digest.
The overall functionality of this code is to create a button that, when clicked, triggers the opening of a modal window. The modal displays the specified content, in this case, a welcome message related to a programming digest. The modal component helps to create overlay-style windows that can be used to display additional information or interact with the user without navigating away from the current page.
These are just a few examples of Bootstrap 5 modals. You can further customize them by adding additional classes or modifying the content inside the modal to suit your specific needs. Remember to include the necessary Bootstrap 5 CSS and JavaScript files in your project for the modals to work correctly.
Bootstrap 5 Accordion Examples:
Bootstrap 5 example: how to create Basic Accordion in Bootstrap 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Accordion Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <div class="accordion" id="accordionExample"> <div class="accordion-item"> <h2 class="accordion-header" id="headingOne"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> Section 1 </button> </h2> <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> <div class="accordion-body"> Content for section 1 goes here. </div> </div> </div> <div class="accordion-item"> <h2 class="accordion-header" id="headingTwo"> <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> Section 2 </button> </h2> <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample"> <div class="accordion-body"> Content for section 2 goes here. </div> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Bootstrap 5 example: how to create Accordion with Different Styling:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap 5 Accordion Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <div class="accordion" id="accordionExample"> <div class="accordion-item"> <h2 class="accordion-header" id="headingOne"> <button class="accordion-button bg-primary text-white" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> Section 1 </button> </h2> <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> <div class="accordion-body"> Content for section 1 goes here. </div> </div> </div> <div class="accordion-item"> <h2 class="accordion-header" id="headingTwo"> <button class="accordion-button bg-danger text-white" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> Section 2 </button> </h2> <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample"> <div class="accordion-body"> Content for section 2 goes here. </div> </div> </div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
Code Explanation:
1 |
<div class="accordion" id="accordionExample"> |
This div element has the class “accordion” and an ID of “accordionExample”. It acts as a container for the accordion component.
1 |
<div class="accordion-item"> |
This div element represents an individual item within the accordion.
1 |
<h2 class="accordion-header" id="headingOne"> |
This h2 element serves as the header for the accordion item. It has the class “accordion-header” and an ID of “headingOne”.
1 2 3 4 |
<button class="accordion-button bg-primary text-white" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> |
This button element is used as the clickable trigger for expanding and collapsing the content of the accordion item. It has the class “accordion-button” and additional classes “bg-primary” and “text-white” to set the background color and text color. The “data-bs-toggle” and “data-bs-target” attributes are used to define the collapse behavior, and the “aria-expanded” and “aria-controls” attributes provide accessibility information.
1 |
Section 1 |
This is the text content of the button, displayed as the label for the accordion item.
1 2 3 |
<div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> |
This div element contains the collapsible content for the accordion item. It has an ID of “collapseOne” and the classes “accordion-collapse”, “collapse”, and “show”. The “aria-labelledby” attribute references the ID of the associated accordion header, and the “data-bs-parent” attribute points to the ID of the accordion container.
1 |
<div class="accordion-body"> |
This div element holds the actual content that will be displayed when the accordion item is expanded.
1 |
Content for section 1 goes here. |
This is the text content of the accordion item’s body, representing the content to be shown when the item is expanded.
The second accordion item follows a similar structure, with different IDs, classes, and content.
Overall, the code sets up a Bootstrap 5 accordion component with two collapsible items. Clicking on the buttons toggles the visibility of the corresponding content.