Utilizing a wildcard as a variable

Is there a more efficient way to consolidate all twenty rules into one using wildcards:

.margin-bottom-1rem {
  margin-bottom: 1rem;
}

.margin-bottom-2rem {
  margin-bottom: 2rem;
}

...

.margin-bottom-20rem {
  margin-bottom: 20rem;
}

I have searched for ways to capture all the class names, but haven't found any information on utilizing part of the captured name as a value. If wildcards cannot achieve this, is there an alternative method to avoid repeating these duplicate rules?

Answer №1

CSS Styles Using SCSS and SASS

$sizes: 1rem, 2rem, 5rem, 10rem, 20rem;

@each $size in $sizes {
  .margin-bottom-#{$size} {
    margin-bottom: $size;
  }
}

Generate CSS Classes with SCSS and SASS

$sizes: 1rem, 2rem, 5rem, 10rem, 20rem

@each $size in $sizes
  .margin-bottom-#{$size}
    margin-bottom: $size

Both methods result in the following CSS output:

.margin-bottom-1rem {
  margin-bottom: 1rem;
}

.margin-bottom-2rem {
  margin-bottom: 2rem;
}

.margin-bottom-5rem {
  margin-bottom: 5rem;
}

.margin-bottom-10rem {
  margin-bottom: 10rem;
}

.margin-bottom-20rem {
  margin-bottom: 20rem;
}

Apply to All Elements in a List

https://sass-lang.com/documentation/values/lists#do-something-for-every-element

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 collapsible menu located beneath my navigation bar is unable to reach the correct position on the right side

I'm attempting to create a collapsible navigation bar with its contents shifting to the right side when resized to medium size, but I'm having trouble. Can someone please assist me with this? I have also included the code below. <nav class=&qu ...

Is it possible to adjust the background color based on the current time of day?

I have successfully designed a visually appealing website using HTML5, CSS, and Bootstrap. I am interested in finding a way to automatically change the background color and navigation bar selection color based on the time of day - blue during the day and d ...

Printing a React component using a button causes formatting related to className to be lost, while printing it inline preserves the formatting

I've hit a roadblock in trying to figure out the React and printing issue for the past week and a half, and it feels like I'm going in circles. Despite finding some helpful solutions on Stack Overflow, none of them seem to work completely for me ...

Looking for a list that you can edit with CSS styling?

I'm struggling to create a flexible list that can be easily modified. My goal is to have an ordered list with unique labels such as 1,2,3,4,5,6,7,8,9,0,A,B,!,# Although I've managed to number the list items, I am stuck on adding the extra label ...

Achieving uniform space between divs using flex

This is the layout setup I am aiming for using display:flex. My goal is to have text1 displayed at the top, text3 at the bottom, and text2 in the middle. https://i.sstatic.net/xaeaB.png However, the actual outcome looks like this: https://i.sstatic.net/ ...

After the automation is finished, I am interested in outputting the IMDB rating of a movie or series to the terminal

I prefer using Google search to find the element because I find it easier to navigate compared to IMDB. import selenium.webdriver as webdriver print("This script is designed to retrieve the IMDb rating of a movie or TV series!!!") def get_results(search_ ...

Creating various rows within a loop can be achieved by using conditional statements to determine

I am faced with a challenge of handling an array of images, among other properties, and I aim to achieve the following outcome: https://i.sstatic.net/BYajY.png As the images reach the end of the container (which fits 4 images on desktops and adjusts for ...

The rows-per-page menu option in Vuetify suddenly vanishes

I'm currently working on incorporating a v-data-table inside a v-card, you can view the code in this CodePen snippet: https://codepen.io/benwasin97/pen/eYveZGL <v-data-table :headers="headers" :items="items" ...

An issue with the animation script in Ionic

I'm having trouble converting this to an Ionic application. Here's my code sample. Can anyone help me with putting this code correctly in Ionic? I'm trying to achieve something like this example <script src="https://cdnjs.cloudflare.com ...

What is the purpose of using overflow:hidden; in our code?

I am curious about the rationale behind using these three properties on this menu. I don't quite understand why they are necessary. I am eager to delve into the technical reasoning behind this. margin: 0; padding: 0; overflow: hidden; Snippet of HTM ...

Calculate the sum of hours worked in a day using pure JavaScript, no external libraries required

Hello, I'm new to this website and please excuse me if my English is not perfect, I am trying my best to communicate :) I have been working on a page that calculates the total hours worked in a day and this is what I have achieved so far until 15/06 ...

Browse through content without displaying the page title on the navigation bar and without the use of frames

When I sign into certain websites, I have noticed that the address displayed is often something like this: https://examplesite.com/access Even though all the links on the landing page are clickable and functional, their corresponding addresses never show ...

Troubleshooting: EJ Syncfusion React Scheduler Issues

I have recently implemented a syncfusion calendar for managing appointments in my ReactJs project, following the code examples from this link . After editing the JS and CSS codes (available at ), I encountered an issue with the CSS not displaying correctl ...

Move buttons from one group to another group

I have a task to update a group of buttons with different buttons depending on the initial button chosen. Button1: when clicked, both buttons will be replaced by Button1a and Button1b Button2: when clicked, both buttons will be replaced by Button2a ...

Ruby on Rails slider bar functionality

Currently, I am developing a video-focused application using Ruby 1.9.2 and Rails 3.1. One of the key features I need to implement is a slider bar that allows users to adjust the total duration of a video in real-time. Despite my attempts to use HTML5 fo ...

Ways to include scrolling specifically for one template

As a newcomer to frontend development, I have recently started learning Vue and the Quasar Framework. I am currently working on a table and trying to set a fixed table name with only the table items scrollable. <template> <q-table virt ...

Attempting to retrieve the list of 'Style' for a GtkWidget

Seeking assistance in obtaining a list of style properties for a GtkWidget (GtkButton). Here is the current code I have: #include <gtk/gtk.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char *argv ...

Positioning a div absolutely while still keeping the integrity of the page layout

I'm in the process of setting up a blog page on my website with a design that includes two columns. The first column, known as .col_1, is fixed in width and is showcased in red. The second column, represented by .col_2, occupies the remaining space an ...

Resolving problems related to window scrolling in javascript for implementing a "sliding door" style animation triggered by a scroll event

I am new to web development and I have been working on creating a portfolio website to learn. I have seen some websites with really cool effects on their landing pages and I would like to incorporate something similar into my site. Specifically, I am inte ...

Overriding Styles Set by an External CSS file

Let's assume that I have two different style sheets set up on my webpage: site.css: .modal { position: absolute; width: 200px; ... } ... reset.css: .reset * { position: static; width: auto; ... } Unfortunately, I don ...