Carousel
Bootstrap Carousel is a flexible, responsive way to add a slider to your site. It can display slides of images, text, or custom markup, cycling through elements as a slideshow. Carousels are often used to showcase featured content or highlight important information in a visually engaging way.
Key Classes and Components of Bootstrap Carousel
Here are the primary classes and components used in Bootstrap Carousels:
– .carousel: The main wrapper class for the carousel.
– .carousel-inner: Encloses the carousel items.
– .carousel-item: Represents an individual content item within the carousel.
– .carousel-control-prev and .carousel-control-next: For adding previous and next control buttons.
– .carousel-indicators: For adding indicators that represent each slide and can be used to switch between them.
Basic Bootstrap Carousel
!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Bootstrap Carousel with Captions</title>
<link href=”https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css” rel=”stylesheet”>
</head>
<body>
<div id=”carouselWithCaptions” class=”carousel slide” data-ride=”carousel”>
<div class=”carousel-inner”>
<div class=”carousel-item active”>
<img src=”https://images.unsplash.com/photo-1700771266232-7a31af68eb31?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHx0b3BpYy1mZWVkfDN8Ym84alFLVGFFMFl8fGVufDB8fHx8fA%3D%3D” class=”d-block w-100″ alt=”Slide 1″>
<div class=”carousel-caption d-none d-md-block”>
<h5>First Slide Title</h5>
<p>Description for the first slide.</p>
</div>
</div>
<div class=”carousel-item”>
<img src=”https://images.unsplash.com/photo-1700685188486-d1acaf2f22c2?q=80&w=1931&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D” class=”d-block w-100″ alt=”Slide 2″>
<div class=”carousel-caption d-none d-md-block”>
<h5>Second Slide Title</h5>
<p>Description for the second slide.</p>
</div>
</div>
<!– More carousel items –>
</div>
<a class=”carousel-control-prev” href=”#carouselWithCaptions” role=”button” data-slide=”prev”>
<span class=”carousel-control-prev-icon” aria-hidden=”true”></span>
<span class=”sr-only”>Previous</span>
</a>
<a class=”carousel-control-next” href=”#carouselWithCaptions” role=”button” data-slide=”next”>
<span class=”carousel-control-next-icon” aria-hidden=”true”></span>
<span class=”sr-only”>Next</span>
</a>
</div>
<script src=”https://code.jquery.com/jquery-3.3.1.slim.min.js”></script>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js”></script>
<script src=”https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js”></script>
</body>
</html>
Explanation
– The carousel is created with a `<div>` element using the `.carousel` and `.slide` classes. The `data-ride=”carousel”` attribute initializes the carousel functionality.
– Inside the carousel, `.carousel-inner` acts as a container for the slides, each represented by `.carousel-item`.
– The first `.carousel-item` is marked as `.active` to denote that it should be visible when the carousel loads.
– `<img>` tags within `.carousel-item` display the content of each slide.
– Navigation controls are added with `.carousel-control-prev` and `.carousel-control-next`. These include both an icon (`.carousel-control-prev-icon` and `.carousel-control-next-icon`) and screen-reader text (`.sr-only`).
– `.carousel-indicators` are used to display a visual indicator of the current slide and allow users to switch between slides.
– Including Bootstrap’s JavaScript, along with jQuery and Popper.js, is essential for enabling the interactive functionality of the carousel.
This example provides a basic implementation of a Bootstrap carousel, showcasing a simple slideshow with controls and indicators. The carousel is fully responsive, ensuring it works well across a range of devices and screen sizes.
Creating a Bootstrap Carousel with Captions
To add captions to your Bootstrap carousel, you can include a `<div>` with the class `.carousel-caption` inside each `.carousel-item`. This caption can contain any HTML content, including headings, paragraphs, or buttons, and will be displayed over the carousel image.
Bootstrap Carousel with Captions
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Bootstrap Carousel with Captions</title>
<link href=”https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css” rel=”stylesheet”>
</head>
<body>
<div id=”carouselWithCaptions” class=”carousel slide” data-ride=”carousel”>
<div class=”carousel-inner”>
<div class=”carousel-item active”>
<img src=”slide1.jpg” class=”d-block w-100″ alt=”Slide 1″>
<div class=”carousel-caption d-none d-md-block”>
<h5>First Slide Title</h5>
<p>Description for the first slide.</p>
</div>
</div>
<div class=”carousel-item”>
<img src=”slide2.jpg” class=”d-block w-100″ alt=”Slide 2″>
<div class=”carousel-caption d-none d-md-block”>
<h5>Second Slide Title</h5>
<p>Description for the second slide.</p>
</div>
</div>
<!– More carousel items –>
</div>
<a class=”carousel-control-prev” href=”#carouselWithCaptions” role=”button” data-slide=”prev”>
<span class=”carousel-control-prev-icon” aria-hidden=”true”></span>
<span class=”sr-only”>Previous</span>
</a>
<a class=”carousel-control-next” href=”#carouselWithCaptions” role=”button” data-slide=”next”>
<span class=”carousel-control-next-icon” aria-hidden=”true”></span>
<span class=”sr-only”>Next</span>
</a>
</div>
<script src=”https://code.jquery.com/jquery-3.3.1.slim.min.js”></script>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js”></script>
<script src=”https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js”></script>
</body>
</html>
Explanation
– Each `.carousel-item` contains an `<img>` tag for the slide image and a `.carousel-caption` for the slide’s caption.
– The `.carousel-caption` div typically includes a heading (`<h5>`) and a paragraph (`<p>`) to describe the slide. You can customize this content as needed.
– The `d-none d-md-block` classes on the caption div ensure that the caption is hidden on very small screens (like phones) and visible on medium-sized screens and up. You can adjust this behavior based on your responsive design needs.
– As before, navigation controls and indicators are included to navigate through the carousel slides.
This example demonstrates how to enhance a Bootstrap carousel with descriptive captions, adding context to the images and making the carousel a more informative and engaging component of the webpage.
YouTube Reference :