Creating custom styles in SCSS using the pseudo-element :after and targeting a specific class with the ampersand symbol

I am trying to add a transition effect to the element with an applied active class. My goal is to avoid duplicating my SCSS code (even though I understand it will be duplicated in the compiled CSS). The sole purpose of the active class is to modify the bottom-border-color of the triangle.

> li {
    &:after {
        position: absolute;
        content: '';
        border-bottom: 30px solid transparent;
        border-left: 30px solid transparent;
        border-right: 30px solid transparent;
        bottom: -100px;
        left: 50%;
        @include transform(0, -50%);
        @include transition(border-bottom-color .3s ease-in);

        &.active { 
            border-bottom-color: $color-bg-green;
        }
    }
}

Answer №1

My suggestion is to rewrite it like this. It seems that trying to nest the .active class within the :after pseudo-element may not work as intended.

> li {

    &:after {
        position: absolute;
        content: '';
        border-bottom: 30px solid transparent;
        border-left: 30px solid transparent;
        border-right: 30px solid transparent;
        bottom: -100px;
        left: 50%;
        @include transform(0, -50%);
        @include transition(border-bottom-color .3s ease-in);
    }    

    &.active:after {
        border-bottom-color: $color-bg-green;
    }

    /** an alternative approach if there are properties to set for .active **/
    &.active {
      someProperty: someValue;

      &:after {
        border-bottom-color: $color-bg-green;
      }
    }
}

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

What causes the ul element to display upon clicking the input field?

Why is the ul disappearing with display: none when the input element is clicked? Here is the HTML code snippet: When this input is clicked: <input class="input country" type="text"/> This unordered list should be displayed (but ...

Tips for ensuring Opera browser compatibility with buttons using the CSS background image property

Is there a way to make background images work on buttons in Opera web browser using CSS? I've tried using the background image property, but it doesn't show up when I use Opera. Strangely enough, it works fine in Firefox. However, I have noticed ...

What methods can we use to transform Bootstrap rows and columns to resemble a table design?

Attempt number one did not work due to padding issues in the columns. Attempt number two also failed, leading me to believe I may be making a mistake somewhere. This is what I am aiming for: https://i.sstatic.net/yTz6o.png Can anyone provide some assis ...

Attempting to insert a square-shaped div within a larger square-shaped div and enable it to be moved around by dragging

Imagine this scenario: I have a button and a large div. When the button is clicked, the code adds a new div inside the larger one. However, the new div is not draggable because I didn't implement the necessary code. I'm also trying to figure out ...

Is there a way to configure my dropdown menu so that only one dropdown can be open at a time and it remains open when clicking on a <li> item?

I've been working on developing a dropdown menu that appears when clicked, rather than hovered over. I've managed to implement this functionality using JavaScript, and overall, it's working quite well. Currently, the menu shows or hides whe ...

The functionality of CKEditor is compromised when used in conjunction with React Material-UI

I've been struggling with integrating Material UI and CKEditor5. The issue I'm facing is that CKEditor doesn't seem to be working properly. Despite trying to apply editor features like bold or italic, nothing changes on the screen. However, ...

What is the best way to align an HTML DIV with a logo on the left side?

I am trying to position a div called #top_menu in the center of the page. <div id="top_menu">This is the menu and it is centered on the page</div> Here is my CSS code: #top_menu { margin: 0 auto; width: 600px; ... } Next to the top menu, I ...

Currently, I am in the process of constructing a DSpace repository on an Ubuntu 16.04

I have successfully set up a DSpace repository on Ubuntu 16.04 LTS by following the installation instructions provided in this manual. After deploying the webapps, I encountered an issue when trying to add new items. Upon clicking the search link, I recei ...

Ways to align an image at the center within a Span

How can I horizontally center an image within a <span> element? https://i.sstatic.net/40IL0.png <div class="col-1"> <span class="dot"> <h:graphicImage library="imgs" name="campus.png" width="25" height="25" style="ali ...

What is the best way to align the start of my array in the middle of the screen?

I decided to implement a basic map function to display our array on the screen: let numbers = new Array(41); for (let i = 0, num = 0; i < numbers.length; i++, num += 5) { numbers[i] = num; } {numbers.map((item, index) => { retur ...

Issues arise when setting a delay for CSS transitions to occur due to the

I'm trying to create a scroll bar that only appears when hovered over, similar to how it works on Facebook where the scroll bar shows up slowly instead of all at once. I've tried using CSS transition delay, but it doesn't seem to be working ...

Utilizing conditional styling in React: the ability to add or attach CSS classes based on

Is there a more efficient way to implement conditional formatting for this scenario? const PaginationStorePageLink = ({ store, pageNum }) => (observer(({ PaginationStore }) => { const className = this.props.store.currentPage === this.props.pageNum ...

Troubles arise when trying to place images next to items in an unordered list

I'm trying to create an unordered list with list items that include images with bullets beside them. However, when I use a background image to achieve this, the image ends up behind the text. If I set the image as a list-style-image, it doesn't l ...

Adjust the size of the window to prevent the text from shifting

How can I lock text and tables in place when resizing the window? Can someone provide an example? ...

CSS - Unchanging absolute unit across all devices

After reading through this particular inquiry, it became evident that a pixel does not qualify as an absolute unit—a fact that I have validated through practical experience. Despite experimenting with pt, cm, and mm, I have yet to discover a definitive ...

Tips for concealing the loader once all pages have been loaded

Within the context of my website development project at , I am utilizing a script for infinite scrolling to consolidate multiple pages into one seamless scrolling experience. I am seeking a solution to hide the loader (the spinning icon) when no additiona ...

Looking for a way to ensure a div reaches 100% height within a specified area without exceeding the boundaries of the body?

I am working with jQuery mobile and have specific requirements for my project: The main content will be a large graphic that needs to fill the entire page, with both horizontal and vertical scrolling if necessary. The header should remain fixed at the to ...

Sliding horizontally with a responsive touch

I am looking to implement a horizontal responsive page navigation, similar to the illustration shown below: This is my current progress: DEMO $(document).ready(function () { var slideNum = $('.page').length, wrapperWidth = 100 * s ...

The functionality of Bootstrap 5 sass components seems to be malfunctioning

Despite having Bootstrap 5 installed, my design is working perfectly fine but the Bootstrap components like accordions and alerts are not functioning properly. It appears that these components are missing from Bootstrap and the sass files do not have any ...

Challenges with jQuery parent method reaching the grandparent element

Hey, I'm a newbie to jQuery and I'm trying to make changes to divs that are two levels above the function trigger: Here's my initial attempt: I try to locate the closest '.node' which is the parent of all other divs and then modif ...