Is it time to go back to the parent selector using the ampersand symbol?

I recently started using Less for writing CSS, and it has definitely been a time-saver for me. However, I have encountered a small issue with my code:

<a href="#" class="btn-black-bg btn-right-skew">largest fight gym in Australia</a>

Less

.btn{
    position: relative;
    padding: 15px 22px;
    color: @color-black;

    &-black-bg{
        background: @color-black;
        color: @color-white;
    }

    &-right-skew{
        position: relative;

        &:after{
            content: '';
            position: absolute;
            right: -10px;
            top: 0px;
            width: 20px;
            height: 100%;
            .skewX(-15deg);
        }
    }
}

My objective now is to have the btn-right-skew also show a black background if btn-black-bg is present. In regular CSS, I could achieve this with the following code:

.btn-black-bg.btn-right-skew:after{
    background: #000;
}

However, I am unsure how to accomplish this using LESS. Any help or suggestions would be greatly appreciated.

Answer №1

After reviewing your HTML code, it appears that by adding background: #000 to the .btn-black-bg:after class alone should suffice. However, if you wish to apply certain properties only when both classes are present on the same element, you can utilize the parent selector as shown below:

.btn {
    &-black-bg&-right-skew:after {
        background: #000;
        color: #fff;
    }
}

It's important to note that nesting this within the &-black-bg or &-right-skew is not possible due to the order of classes being irrelevant in CSS. Consequently, the parent selector will always refer to the entire parent element rather than just the immediate parent. The closest form of nesting would involve the following structure, where the .btn class needs to be statically added to the innermost selector instead of using &:

.btn {
    &-black-bg {
        &.btn-right-skew:after {
            background: #000;
            color: #fff;
        }
    }
}

While variables can technically achieve nesting as described here, I do not recommend such an approach for your specific case since the aforementioned selector is clearer and more straightforward.

Answer №2

One suggestion is to organize the classes by creating a base class "btn" and additional modifier classes like "black-bg" and "right-skew". This approach can make it clearer how styles are being applied and combined.

Take a look at my example on codepen: http://codepen.io/fabiandarga/pen/bVNpLE

<a href="#" class="btn black-bg right-skew">largest fight gym in Australia</a><br /><a href="#" class="btn green-bg right-skew">largest fight gym in Australia</a>

Here is the CSS:

.btn {
    display: inline-block;
    position: relative;
    padding: 15px 22px;
    color: @color-black;

    &.black-bg {
        background: @color-black;
        color: @color-white;
    }
    &.green-bg {
        background: @color-green;
        color: @color-white;
    }

    &.right-skew {
        position: relative;

        &.black-bg {
            &:after {
                background: @color-black;
            }
        }
        &.green-bg:after {
            background: @color-green;
        }
        &:after {
            content: '';
            display: block;
            position: absolute;
            right: -10px;
            top: 0px;
            width: 20px;
            height: 100%;
            transform: skewX(-15deg);
        }
    }
}

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

The button remains active in Internet Explorer 10 and cannot be disabled

The button has been specified as <input type="button" id="disable" disabled="disabled" value="Upload" /> However, the appearance of the button does not show it as disabled in Chrome, Firefox, and Safari. Additionally, in Internet Explorer, the bu ...

Despite having unique ids, two file input forms are displayed in the same div in the image preview

Running into a minor issue once again. It may not be the most eloquent query, but I'm genuinely stuck and in need of some assistance. I am attempting to showcase image previews of selected files in a file input form. I have a jQuery script that reads ...

Switching the keyboard language on the client side of programming languages

I'm interested in altering the keyboard language when an input element changes. Is it possible to modify the keyboard language using client-side programming languages? And specifically, can JavaScript be used to change the keyboard language? ...

Assist in HTML to exhibit the display name property devoid of a label

Here is the code snippet I am working with: [Display(Name = "Empresa")] public string Company{ get; set; } In my ASPX file, I have: <th><%: Html.LabelFor(model => model.Company)%></th> This code generates the following output: < ...

