Adjust transparency in Bootstrap slider with picture indicator

Currently, I am in the process of creating an HTML page featuring a Bootstrap carousel. To enhance user interaction, I have replaced the standard selector with images that, when clicked, will transition the slider to display corresponding content.

In order to add another layer of visual appeal, I sought to adjust the opacity of the image selectors based on the active slide within the carousel. Initially setting all selectors at 0.5 opacity, I intended for each one to change to full opacity (1) when its respective slide is displayed. However, I encountered difficulty implementing this feature and now seek guidance on alternative methods to achieve the desired effect.

The following code outlines the structure of my image selector:

<ol class="carousel-indicators">
  <li data-target="#myCarousel" data-slide-to="0" class="active">
   <div class="img">
    <img src="xx.jpg" />
   </div>
  </li>
  <li data-target="#myCarousel" data-slide-to="1">
   <div class="img">
    <img src="xx.jpg" />
   </div>
  </li>
  <li data-target="#myCarousel" data-slide-to="2">
   <div class="img">
    <img src="xx.jpg" />
   </div>
 </li>
</ol>

Although I attempted to apply the following CSS rule, it did not produce the desired outcome:

.carousel-indicators .img.active {
  opacity:1;
}

Answer №1

Your current selector (.carousel-indicators .img.active) isn't functioning as expected because the .img element itself won't have an .active class. Instead, Bootstrap adds the class to its parent element, which is the li.

To address this issue, you can adjust the CSS selector to target the active indicator using .carousel-indicators .active. Additionally, there's no need for an extra div element; simply place the image directly inside the li container.

Take a look at the code snippet below where I've included multiple images with indicators that change opacity. Click the "Run code snippet" button to see it in action:

@import url("https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css");
.carousel-indicators li {
  border: 2px solid rgba(255, 255, 255, 0.5);
  height: 50px;
  margin-left: 2px;
  margin-right: 2px;
  opacity: 0.5;
  width: 50px;
}

.carousel-indicators li::after {
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.75);
  content: '';
  display: block;
  height: 1px;
  width: 100%;
}

