Do you know of a more streamlined approach to formatting this SCSS code?

Currently working on a Saleor Site and diving into the world of Django and SASS for the first time.

While crafting my own styling rules in SCSS files, I've noticed some repetitive code that could potentially be streamlined. It seems like there should be a more efficient way to handle this without duplicating so much content. Unfortunately, I couldn't find any helpful style guides specific to SCSS.

Anyone have suggestions on how to optimize this code?

.p {
    &-around {
        &_none {
            padding: $none;
        }
        &_x-small {
            padding: $x-small;
        }
        &_small {
            padding: $small;
        }
        &_medium {
            padding: $medium;
        }
        &_large {
            padding: $large;
        }
        &_x-large {
            padding: $x-large;
        }
    }
    &-top {
        /* only real difference is just "padding-top" instead of "padding" */
        &_none { 
            padding-top: $none;
        }
        &_x-small {
            padding-top: $x-small;
        }
        &_small {
            padding-top: $small;
        }
        &_medium {
            padding-top: $medium;
        }
        &_large {
            padding-top: $large;
        }
        &_x-large {
            padding-top: $x-large;
        }
    }
    /* This pattern repeats for right, bottom, vertical, and horizontal padding adjustments */
}

All feedback is appreciated.

Edit: Here's the improved code after implementing Jakob's suggestion for optimization.

@each $size, $value in (
    'none'   : $none,
    'x-small': $x-small,
    'small'  : $small,
    'medium' : $medium,
    'large'  : $large,   
    'x-large': $x-large        
){
    .p {
        &-around_#{$size}     { padding:        $value; }
        &-vertical_#{$size}   { padding-top:    $value; padding-bottom: $value; }
        &-horizontal_#{$size} { padding-left:   $value; padding-right:  $value; }
        &-top_#{$size}        { padding-top:    $value; }
        &-bottom_#{$size}     { padding-bottom: $value; }
        &-right_#{$size}      { padding-right:  $value; }
        &-left_#{$size}       { padding-left:   $value; }
    }
    .m {
        &-around_#{$size}     { margin:        $value; }
        &-vertical_#{$size}   { margin-top:    $value; margin-bottom: $value; }
        &-horizontal_#{$size} { margin-left:   $value; margin-right:  $value; }
        &-top_#{$size}        { margin-top:    $value; }
        &-bottom_#{$size}     { margin-bottom: $value; }
        &-right_#{$size}      { margin-right:  $value; }
        &-left_#{$size}       { margin-left:   $value; }
    }
}

Answer №1

In my opinion, the best approach would be to utilize the map, @each loop, and interpolation using #{} like this:

$padding: (
    'none'   : none,
    'x-small': 1px,
    'small'  : 2px,
    'medium' : 3px,
    'large'  : 4px,   
    'x-large': 5px        
);

.p {
    @each $size, $value in $padding {
        &-around_#{$size} { padding:        $value; }  
        &-top_#{$size}    { padding-top:    $value; }       
        &-right_#{$size}  { padding-right:  $value; }       
        &-bottom_#{$size} { padding-bottom: $value; }               
        &-left_#{$size}   { padding-left:   $value; }               
    }
}

If you prefer to maintain variables, you could try this method instead:

.p {
    @each $size, $value in (
        'none'   : $none,
        'x-small': $x-small,
        'small'  : $small,
        'medium' : $medium,
        'large'  : $large,   
        'x-large': $x-large        
    ){
        &-around_#{$size} { padding:        $value; }  
        &-top_#{$size}    { padding-top:    $value; }       
        &-right_#{$size}  { padding-right:  $value; }       
        &-bottom_#{$size} { padding-bottom: $value; }               
        &-left_#{$size}   { padding-left:   $value; }               
    }
}

Answer №2

Perhaps this approach would yield the optimal result.

.p {
    &-around, &-top {
        &_none {
            padding: $none;
        }
        &_x-small {
            padding: $x-small;
        }
        &_small {
            padding: $small;
        }
        &_medium {
            padding: $medium;
        }
        &_large {
            padding: $large;
        }
        &_x-large {
            padding: $x-large;
        }
    }
}

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

Various lists do not appear directly under each other

Hello everyone, I recently created a webpage displaying different lists of football players. My goal is to keep the lists stacked vertically without any floats that would make them appear side by side. The separate lists are essential for properly categori ...

Decrease the size of the "subscribe" button

Our "subscribe" button is currently displayed like this: https://i.sstatic.net/toiOb.png However, we are looking to reduce the extra width and have it appear more like this: https://i.sstatic.net/NaaYJ.png We are using the same code for both versions: ...

