Is there a way to specifically import only variables and mixins from Sass stylesheets?

Using the Zurb Foundation 4 (S)CSS framework has presented a challenge with massively duplicated styles. Each file that I import 'foundation' into brings along all of Foundation's styles, resulting in redundant declarations for body, .row, .button, and other elements. This not only prolongs SCSS compile times but also clutters the web developer console in Chrome with repeated styles from Zurb.

To address this issue, I've introduced a globals.scss file containing overrideable variables used by Foundation. This file is essentially a copy-paste of foundation_and_overrides.scss and imported before any reference to Foundation mixins. By importing just the globals.scss file, duplication is eliminated in files that do not utilize Foundation mixins.

However, the problem persists in files that do make use of Foundation mixins. Is there a way to selectively import only the mixins from an SCSS file without pulling in the concrete Foundation styles?

Answer №1

Imports in coding can be a bit tricky - it's usually an all or nothing deal. Whatever is in the imported file is what you'll end up with. However, if you take a close look at the source code of Foundation, you'll find some variables that allow you to tweak things and prevent certain styles from being included (for example, in the buttons section, by setting $include-html-button-classes to false, you can turn off those styles). Keep in mind, though, this customization method is unique to Foundation and may not apply to other libraries.

When importing Foundation using @import "foundation", you're essentially bringing in this main file: https://github.com/zurb/foundation/blob/master/scss/foundation.scss. This file, in turn, imports other files. If you don't need everything that Foundation offers, you can choose to import only specific files that you require (e.g.,

@import 'foundation/components/side-nav'
for just the side-navigation component).

Answer №2

I encountered a similar situation where I needed to access a variable from another file without importing all the CSS.

With the `@use` keyword in newer versions of Sass, you can ensure that CSS is only emitted once.

However, there are some drawbacks to this approach:

  • Only "Dart Sass" currently supports compiling it (at least, at the time of writing).
  • `@use` rules need to be written before any other rules.
  • Replacing `@import` with `@use` requires adding a scope prefix, like so:
    @use '../my-module';
    
    body {
        background-color: my-module.$my-variable;
    }
    

Warning: The `@extends` keyword(s) cannot have the `my-module.` prefix, as extensions are not scoped at all (at least, at the time of writing).

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

Why isn't this code for hiding the animation and displaying it not functioning properly?

Why isn't this animation from display none to block working properly? The initial code appears as follows, and it functions correctly: $(".box_outer").stop().animate({top: '25px' , opacity: 1}, 100); When I add display: none; to the class ...

Creating PDFs using Puppeteer in the presence of `link` tags

On my website, students have the ability to create their own notes using a RichText editor where the content can be quite complex. I want to provide them with an option to download these notes as a PDF file. To achieve this, I am planning to use the "puppe ...

Ensuring consistent width for two divs using CSS

https://i.stack.imgur.com/3n8La.png I am currently working on creating a responsive CSS layout for two div elements. The first div will contain the summary, while the second div will hold the detailed description. My challenge is to make sure that the seco ...

Encountering a jQuery error while trying to utilize the $window.load

I have a code snippet that is functioning well when wrapped within a document ready event: jQuery(document).ready(function($) { $('tr[data-name="background_colour"] input.wp-color-picker').each(function() { //this section works fin ...

Changing the width of an SVG path from 0 to 100% using CSS

I have an SVG design of wires that I want to animate the width from "0 to 100%", but I'm having trouble achieving this effect. It's easy to do with a div element, but not with an SVG image. Below is my code snippet: svg path { animation: f ...

Unable to display image between two inline-table divs while using Bootstrap

I'm currently developing a website for trading in-game items, but that's not important right now. The code snippet I am working with is: <div class="trade"> <h6><strong>RaiZeN</strong> wants to trade: (5 minutes ...

I encountered an issue while iterating through Object HTMLDivElement and applying ChileNode style z-index, resulting in an undefined output in both Firefox and Chrome browsers

function adjustPosition(currentDiv) { try { for (var i = 0; i < document.getElementById("parentContainer").childNodes.length; i++) { document.getElementById("parentContainer").childNodes[i].style.zIndex = 0; ...

Split the output into two separate columns in a cshtml file using a foreach

Is there a way to modify my current code so that the output is displayed in two columns instead of one? Would using flex grid be the best approach, or is there another method I should consider? <div class="col-12 box-container"> <div class=" ...

Altering the appearance of an Angular component in real-time by applying various CSS style sheets

I'm currently working on implementing a dynamic style-sheet change for a single-page application using Angular. The concept is to offer users the ability to select from various themes through a dedicated menu. Although only two theme variants are show ...

What to do when CSS Overflow doesn't work as expected?

Are there any alternatives for browsers that don't support CSS overflow? I am working on a new layout that heavily relies on the overflow property, however it seems like android browsers do not handle it well and as a result, my layout is broken. I am ...

What is the best way to expand my navigation bar to fill the entire width of my div?

A big shoutout to @seva.rubbo for fixing the issue! Check out the updated code on JSFiddle I recently designed a layout with a fixed width div of width: 900px;. I centered this div, leaving blank spaces on either side. Now, I'm looking to add a navig ...

Assigning the Style property to an element using a string that includes HTML tags

My HTML string is populated with elements such as button, li, span, and more. I am looking to add specific styles to buttons based on their class name. For example, if button.btn { some styles } and button.btn-success { some other styles } O ...

How does the app.scss file detect that the ~foo alias corresponds to the foo directory located within the node_modules

My Laravel project has a app.scss file stored in resources/assets/sass/frontend/. The node_modules directory is located at the root of my project. Within the app.scss file, I initially had: @import '../../../../node_modules/daterangepicker/daterang ...

What is the best way to integrate a ttf custom font into my Rails application?

Despite following the instructions in this thread, I am unable to achieve the same results. main.css h1 { font-family: 'Shadows Into Light', serif; src: url('/assets/fonts/shadows.ttf') format("Shadows Into Light"); font-size: 4 ...

Using bootstrap's center-block class, you can easily center

I'm attempting to center the content of a form. <div class="login-container"> <div class="center-block"> <form action="/joinChart" method="get"> <input name="username" id="username" type="text"><br><br> ...

Make sure the div is always positioned above everything, including any built-in pop-up windows

When working with two forms, one for user input and the other as a pop-up window to display results, users sometimes close the pop-up window prematurely if they think there is a network issue causing a delay in data execution. To prevent this, I am consi ...

A specific div remains in place as the user scrolls

Imagine a div that is positioned 60px from the top of the screen. What if, as you scroll down, it stops when it reaches 10px from the top and remains there for the rest of your scrolling session? And then, when you scroll back up, it goes back to its ori ...

Using jQuery to change the `class` of the parent `div` based on the length of a `p`

I am attempting to adjust the height of a parent div based on the length of text in its child p.descr. This is achieved by adding a specific class that corresponds to different height values in CSS. Below is the jQuery code snippet: $(document).ready(fu ...

Sending data from one page to another using Ajax request

Rail 5.2 datatables Within my views/books/index.html.slim file, I am including a partial from another MVC like so: = render partial: 'authors/index', :locals => {:author => @book.author} Inside my views/authors/_index.html, the code lo ...

Using jQuery to store the name of a Div in a variable and subsequently invoking it in a function

Recently, I've been grappling with a task involving storing a div name in a variable for easier editing and incorporating it into standard actions like show/hide. My code functions perfectly without the variables, but introducing them causes the div ...