question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SASS @import and maintaining URLs for images etc

See original GitHub issue

Shouldn’t URLs be maintained when importing .scss files from other directories?

e.g.

File structure: css/

  • main.scss
  • dir/
    • dir.scss

main.scss:

@import './dir/dir.scss';

dir.scss:

body { background: url('../../arbitrary/bg.jpg'); }

Generated CSS (in css/)

body { background: url('../../arbitrary/bg.jpg'); }

Shouldn’t SASS see that the .scss file being imported is one directory above it and then move all directories up accordingly? So:

body { background: url('../arbritrary/bg.jpg'); }

CSS @import will ‘preserve’ proper URLs not because it changes them (I think), but because each @import is handled as a separate HTTP request to the CSS file being ‘imported’.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:12
  • Comments:25 (2 by maintainers)

github_iconTop GitHub Comments

46reactions
vectorjohncommented, Sep 26, 2017

The opposition to this seems misguided. import as implemented is wrong for relative paths. Everyone opposing this seems to be under the impression that any sass include file has to know its location relative to everywhere it gets included. This is completely broken. Compass URL Helpers aren’t the right solution, this should just work.

I can’t imagine how anybody is using url() in an included file. It breaks encapsulation. If my _cool-include.scss file depends on url(‘./foo.jpg’), where is it supposed to find foo.jpg? Anyone would reasonably expect it to be next to _cool-include.scss, exactly like a normal css import would act. File references should be relative to the css (or scss) file. Instead, it depends on what included _cool_include.scss, which is a pretty brittle way of doing things. Then if it’s included by thing1/root.scss AND thing2/root.scss, I guess whoopsie daisy?

41reactions
felixfbeckercommented, Oct 15, 2015

Please make this an option. I’m using NPM (same for Bower though) to install bootstrap-sass and font-awesome. I then @import them in my app.scss. Both have fonts referenced. I use gulp to build my app. If I run a url reworker after the sass compilation, I can’t get the original path because it’s now all in app.css. If I run it before the sass compilation, there are no references because the @imports have not been processed yet. There is no way for me to fix the paths if Sass doesn’t include an option for fixing relative urls, or at least providing some sort of hook.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass: @import
Sass extends CSS's @import rule with the ability to import Sass and CSS stylesheets, providing access to mixins, functions, and variables and combining ......
Read more >
Can I import an externally hosted file with sass? - Stack Overflow
Yes, you can import external css file using PostCSS Import URL Plugin. It will pull the external CSS into your ...
Read more >
2 Smartest Ways to Structure Sass - Webdesigner Depot
2 Smartest Ways to Structure Sass · Divide the stylesheets into separate files by using Partials · Import the partials into the master...
Read more >
Sass · Bootstrap v5.2
Utilize our source Sass files to take advantage of variables, maps, mixins, and functions to help you build faster and customize your project....
Read more >
8 Tips to Help You Get the Best out of Sass - SitePoint
Partial files are created using an underscore and are not output as separate CSS files. Each partial should be imported using a master...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found