Assemble an assortment (array) in SASS to be utilized in an @for loop

I am currently delving into SASS and attempting to send a collection of data (an array) to a @mixin for processing. However, I am struggling with defining the appropriate Data Structure to input the values into the @mixin

Here is some example pseudo code:

@mixin roundcorners($collection) {

    $collectionLength = length(collection);

    @for($i from 0 to $collectionLength) {
        border-#{$collection[$i]}-radius: 9px;
    }

}

.mybox {

    @include roundcorners(['top-right','bottom-left']);

}

The expected result should be:

.mybox {
    border-top-right-radius: 9px;
    border-bottom-left-radius: 9px;
}

Answer №1

If you're working with SASS and looking for something similar to an array, consider using a list instead. You can iterate through the list using the @each directive, as shown below:

@mixin roundcorners($collection: (top-left, top-right, bottom-right, bottom-left), $radius: 0)
   @each $corner in $collection
     border-#{$corner}-radius: $radius

For more information on using the @each directive in SASS, check out this link.

In this example, I've utilized string interpolation to insert the list value into the rule itself. Although I'm not certain if this is permitted, it's worth checking when you have access to your development environment.

I've also incorporated default values in the arguments, allowing you to specify a custom radius. Keep in mind that passing in any corner from the list will override the entire default list, so be mindful of this behavior.

An alternative and more straightforward approach could be:

@mixin rounded($topLeft:false, $topRight:false, $bottomRight:false, $bottomRight:false)
  @if $topLeft != false
     border-top-left-radius: $topLeft
  @if $topRight != false
     border-top-right-radius: $topRight
  @if $bottomRight != false
     border-bottom-right-radius: $bottomRight
  @if $topLeft != false
     border-bottom-left-radius: $topLeft

By setting defaults in this mixin, you can easily call it like this:

@include rounded(false, 9px, false, 9px)

Using 'false' instead of 0 as the default value ensures that unnecessary radius rules are not generated. It also provides flexibility to reset corners to a 0 radius when needed.

Answer №2

This is my solution that allows for setting different radius values.

@mixin border-radius($radius:5px){
  @if length($radius) != 1 {
      $i:1;
      //handles older Mozilla browsers
      @each $position in (topleft, topright, bottomright, bottomright) {
          -moz-border-radius-#{$position}:nth($radius, $i);
          $i:$i+1;
      }
      //Handles webkit browsers
      -webkit-border-radius:nth($radius, 1) nth($radius, 2) nth($radius, 3) nth($radius, 4);
      //Standard CSS3
      border-radius: nth($radius, 1) nth($radius, 2) nth($radius, 3) nth($radius, 4);
  } @else {
      -webkit-border-radius: $radius;
      -moz-border-radius: $radius;
      border-radius: $radius;
  }
}

This means you can set all the radii the same:

@include border-radius(5px)

or provide different values like this:

@include border-radius((5px, 0, 5px, 0))

I hope this helps keep your CSS concise and organized :)

Answer №3

After receiving guidance from @Beejamin, I successfully implemented the code provided and resolved syntax errors to create a working solution.

@mixin roundcorners($collection: (top-left, top-right, bottom-right, bottom-left), $radius: 0) {
    @each $corner in $collection {
        border-#{$corner}-radius: $radius
    }
}

@include roundcorners((top-right, bottom-left), 9px);

Although his final solution is preferred as it allows for individual radii assignment to each corner.

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

Having trouble loading certain attributes with Django and TailwindCSS?

Experiencing some challenges while working with Django and TailwindCSS attributes. Here's an example using a table: <div class="relative overflow-x-auto shadow-md sm:rounded-lg"> <table class="w-full text-lg text-left text- ...

pointing out the existing issues with the menu

I am struggling to figure out why I cannot highlight the current menu page on a navigation bar (aside from the fact that I am a complete beginner :-) I have tried using a JQuery function that I found online, but none of them seem to work. Here is the funct ...

Do overlay elements have to be positioned at the bottom of the HTML body every time?

As I delve into the world of JavaScript frameworks, one common trend I've noticed is that elements like dialogs, tooltips, and alerts usually appear at the end of the body tag. With my own implementation of these UI elements, I am determined to make ...

Preventing window resize from affecting print screen content in Angular 8

I'm struggling with a print button feature in my Angular 8 project. When the window is resized, the content within the print window also resizes, which is not the behavior I want. I've tried a couple of solutions, but nothing has worked so far: 1 ...

