What is the best approach to utilize Sass for setting a default value that can be easily overridden if needed?

My goal is to harness the power of Sass in order to generate a reusable HTML snippet with predetermined values that can later be customized if needed. Let's imagine I am creating a button and have established some basic SCSS to make the button default to a red color:

%button-shared {
    $button-color: red;
    td {
        border-color: $button-color;
        color: $button-color;
        background-color: white;

        text-decoration: none;

        width: 50px;
        height: 15px;
        border-radius: 6px;
    }
}

I utilized a variable so that it could be defined in multiple locations. For instance, I desire a white button with an outlined border that matches the colored text, as shown here:

https://i.sstatic.net/tbcyO.jpg

Now, let's assume I assign different classes to my two buttons in HTML, namely button-one and button-two. My objective now is to apply the SCSS styling to these buttons by extending %button-shared to these specific classes. However, I also wish for button-one to have a different color than the default red:

.button-one {
    $button-color: green;
    @extend %cta-shared;
}

.button-two {
    @extend %cta-shared;
}

Regrettably, this approach results in both buttons being rendered in red. How can I structure my SCSS code to easily allow for the overriding of variables?

Answer №1

Utilize mix-ins in your Sass code by referring to this documentation: https://sass-lang.com/documentation/at-rules/mixin

@mixin button-shared($button-color: red) {
   td {
        background-color: white;
        border-color: $button-color;
        color: $button-color;

        text-decoration: none;

        width: 50px;
        height: 15px;
        border-radius: 6px;
    }
}

.button-one {
    @include button-shared(green);
}

.button-two {
    @include button-shared();
}

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

Is it possible to create a dropdown like this using CSS?

Planning to create a new webpage. The main feature will be a dropdown menu that I envision like this: Check out the design here: http://img694.imageshack.us/img694/9350/dropdown.png Wondering if it can be achieved purely with CSS, or would I have to reso ...

Styling dropdown menus with CSS

I have encountered an issue with the dropdownlist control on my asp.net page. I set the width to 150px, but in Mozilla browser, the list items appear expanded while in IE, they are restricted to the width of the dropdown list. How can I achieve a consisten ...

I believe it's just basic jQuery with parents

I am having trouble figuring out how to move the class .inner separately in the USPs! Can someone please explain how this can be achieved? Any suggestions on how to solve this issue? HTML <div class="usps"> <div class="dienst"> & ...

Guide to importing a scss file into a scss class

Is there a way to apply a different theme by adding the "dark-theme" class to the body? I've attempted the following implementation: @import '../../../../node_modules/angular-grids/styles/material.scss'; .app-dark { @import '../../. ...

A function that listens for the onmouseover event and updates the image source of an HTML img element

I have created a function that positions my objects based on the array they are assigned to. For example, if they are in players[0], they will be placed in one position, and if they are in players[1], they will be placed elsewhere. The X and Y values are ...

Tips for styling cells in a certain column of an ng-repeat table

I am currently facing an issue with a table I have created where the last column is overflowing off the page. Despite being just one line of text, it extends beyond the right edge of the page without being visible or scrollable. The table is built using th ...

Solving issues with malfunctioning Angular Materials

I'm facing an issue with using angular materials in my angular application. No matter what I try, they just don't seem to work. After researching the problem online, I came across many similar cases where the solution was to "import the ...

How to use jQuery to hide radio buttons that are not checked when clicking a submit

Looking to dynamically hide all unchecked radio buttons and their labels until a submit button is clicked, displaying only the checked radio button. <form method="post"> <input type="radio" name="radiobtn"> <label for="first">Fir ...

Struggling to pass an array as a return value from a nested function

I'm seeking assistance with the code snippet below. I am encountering difficulties in accessing the "result" variable outside of the selectCb function scope. While "result" is properly assigned and functional within the selectCb scope, it becomes inac ...

How can we effectively utilize LESS variables in styles.less when working with LESS files within components in Angular versions 6 or 7?

Running Angular version 7.0.0 will generate a folder structure typical for "ng new". Below is the content of my styles.less file: @personal-black: #0000; This snippet shows the content of my app.component.less file: ...

Making all elements the same height in rows with w3.css

Struggling to achieve equal height for the elements in my rows. Any suggestions on how to make this work? Thank you and cheers. <div class="w3-row"> <div class="w3-container w3-quarter w3-green text-center matrix-element"> < ...

Creating elegant text in HTML using only CSSLearn how to design stylish text using CSS without any additional code

Can you generate stylish text using just CSS in HTML? Check out this link for a great demonstration of fancy text. ...

Can someone provide an explanation for why CSS filter performance is significantly slower on Safari when using a different order than usual

It appears that the positioning of filter: none; in CSS has a significant impact on speed in Safari. Can someone offer a detailed explanation of what is going on? Compare the following two examples in Safari only Example 1: (Having filter: none; at the e ...

Tips for choosing the parent's parent and applying a width of 100%

I am currently facing some challenges while creating a simple navigation bar from scratch. I am struggling with setting specific widths, especially wanting .navbarDropBtn to align its width with .navbarMain. However, it seems to only match the width of the ...

The footer is seen positioned halfway down the web page

Accidentally, I placed my footer within the main tag which initially displayed correctly. However, upon correcting it, the footer ended up displaying between the header and the main sections... This project is extremely important for school and the deadlin ...

Alter the font color upon clicking the menu using jQuery

When I click on the menu and sub-menu items, I want to change their colors along with their parent. You can see an example of how I want it to work here. However, currently, when I click on a sub-menu item, the color of the parent menu item gets removed. ...

Creating a bordered triangle using only CSS

Looking to create a message holder with a triangular tip bordered shape? I've successfully crafted the tip using two triangles: #triangle-border { width: 0px; height: 0px; border-style: solid; border-width: 0 100px 80px 100px; bor ...

Is there a flaw in how Firefox handles text-overflow and inline-block?

It seems that according to the text-overflow specification, the ellipsis effect only applies to inline elements: This property defines how content within an inline element is displayed when it overflows its container's boundaries in the inline dire ...

problem with overflow in HTML and CSS opera browser

My gradient is not displaying properly in the Opera browser. Can anyone help me troubleshoot? Check out the code on jsfiddle for reference:) http://jsfiddle.net/KtDTK/7/ <div class="product_1"> <div class="product_block"> <div cla ...

Table header sticking does not work when overflow is set to scroll or auto

After trying numerous solutions without success, I am reposting this question. My table has a horizontal scroll and I attempted to make the table header sticky using position:sticky, but it caused the scrolling functionality to stop working. Is there a wa ...