Ahh.... After a long hours of debugging I have finally found the issue. It was my foolishness because of which I struggled. I wanted to delete this question but thought that it would help new comers like me when the stuck to similar issues.
Problem was here
loader: 'url-loader?limit=10000'
I was setting the limit to file size of 10kb without really knowing what it does. And the image which I was loading was of size 21kb ! This happens when you copy some else webpack config files :) Sign of javascript fatigue !
Answer from WitVault on Stack OverflowVideos
Ahh.... After a long hours of debugging I have finally found the issue. It was my foolishness because of which I struggled. I wanted to delete this question but thought that it would help new comers like me when the stuck to similar issues.
Problem was here
loader: 'url-loader?limit=10000'
I was setting the limit to file size of 10kb without really knowing what it does. And the image which I was loading was of size 21kb ! This happens when you copy some else webpack config files :) Sign of javascript fatigue !
When using webpack to load your css and the assets they access, your css needs to follow the same module naming rules you use in your JavaScript import or require calls.
Assuming the img folder is in the root of your source tree, you should refer to it like this in scss:
// note there is no leading /, which tells "require()" to start from the root of your source tree
$bg-img: url('img/bg.jpg');
Or just go completely relative. Assuming you your source code is like this:
/src/styles.scss
/img/bg.jpg
Your styles.scss could use a relative path:
// note the path is relative to where the style.scss is in your source tree.
$bg-img: url('../img/bg.jpg');
depending on how your packs are structured/applied you might be able to use a loop to generate a bunch of generic styles. See the documentation here: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#id35
Do you really need 3 separate components to get your image url? wouldn't: $img and then setting that to /folder/img.ext be far easier?
Also, you don't need the #{} for repeat by the way.
I hope this is what you're after… the question is not very specific in terms of what you need the outcome to actually be.
Cheers, Jannis
Update:
Okay, I see you've updated your question (thanks for that). I believe this could be a little better for general use:
@mixin background($imgpath,$position:0 0,$repeat: no-repeat) {
background: {
image: url($imgpath);
position: $position;
repeat: $repeat;
}
}
.testing {
@include background('/my/img/path.png');
}
This will then output:
.testing {
background-image: url("/my/img/path.png");
background-position: 0 0;
background-repeat: no-repeat;
}
Or you can use the shorthand version:
@mixin backgroundShorthand($imgpath,$position:0 0,$repeat: no-repeat) {
background: transparent url(#{$imgpath}) $repeat $position;
}
.testing2 {
@include backgroundShorthand('/my/img/path.png');
}
Which will generate:
.testing2 {
background: transparent url(/my/img/path.png) no-repeat 0 0;
}
Lastly if you want to specify your base path to your image directory separately you can do the following:
$imagedir:'/static/images/'; // define the base path before the mixin
@mixin backgroundShorthandWithExternalVar($filename,$position:0 0,$repeat: no-repeat) {
background: transparent url(#{$imagedir}#{$filename}) $repeat $position;
}
.testing3 {
@include backgroundShorthandWithExternalVar('filename.png');
}
This will then generate:
.testing3 {
background: transparent url(/static/images/filename.png) no-repeat 0 0;
}
Is this what you needed?
If not feel free to update the question or reply/comment.
some other sample:
path to the image:
$path--rel : "../images";
color
$black: #000000;
creating the mixin:
@mixin background-image($img, $background-position, $background-color) {
background-image: url('#{$path--rel}/#{$img}');
background-repeat: no-repeat;
background-position: $background-position;
background-color: $background-color ;
}
using the mixing:
.navbar-inverse {
@include background-image("header.png", right, $black);
}
result:
.navbar-inverse {
background-image: url("../images/header.png");
background-repeat: no-repeat;
background-position: right;
background-color: #000000;
}
In a perfect world your URL paths should be absolute, so always starting with / refering to the base path of the domain.
You have:
background-image: url('./../img/coffee.png') no-repeat;
The shape you should be looking for is:
background-image: url('/folder/img/coffee.png') no-repeat;
^ This lead slash references the base path of the domain.
So, the above example is domain.com/folder/img/coffee.png - you see the leading / is the base the whole URL is based from.
The advantage of this is your SCSS or CSS or image files can be placed in any location in your domain structure.
Absolute Pathing is the key to success!
Example 2:
background-image: url('/images/tea.png') no-repeat;
Image is located at:
mydomain.org/images/tea.png
The location of the SCSS or CSS file is not important, as long as it's on the same domain.
Solution
If other alternatives don't work, you can use ~ to reference the project directory and access public folder from there.
background-image: url("~/public/images/icon.png");
OR the other alternative would be to move images to the src directory, and this won't be a problem since webpack would pack them in a folder called static/media as static files
Hello there,
Not sure if this is a Gatsby related issue or a SASS issue. But whenever I'm trying to set the background image of and element (div) with the background-image: url() css property in SASS I get the following error:
./src/styles/main.scss (./node_modules/css-loader??ref--14-oneOf-1-1!./node_modules/postcss-loader/src??postcss-3!./node_modules/sass-loader/dist/cjs.js??ref--14-oneOf-1-3!./src/styles/main.scss)Module not found: Error: Can't resolve '../../../images/home/blog-bg.png' in '/Users/aeum3893/Documents/work/folder/tech/src/styles'
It can't find the image with the provided path and I have tried different paths like:
- Full path with quotes & without quotes:
background-image: url('/Users/aeum3893/Documents/work/folder/tech/src/images/home/blog-bg.png');
- Relative Path with quotes & without quotes:
background-image: url('src/images/home/blog-bg.png');
- Funky stuff with quotes and without:
background-image: url(../../../images/home/blog-bg.png);
background-image: url(/../../../images/home/blog-bg.png);background-image: url(blog-bg.png);
And so on... It breaks. I don't know what I'm missing here.
Folder structure:
- src
-- components
-- images // Images that I want to use
-- pages // Pages
-- shared // Reusable components
-- styles // SASS folder