What attribute should I utilize to ensure the image always takes priority over text when using @media queries?

resolution My goal is to have the image appear on top of the text when resizing the window, but maintain the appearance shown in the attached image when using a larger resolution. Below is the CSS code I am currently using: .section4 { text-align: cen ...

In Swift, what is the best way to eliminate the backslash "" escape character from a string array?

I'm currently grappling with the challenge of eliminating escape characters in an array of strings that contain apostrophes. Swift automatically includes escapes in strings with apostrophes when they're added to an array. For instance, let' ...

Utilizing Bootstrap checkboxes with the power of jQuery

I am currently facing an issue with the behavior of bootstrap checkboxes compared to regular ones. After researching on various online forums, it appears that bootstrap checkboxes return a value of undefined which is causing my if checked statement to not ...

Unconventional border effect while hovering over image within navigation container of Bootstrap

Whenever I hover over the image inside the navigation bar, there is a strange white/gray border that appears. It's quite annoying. Does anyone know how to remove this border? Here is the code snippet causing the issue: <ul class ...

Best practices for managing modifications to objects within an array using React Redux

In my React Redux app, I fetch data from the server and display it. The data is shown in the parent container using code like this: render(){ var dataList = this.props.data.map( (data)=> <CustomComponent key={data.id}> data.name </CustomCo ...

Unusual SASS results observed while utilizing extends functionality

Having issues with my SASS files: _android.scss: $width: 111px; @import 'child'; .android .child { @extend %child; } _ios.scss: $width: 999px; @import 'child'; .ios .child { @extend %child; } _child.scss: %child { wi ...

Retrieve a collection of dates that fall within a specified start and end date and store them in an

I'm having trouble adding the result to an array. Here is the code I am using: $listData2=""; $end = date("n/d/Y",strtotime($row['dDate'])); $start = date("n/d/Y",strtotime($row['aDate'])); $datediff = strtotime($end) - strtotime ...

Extract a free hour from the JavaScript time array without any filtering

In my Javascript array, I have the teaching schedule of a teacher for the day. { "time": [ { "start":"10:00","end":"11:00" }, { "start":"12:00","end":"01:00" }, { "start":"04:00","end":"06:00" } ] } My goal is to determine the free hours in the abo ...

The CSS animation is functioning smoothly in Chrome, but unfortunately, it is not working in Firefox

I'm having trouble with my animation not working in Firefox and Safari, even though it works fine in Chrome. I've tried using the -Moz- and -WebKit- vendor prefixes, but it seems that Firefox and Safari already support the animation. CSS: .vi ...

Converting a 3D image array in NumPy to a 2D array

I am working with a 3D-numpy array representing a grayscale image. Here is an example of how it looks: [[[120,120,120],[67,67,67]]...] Since it is a gray image, having every R, G, and B value the same is redundant. I am looking to create a new 2D array t ...

Replace FormControl.disabled by using a CSS or HTML attribute

I have a customized angular date picker where I want to restrict user input by disabling the input field and only allowing selection from the date picker. However, using the "disabled" attribute on the input element is not an option due to conflicts with t ...

Issue persists with Node Sass in current environment despite multiple attempts to resolve through rebuilds and uninstalls, specifically with version 4.12 on a 64

Currently, I am working with the most recent versions of Node (12.8) and Node-Sass package (4.12.0). You can find detailed documentation on these versions here: (https://github.com/sass/node-sass/releases). Despite my extensive research, whenever I try to ...

Trouble with Two Divs Layout

My website layout includes links on the left side of the page within a floating div, with content on the right side in another floating div. However, I'm facing an issue where the content moves below the links when the page is resized. I attempted to ...

Are Nodes Distributed and Styled in Polymer's Network?

Let's imagine I have created two unique custom elements as extensions of the innovative Polymer Element: c-el1 c-el2 Here is how they are templated: c-el1 <dom-module id="c-el1"> <template> <style> p{ ...

Getting a surprise image from the collection

I am experiencing an issue with retrieving images from an array. I have an array containing 6 variables that represent the paths to the images. However, when I use console.log at the end, it returns a random variable like sk11, sk15, etc., which do not c ...

What is the best way to merge two images together in order to achieve consistent vertical alignment on all web browsers?

Struggling with vertically centering thumbnails on my e-commerce site due to varying image sizes. Tried CSS options like display-table and line-height, but IE compatibility is an issue. Is it common practice for large sites to resize images and overlay the ...