Adjusting the value of a variable in an Scss selector will result in a change, regardless of the presence of an element matching the selector

CSS File

$dark-mode: false!default;
$primary-dark: blue;

[dark-mode='dark'] {
  background: gray!important;
  $dark-mode: true!global;
}

[dark-mode='light'] {
  background: yellow!important;
  $dark-mode: false!global;
}

@if $dark-mode==true{
  $primary-dark: black!global;
} @else{
  $primary-dark: blue!global;
}

HTML

<html>
<body dark-mode="dark">
</body>
</html>

The code provided above always sets the $dark-mode value as specified in the [dark-mode='light'] block while the background is set correctly.

Answer №1

It appears there may be a misunderstanding about how SASS operates. SASS variables are utilized solely during the compilation process, meaning they cease to exist once the code is compiled.

Both selectors [dark-mode='light'] and [dark-mode='dark'] are compiled regardless of whether a corresponding element with the value dark or light exists in the DOM. As the light properties are set last in the code, $dark-theme will always be evaluated as false in the @if statement, causing $primary-dark to consistently hold the value blue.

If you wish to maintain this approach, consider exploring CSS variables as an alternative. For more information, you can refer to this helpful article on the topic: Color Themes: CSS Custom Properties vs SASS Variables.

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

What is the process for selecting a radio button using Selenium WebDriver?

I am trying to select the radio button in this HTML code, but I'm having trouble. Here is the snippet of my HTML: <div class="appendContent"> <div> id="contentContainer" class="grid_list_template"> <div id="routeMonitor ...

When the child of li is clicked instead

Below is a list I have: <ul id="orderlist"> <li id="2"> <span class="pull-right value">Ready</span> <img src="" class="img-responsive"> Filet Mignon <small>2 servings</small> <small ...

A guide on manipulating an input field to trigger md-datepicker upon clicking

What is the best way to convert an input text into a fire md-datepicker and achieve a similar result like this? ...

What is the best way to extract text directly from a span element without including the text of its child nodes?

I am dealing with a piece of HTML that is dynamically generated, and it looks like this: <span> 10/10/2012 <div class="calendar"> lots of text elements here </div> </span> Is there a way to specifically retrieve the tex ...

What could be the reason for lxml not being able to locate this class?

Trying to extract text from a webpage using Python has always been tricky, especially with the surprises lxml tends to throw. Here's what I attempted: >>> import lxml.html >>> import urllib >>> response = urllib.urlopen(&a ...

What is the process for embedding images and text within a nested division element?

I have a website layout with 4 main sections: header, left side, right side, and footer. I would like to further divide the header into two sections - left header and right header. The right header should contain an image along with other options like home ...

Trouble aligning Material UI data-table columns (table head) to center in React

I have a data table where I declare rows and columns as simple variables, making it difficult to style them using 'style={{textAlign: center}}'. I tried adding a CSS file, but it did not work with the Material UI table. I am unsure of how to proc ...

Issue with displaying Font Awesome icons in Bootstrap 4 navbar brand

Recently, I've been setting up a local Laravel website to explore the latest features and changes in Laravel 5.6. However, I've encountered an issue while trying to integrate a Font Awesome icon into the navbar-brand of Bootstrap 4. The icon does ...

Adding hash history to a sortable showcase: A step-by-step guide

I am in the process of developing a filterable portfolio feature for my website. Once a user clicks on a filter, it adds a hashtag to the end of the URL. For example, clicking on 'design' would result in www.yourdomain.com/#design. I am wonderin ...

Limit image size in Outlook when using Mailchimp

I'm currently working on coding a Mailchimp template for a client and I've run into an issue with image dimensions. The problem arises when images exceed the width of the template (usually 600px), as they are displayed at their original size in ...

How can I use a CSS file in PHP to conceal the folder structure?

main.css $theme->assign('css_file',SITE_URL.'styles.php?theme='.THEME_ID); $theme->display(TEMPLATES_PATH.'header.tpl'); header.tpl <link rel="stylesheet" href="{$css_file}"> styles.php include TEMPLATES_PATH. ...

Positioning a single column of iframes in the center

I have 6 graphs that I need to arrange in a grid with 2 rows and 3 columns. Each row should have one left-aligned graph, one center-aligned graph, and one right-aligned graph. However, the center-aligned graphs are not displaying correctly. Can someone hel ...

Clickable list element with a button on top

Within my web application, there is a list displaying options for the user. Each 'li' element within this list is clickable, allowing the user to navigate to their selected option. Additionally, every 'li' element contains two buttons - ...

Bring to life in both directions

I want to incorporate Animista animations to make a button scale in from the left when hovering over a div block, and then scale out to the left when the mouse moves out of the div block. Check out my code snippet below: http://jsfiddle.net/30sfzy6n/3/. ...

What is the method to close the picker when using type="datetime-local"?

Currently, I am utilizing Vue with the "datetime-local" type for input. While I can successfully select the date and time, my goal is to have the picker close automatically after a selection has been made. I've experimented with adding an onchange ev ...

Ant Design: Incorporating margin and padding causes slight rendering glitches in the markup

https://i.sstatic.net/BWmFU.gif https://i.sstatic.net/ZaLH8.gif When I apply margin and padding to this class, it starts twitching like in the first GIF. Removing the margin and padding fixes the issue, but I need them for styling. What is causing this ...

Next.js fails to properly render external links when utilizing the Link component

It was quite surprising to discover that a basic Link component doesn't function as expected in Next.js when attempting to use it with an external URL and HTML Button tag within. Here is my attempt at solving this issue: Attempt 1: <Link href="h ...

Trouble with center alignment of div in CSS

I attempted to create a centered horizontal menu on my webpage but the 'div align=center' attribute is not functioning correctly. .nav ul { display: block; list-style-type: none; margin: 0; position: absolute; } .nav li { display: in ...

Display the website logo when users share on WhatsApp

My goal is to have my website icon appear in WhatsApp when I share it (similar to the example below). https://i.stack.imgur.com/KCWi8.jpg I initially tried using this code, but it didn't work: <link rel="shortcut icon" href="css/img/favicon/favi ...

What is the best way to prevent buttons from shifting when I enlarge the font size in HTML?

Is there a way to make buttons increase in size when hovered over without causing the other buttons to shift to the side? I've tried using the following code to achieve this but it's not working as expected: <style> button{transition-d ...