r/css • • 6d ago

Help CSS background from image asset

Post image

I feel like my issue is a Programiz problem, but I thought I could get more visibility by cross posting.

0 Upvotes

6 comments sorted by

•

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.

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:

  1. add this to your HTML: <img id="bkg" src="page1bg.jpeg" style="display: none">
  2. add this to your CSS: body { background-image: var(--bkg); }
  3. 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

2

u/azangru 6d ago

Have you tried this using a plain html, css and image file in a local directory on your computer rather than in an online editor?

1

u/xxcrucialxx 6d ago

What exactly u trying to do?

1

u/Evgenii_Korolev 6d 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.