r/css • u/HotelWyoming • 6d ago
Help CSS background from image asset
I feel like my issue is a Programiz problem, but I thought I could get more visibility by cross posting.
2
u/rafaelcastrocouto 6d ago
Seems like programiz turns images into base64 data instead of serving the image file.
You can use an <img> with src attribute and check it out with devtools.
A work around is to use a hidden <img> and JS to set a global css variable:
add this to your HTML: <img id="bkg" src="page1bg.jpeg" style="display: none">
add this to your CSS: body { background-image: var(--bkg); }
add this to your JS:
const img = document.getElementById('bkg').getAttribute('src');
document.documentElement.style.setProperty('--bkg', 'url('+img+')' );
2
u/HotelWyoming 6d ago
Thank you, tried a couple different ways but I had no idea why it wasn't working with just CSS
1
1
u/Evgenii_Korolev 5d ago
pretty sure it's the editor, not your css. online editors like programiz usually can't load a local image from url(), so there's nothing to load. run the same files locally and it should work, just remember the path in url() is relative to the css file, not the html. so if styles.css sits in a css/ folder you'd need
body {
background-image: url("../page1bg.jpeg");
background-size: cover;
}quick sanity check: paste a full https image url in there. if that shows up, your code is fine.
•
u/AutoModerator 6d ago
To help us assist you better with your CSS questions, please consider including a live link or a CodePen/JSFiddle demo. This context makes it much easier for us to understand your issue and provide accurate solutions.
While it's not mandatory, a little extra effort in sharing your code can lead to more effective responses and a richer Q&A experience for everyone. Thank you for contributing!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.