Automatically adjust height directive to fill up the remaining space

Is there a way to dynamically adjust the height of an element to fill the remaining space within its container? In my current layout, I have a fixed-height header (or menu) in pixels, a content area that may overflow the window height and require scroll b ...

Poor mapping on Google Maps API

I've been trying to embed a Google Maps on my website, but for some reason, it keeps stretching and showing grey sections with rounded borders. Here's the code I'm using: <div class="p-5 col-6 d-flex flex-column justify-content-between"& ...

What is the best way to display and conceal various elements with each individual click?

Utilizing the onClick function for each triggering element, I have implemented a show/hide feature for multiple element IDs upon click. The default setting is to hide the show/hide elements using CSS display property, except for the initial 3 main elements ...

displaying a separate HTML file with its own distinct CSS styling

Recently, I was tasked with investigating an existing website. Currently, the website utilizes a single root html file that contains multiple iframes linking to external html pages. However, the team behind the website has noticed that this setup is causin ...

Exploring HTML segments with Python and BeautifulSoup

As a beginner, I am exploring web scraping examples from Automate the Boring Stuff. My goal is to create a Python script that automates downloading images from PHD Comics using the following steps: Identify the image link in the HTML and download it. ...

Click on the button to convert the content of the text area into a variable

Is there a way to capture all the text inside a textarea or input field as a variable upon button click using jQuery or JavaScript? The .select() function doesn't seem to achieve this, instead displaying numerous CSS properties when logged. What app ...

What is the process for adjusting the switch size in react-bootstrap?

I've been struggling to increase the size of the react-bootstrap switch without success. I searched through the documentation and tried various methods, but couldn't find a solution other than directly modifying the bootstrap css. Here's the ...

Issue with Chrome related to svg getScreenCTM function

Check out the jsFiddle demo I am attempting to utilize the getScreenCTM function to retrieve the mouse position based on SVG image coordinates. It seems to work in IE and Firefox, but not in Chrome. When I define the attributes width and height in the SV ...

The error message "email() is not a valid function when using the onclick attribute

Can anyone lend a hand? I feel like I must be overlooking something really obvious. I'm having trouble calling my function to execute my ajax call. Any assistance would be greatly appreciated. Thank you! Here is an excerpt of the HTML code: $(docu ...

How can you trigger a modal to appear when an image is clicked?

There are images inside <div> tags. When one of the images is clicked, a modal appears to display information about the specific image. Each modal has a unique id or class, such as modal4. My example: <image type="image" src="Images/Drake.jpg" ...

Transmitting the identification number of a post with the help of J

I am currently working on a while loop in PHP that fetches associated results from a query and displays them within a class identified by the user who created the post. My goal is to send the id of the class to PHP, similar to how I am sending the num vari ...

The width of table columns is altered upon the second rendering in JQuery DataTables

Is there a way to prevent JQuery DataTables cells from extending width after adding hyperlink to cell? I am facing an issue where I have two tables on the same page. The first table column has a link which populates the second table. It works fine when th ...

Conceal excessive content on elements set in a stationary position

How can we prevent overflow of a fixed div within a container? Initially, I attempted nesting fixed elements inside each other, but that did not solve the issue. The only solution that comes to mind is using "inverted" masks: other fixed divs that hide eve ...

Using Javascript to dynamically swap images within a div as the user scrolls

Can someone help me achieve a similar image scrolling effect like the one on the left side of this website: I am looking to change the image inside a div as I scroll and ensure that the page doesn't scroll further until all the images have been scrol ...

What is causing my radio button event to activate when a separate radio button is selected?

I have implemented the code below to display "pers" and hide "bus," and vice versa. JQuery: $('input[name="perorbus"]').click(function() { if ($(this).attr('id') == 'bus') { $('#showbus&apo ...

What is the best way to reveal CSS files downloaded via npm?

I'm currently working on a Sass/CSS library that I have hosted on npm. I am exploring ways to make it easier for clients to access it, such as using: @import 'library_name' Instead of the less appealing option: @import 'node-modules/ ...