Validating SASS based on class name

Hi there, I am fairly new to SASS and do not consider myself a programming expert.

I have ten aside elements that each need different background colors depending on their class name.

Even after going through the SASS documentation, I am still unable to figure it out.

I would like to set up a condition where if an aside has a class name of x, then the background color should be x. If the aside has a class name of y, then the background color should be y, and so on.

Is there a more efficient and elegant way to achieve this?

Thank you all in advance, and I apologize if this is a straightforward question.

Answer №1

If you are working with colors that do not have traditional names (or if the class name does not directly correspond to a color, for example products = blue, addresses = red), using a list of lists is the way to go:

$colors:
    ( black #000
    , white #FFF
    , red #F00
    , green #0F0
    , blue #00F
    );

@each $i in $colors {
    aside.#{nth($i, 1)} {
        background: nth($i, 2);
    }
}

Output:

aside.black {
  background: black; }

aside.white {
  background: white; }

aside.red {
  background: red; }

aside.green {
  background: lime; }

aside.blue {
  background: blue; }

If you are using colors with standard keywords, you can try this approach:

$colors2: black, white, red, green, blue;

@each $i in $colors2 {
    aside.#{$i} { background: $i; }
}

Output (note that this seems to work best with --style debug, as using --style compress may generate errors):

aside.black {
  background: black; }

aside.white {
  background: white; }

aside.red {
  background: red; }

aside.green {
  background: green; }

aside.blue {
  background: blue; }

Answer №2

The decision really comes down to your preference for typing. One option is to create a mixin for the background and then include it within the CSS rule for aside, but do you really need to go to that extra effort?

If you do feel it's necessary...

@mixin background($color) {
    background: $color;
}

aside#foo {
    @include background(#fff);
}

Answer №3

The concept of changing the background color based on the class name in HTML can be achieved using CSS like this:

aside.x {background-color: x}
aside.y {background-color: y}

Have you considered using SASS for this purpose? Perhaps you want to make the design more dynamic by easily adding new classes without updating the CSS manually in the future. However, it's important to note that SASS compiles into static CSS and doesn't offer dynamic changes.

If you really need the functionality to dynamically apply background colors based on class names, using JavaScript (jQuery) may be a solution:

$('aside').each(function () {
    $(this).css('background-color', $(this).attr('class'));
});

Alternatively, utilizing loops in SASS could generate multiple classes with corresponding background colors if needed.

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

Designate the preferred location for requesting the desktop site

Is there a way to specify the location of the 'Request desktop site' option? Here is the code I am currently using: var detector = new MobileDetect(window.navigator.userAgent); if(detector.mobile() != null || detector.phone() != null || det ...

The CSS 'top' attribute is without impact

As I begin my journey with CSS, I am eager to grasp some of its behaviors. In the CSS file, the following code is defined: #spa { position : absolute; top : 8px; left : 8px; bottom : 8px; right : 8px; min-height : 500px; min-width : 500px ...

Trigger click functions sequentially to display elements after specific actions are taken

Is there a way to make jQuery listen for clicks only at specific times? It seems that when I add event listeners, like $("#box").click(function(){, they are executing before the code above them finishes running. Let's say I have two boxes that should ...

Every time I rotate my div, its position changes and it never goes back to

Trying to create an eye test by rotating the letter "E" when a directional button is clicked. However, facing an issue where the "E" changes position after the initial click instead of staying in place. Here is a GIF showcasing the problem: https://i.stac ...

"Utilizing a flexible grid system in Bootstrap to create 2 or

I am looking to create a layout with a variable number of columns using row span. Specifically, I want 3 columns on XS, 2 columns on SM (with the first 2 columns stacked on top of each other), and 3 columns on LG. I am unsure how to make the row span respo ...

Arrangement of Div Box

Here is the HTML code that I am currently working with: <div class="item"> <div class="pageheader"> Header </div> <div class="info"> Created on... </div> <div class="image"> I ...

The height of the <div> element is not increasing along with the content inside. If I set the height to "auto

I encountered an issue with my webpage design. It has set dimensions with blue borders on the sides and bottom. The problem arises when I add Facebook comments to the page - as more comments are added, they extend beyond the bottom of the webpage, below ...

What's preventing me from using the left click function on my published blog post?

This is my first time creating a blogger template and everything seems to be working correctly. However, I have encountered one problem. For some reason, I am unable to left click on my blog post. I have not installed any right/left click disabler and I&a ...

Troublesome situation arising from CSS floats overlapping div elements

It's strange how this issue always occurs when using floats. This is what my div (samle) looks like: <div class="main> <div class="inn_div">&nbsp</div> </div> Here is my stylesheet: .main{ width:250px; border:1px ...

What is the process by which photoswipe applies styles to blur an image upon clicking the share button?

If you want to see an example, check out this link: http://codepen.io/dimsemenov/pen/gbadPv By clicking on the Share button, you'll notice that it blurs the image (and everything else). Despite inspecting it closely, I still can't seem to figu ...

Encircle a particular row in a table with a border

I'm having trouble creating a border around only the top row of my table. Right now, the border is only displayed around the outside of the entire table. I also want to add a border specifically to the first row that contains 'Team, Correct Picks ...

When hovering over it, an element within an li tag appears larger than its parent element

I'm currently working on a project for my school classes and running into an issue with CSS. I'm trying to enclose everything in boxes, but it's proving to be quite challenging. The main problem I'm facing is that when I hover over the ...

Chrome: Box-shadow not visible on images with a background present

Recently, I have noticed an issue with the box-shadow not displaying on images with a background on my website. This problem started occurring in Chrome a few months ago, despite working perfectly fine around six months ago. To pinpoint the problem, I cre ...

A white background emerges when I select md-dropdown

Lately, I've been experiencing an issue on my website where a white background pops up whenever I click on a dropdown (md-select) in my form. Initially, it only happened with forms inside a modal, but now it's affecting dropdowns across the entir ...

Adaptable Collection of 4 Images

I am facing a challenge in replicating eBay's 'Today' featured seller layout with 4 square images creating one box (refer to the image). I am using Bootstrap for this task, and I am finding it difficult to comprehend how to achieve this. I h ...

What is the best way to use JavaScript to replicate the bootstrap WCAG algorithm for selecting dynamic color schemes?

After transitioning to Bootstrap 5, I observed a unique feature where an algorithm determines the text color for buttons when they are hovered or active. For instance: For a primary color of black, hovering over the button changes the background to black ...

Is there a way to shift this div to the bottom and incorporate some quirky pop-up squares?

I am struggling to figure out how to move this div to the bottom. Additionally, I want to create temporary pop-up squares (simple divs) in random directions that will disappear after 50ms. My attempt to float this box to the bottom did not yield the desir ...

Incorporate the Bootstrap classes to arrange the divs side by side, ensuring that any surplus space is utilized effectively

Having implemented a layout with two images and two paragraphs side by side, everything seemed to be working fine until I encountered an issue when setting both images' height to 400px. The goal was for the paragraph to adjust its size based on conten ...

What is the method for customizing the hover color in a mantine.ui menu?

I've been attempting to modify the menu color when hovering over it, but unfortunately, it's not working. Could anyone provide guidance on how to change the hover color in mantine.ui menu? ...

Creating a custom HTML layout: A step-by-step guide

Struggling to create a unique layout in HTML and seeking assistance. Instead of words, I'll show you my current layout with images: While this works fine for now, if div1 becomes taller, the layout will look like this: My ultimate goal is to achiev ...