Drop-down functionality in Bootstrap Form becomes unresponsive after screen width is decreased

I recently encountered a strange issue with my simple Bootstrap form. When I resize my browser width to mobile size or view it on a mobile device, the form stops functioning properly. Unfortunately, I am unable to provide any specific code examples at this ...

Struggling to connect CSS to HTML on Github Pages

I'm new to using Github, and I noticed that when I open the website, my HTML code is displaying without the associated CSS. Here is a snippet of my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

Utilize Tailwind CSS to design a triangular shape positioned in the lower right corner of the parent div

Is there a way to position a triangle in the bottom right corner of a parent div without it extending beyond the boundaries? I attempted to use tailwind css for this, but the sides of the triangle are crossing over the parent div. Any suggestions on how to ...

Tips for confining a float within a div with fluctuating space

I have a scenario where I have multiple divs ('group') containing text, and there is a floated div ('toggle') in the bottom corner. The current code works well if the text length in 'group' is consistent, but the position of t ...

React Application not reflecting recent modifications made to class

My current project involves creating a transparent navigation bar that changes its background and text color as the user scrolls. Utilizing TailwindCSS for styling in my React application, I have successfully implemented the functionality. // src/componen ...

Alter the background shade of an item as it is added or removed from a multi-select list and transferred to another list

Is there a way to visually represent the movement of items between two multi-select lists? I have one list on the right and one on the left, and when objects move from right to left, I want them to have a red background (indicating removal) and if they mov ...

The issue of an HTML button positioned on top of an image not remaining fixed when the browser window is resized

After mastering the art of placing an html form element on top of an image, I am now facing the challenge of making sure that the button remains intact even when I resize the window. For a reference, please visit: Note: Despite its simplicity, I acknowle ...

Maximize background width with a gradient that is compatible with web-based email clients such as Outlook and Gmail

Recently, I was given the task of creating a newsletter to support cancer fundraising. Excitedly, I accepted the assignment but encountered a frustrating issue with web-based email clients (such as outlook.com and gmail.com) not accepting some of my stylin ...

What is the technique for anchoring elements at the top of the page when scrolling?

There is a common design feature where the sidebar on the left starts at a lower position on the page, but as you scroll it moves up to the top and remains fixed in place instead of disappearing off the screen. This is a popular design trend that I have ...

Alignment of text fields in a vertical manner

How can I vertically align text input fields using CSS? <form action='<?= $_SERVER['PHP_SELF']; ?>' method='post'> <p><label for='username' class=''>Username:</label& ...

Toggling jQuery to show or hide content depending on the selector

I am looking to implement a jQuery-based animation and here is the code I have so far: >items=document.querySelectorAll("#customers td span"); [<span alt=​" 02,Counter,11,2013-04-06 14:​59:​16">​ 02​</span>​, <span>​11 ...

CSS transition fails to revert

My standard opacity animation is not working in reverse order. Here is a link to the JSFiddle example. According to the documentation, it should work automatically. Since I am new to JavaScript, I am unsure if this issue lies in my code or if the CSS anima ...

Django Vue3 encounters access-control-allow-origin restriction

I am currently working on a Django rest-api project that uses Vue on the front-end. Unfortunately, I encountered an error while making requests via Vue: Console output: The following error is displayed: Access to XMLHttpRequest at 'https://api.iyziw ...

The <hr> tag is not displaying properly within the <option> element

I'm facing an issue with a selection element in my Vue project <div class="resultContainer"> <section> <option class="resultBtn" v-for="exchange in finalExchanges"> {{exchange}} <hr></option> </section> ...

Show a grid layout with adjustable sizing and elements centered in the middle

When dealing with a layout like the one below, how can we ensure that elements are centered within the wrap container? The margins around the elements should be flexible but at least 14px. .wrap{ display: grid; grid-template-columns: repeat(auto-fi ...

Show all entries belonging to a specific ID on individual rows for each ID using PHP

I have a table generated from inner joins that displays different articles associated with outfit IDs. id_outfit | name | article ---------------------------------------- 56 | one | shoe.png 56 | one | dress.png 56 | one ...

What is the best way to reposition the main body content lower on the page when switching to mobile layout?

I am facing an issue where my website switches to mobile design at 850px, but the button I have on both desktop and mobile mode gets hidden under the header banner when it transitions to mobile design. I want to adjust the main body content of the website ...

Collapse the accordion item when a different one is selected

Check out this code snippet that's working on jsfiddle I'm attempting to add a function to close any open accordion items when another one is clicked. I've added a class "open", but I'm having trouble removing the class when a differen ...