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

Center a span vertically inside a div as the text within the span grows to occupy the entire div

I am facing an issue with a table that has 25 td's. Each td contains specific elements as shown below: <td> <div> <span> text </span> </div> </td> Additionally, there is a script in place that adj ...

Attempting to utilize a Python web scraping tool to extract content from a webpage that undergoes regular text updates

I must admit that I am a complete beginner who is feeling lost and unsure of what to do. My goal is to extract 4 pairs of numbers from a webpage using a web scraper. These numbers change when modified on a different page, but I'm unable to pull the d ...

Creating Flexible Designs - Implementing a responsive sidebar with dynamic scrolling

I am in the process of developing a simple web application that features a sidebar whose vertical size is dependent on the number of elements it contains. My goal is to make the web app responsive, ensuring it works well on devices of any size. In cases ...

Organize three image links side by side using HTML layout

Hello there! I'm currently working on a website project and I'm struggling with arranging some image links in a specific layout. The grey rectangles you see represent where the images should be placed, all of them are uniform in size (275 x 186) ...

Creating a Mui v5 grid item that maximizes its height within a dialog box

I need help setting the height of a Grid Item to occupy all available space in a MUI dialog. In the image below, I am trying to make the Left Side Panel, Main section, and Right Panel extend to fill the orange-colored dialog space. Here is the code on Co ...

Tips for extracting designated link by employing a Selector CSS Query

Please note: While a similar question has been asked on JSoup:How to Parse a Specific Link, I have a more specific variation of this inquiry. Kindly read on for further details. In order to extract data from a particular site link, I am looking to utili ...

Issue with changing image source on hover using JQuery not functioning as expected

I've been attempting to modify the image src when hovering using JQuery. I'm currently working in Brackets, and when I preview it live in Chrome, everything seems fine. However, when I try to open the file directly in Chrome or IE, it doesn' ...

Adjust the parent div's background when hovering over it

Here is the code snippet I am working with: <div class="thumb_image_holder_orange"> <img src="http://dummyimage.com/114x64/000/fff.png" /> </div> I want to change the background of the div when hovering over the image, rather than j ...

Sass does not compile for optimization

My Sass compiler generated an overly large file for me, but it could be much smaller!! I checked the compiler settings and didn't find anything wrong! The issue might be with the compiler itself. It's also possible that there is a problem with my ...

Customize the width of dropdown menus in Bootstrap

Using a dropdown menu, I want to display options for candidates that can be chosen. Additionally, I would like an input field to filter these options; so that when text is entered, the dropdown menu adjusts accordingly. Specifically, I am looking to set th ...

conceal elements created with JQuery within an AJAX function

Is it possible to use jQuery and the $.ajax() method to submit a form, displaying only the result while hiding other elements on the page? I am looking to add a conditional in the Ajax method that will show the result if successful, hiding certain elements ...

The hover effect is implemented across all image elements

Below is the code snippet in question: #mg1 { margin-left: 3%; } #mg1:hover { margin-top: 4%; } <div id="section1"> <center> <div id="bgp"> <center> <p>THUMBNAILS</p> </center> ...

Tips for showcasing an image partially with primefaces/jsf

Looking to display just a single icon from a sprite image? Here is how you can do it using jsf/primefaces: If you tried the code below but ended up displaying the complete image, try adjusting the background-position values: <p:graphicImage value="/re ...

Adjust the image size to fit the page according to its height

Having a dilemma here. I have this square image (2048x2048) that I want to set as a background. The tricky part is, I want it to display borders when the page stretches into widescreen mode, but also resize while maintaining its square shape when the page ...

Customizing HTML Select Elements

Can the HTML Select attribute be customized to have a flatter appearance? Although I can set the border to be 1px solid, the dropdown part still appears 3D and unattractive. ...

Is there a way to horizontally navigate a pallet using Next and Prev buttons?

As someone new to development, I am looking for a way to scroll my question pallet up and down based on the question number when I click next and previous buttons. In my pallet div, there are over 200 questions which are dynamically generated. However, th ...

Font and div placement varies on a mac within a WordPress environment

www.oihanevalbuenaredondo.be After styling my post titles in CSS using font-family: adobe arabic, I noticed that while they display correctly on my Windows machine, they appear in a basic font on Mac OS, Safari specifically. Why is it that Safari cannot r ...

Struggling with the alignment of pictures inside a container

I utilized the Instafeed.js library to fetch the three most recent images from an Instagram account. These images are loaded into a specific div and I successfully customized their styling according to my requirements. However, the current setup is quite s ...

Unlocking the potential of the :not selector when combined with the :nth-of-type selector

My current project involves styling a table. I am looking to apply a specific background color to each odd row, excluding the first row which contains headers. I attempted the following code without success: .new-items-table tr:nth-of-type(odd):not(.new- ...

How can I create a responsive design for my div elements?

I am facing an issue with responsiveness in my div container. Inside the square-shaped frame, I have a h2 tag that does not adjust properly when viewed on different devices such as mobile and browsers. Despite setting up media queries to make it responsive ...