.carousel-indicators .active {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
<div id="carouselWithImageIndicators" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carouselWithImageIndicators" data-slide-to="0" class="active">
      <img class="d-block w-100" src="https://source.unsplash.com/2rHw1I_IoT4/100x100" alt="" />
    </li>
    <li data-target="#carouselWithImageIndicators" data-slide-to="1">
      <img class="d-block w-100" src="https://source.unsplash.com/5PVXkqt2s9k/100x100" alt="" />
    </li>
    <li data-target="#carouselWithImageIndicators" data-slide-to="2">
      <img class="d-block w-100" src="https://source.unsplash.com/NE0XGVKTmcA/100x100" alt="" />
    </li>
    <li data-target="#carouselWithImageIndicators" data-slide-to="3">
      <img class="d-block w-100" src="https://source.unsplash.com/ewfHXBcuFA0/100x100" alt="" />
    </li>
    <li data-target="#carouselWithImageIndicators" data-slide-to="4">
      <img class="d-block w-100" src="https://source.unsplash.com/n3c5pSoD2Fc/100x100" alt="" />
    </li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src="https://source.unsplash.com/2rHw1I_IoT4/1600x900" alt="" />
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://source.unsplash.com/5PVXkqt2s9k/1600x900" alt="" />
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://source.unsplash.com/NE0XGVKTmcA/1600x900" alt="" />
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://source.unsplash.com/ewfHXBcuFA0/1600x900" alt="" />
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="https://source.unsplash.com/n3c5pSoD2Fc/1600x900" alt="" />
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselWithImageIndicators" 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="#carouselWithImageIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Graphic background integrated into form input

Is it advisable to create colored shapes in Illustrator, export them as SVG files, and then embed input tags or textareas within these shapes to allow users to enter text? Are there alternative methods we could use for this functionality? ...

What is the best way to display multiple divs in a single location?

I am facing an issue with displaying different text on mouseover for three buttons. I successfully implemented it for one button, but when I added the other two, it stopped working. Here is the code I used for the first button: Using jQuery- document.get ...

Is there a way to customize this JQuery and HTML code to be specific to each row?

In CB UserList, each row in the form appears multiple times representing different records. The below code is intended to display a submit button and modules for show/hide functionality separately for each record on the list. However, when a user makes a s ...

Designing a personalized carousel component in Angular

Looking to replicate this design, any tips? I'm aiming for a carousel layout with developers listed in a project, where the center item is larger than the others. Any guidance on how to achieve this look? ...

Trouble with connecting CSS files

Can anyone explain why my CSS file isn't linking properly when the HTML file is located in a subfolder within the directory that contains both the CSS and HTML folders? I attempted to remove the main folder path from the link, but the issue still pers ...

Preloading videos for optimal performance on mobile devices

Looking for a solution where 4 MP4/video files, each 5MB in size, can be played consecutively without gaps between them on all browsers and devices. Preferably not looking for a solution involving the replacement of video source files. The current approac ...

Utilizing CSS to expand a section of an image from a sprite sheet across a div

I need to resize a div that is 1x5 and display an embedded image from a sprite sheet. I am looking for a CSS solution to either repeat or expand this div to cover the area of a larger div. Is there a way to achieve this using only CSS, or will it require ...

Is there a way to align one <article> next to another <article> to create a horizontal layout?

Can anyone help me figure out how to display two <article>s on the same line or side-by-side, with the FirstArticle positioned to the left and the SecondArticle to the right? .MainContent { border-radius: 5px; -moz-border-radius: 5px; ...

Create a webpage layout using Bootstrap that features a left-aligned image within a div, with

Is there a way to achieve this layout using Bootstrap? View the desired design I am looking to create a div with an image on the left (float:left) and text (which could be one or multiple lines) along with a button aligned to the bottom right. The goal i ...

Embarking on the journey of nesting components within styled-components

Check out these custom components I created: <UserImg imgUrl={userPhoto}> <DropDown> <div onClick={handleAuth}>Sign Out</div> </DropDown> </UserImg> const DropDown = styled.div` letter-spacing: 0.2em; positi ...

The web element cannot be located, but it becomes visible when manually inspected

I'm facing an issue with accessing this menu in selenium as the web element doesn't show up in the inspector until I do it manually. <a id="cke_3275" class="cke_menubutton cke_menubutton__table cke_menubutton_off cke_menubutton_ ...

Utilizing JavaScript to eliminate objects from a collection and showcase a refreshed arrangement

On a simple webpage using bootstrap and js/jquery, data is fetched via ajax to create entries displayed to the viewer. The process involves: - making a request - parsing XML to create an array of objects with all the data - sorting by one property - cr ...

Swapping out ChildNode data for HTML elements

When attempting to replace a node value with HTML content, I encountered an issue where the HTML tags were being displayed on the HTML page instead of applying the intended style. <style> .YellowSelection { background: yellow; } < ...

downsides of scrolling text functionality

Sure, it may seem evil. But what makes it so? Is it because all browsers support it? Which aspx asp.net controls are restricted in this tag? What are the reasons for avoiding this tag? ...

Arrangement of HTML divs and styling classes

I have been experimenting with divs and classes in order to achieve proper alignment of text on my website. Currently, I am working with 2 classes that I would like to display side by side to create 2 columns. However, the right column containing the text ...

"Struggling to horizontally center a div with a fixed width in CSS - what am I

Whenever I try to use it on my browser, the content shifts to the left and I can't seem to center it using text-align or flexbox. The issue gets resolved when I remove the width property, but I really need it to have that paragraph-style look I'm ...

Synchronization issue between CSS style and Javascript is causing discrepancies

I am looking to avoid using jquery for simplicity. I have three websites that each page cycles through. My goal is to scale the webpages by different values. I attempted applying a class to each page and used a switch statement to zoom 2x on Google, 4x o ...

Styling Easy-Autocomplete with jQuery

I am currently working on integrating autocomplete functionality using the jquery easy-autocomplete plugin npm install --save easy-autocomplete I am facing two issues related to its styling. The results are being displayed in a bulleted list format. I ...

history.js - failure to save the first page in browsing history

I have implemented the history.js library to enable backward and forward browser navigation on an AJAX products page triggered by clicking on product categories. While I have managed to get this functionality working smoothly, I am facing a particular iss ...

Blurry oval box shadows in CSS are not visually appealing

I'm working on a school project that involves replicating a website. I'm trying to achieve a shadow effect under text in an oval shape, but so far my attempts have just resulted in a blurry rectangle. Here is what I have currently: Click here A ...