3

I'm doing a profile for this website, and I wanted to do a cover photo like element like facebook has. The problem I have ran into is, first how do I save the background image of the cover? Like, I can save the url of the image in the database, but how would I possibly be able to put it in css?

I do not really want to just put:

<div style="background-image:url(<?php echo $imageUrl?>) no-repeat center center fixed">
</div>

That looks really ugly markup. Is there some other way?

6
  • Save it under a predetermined name Commented Jan 2, 2014 at 6:11
  • As ugly as it is, it's the only realistic solution IMHO Commented Jan 2, 2014 at 6:12
  • That looks really ugly markup. - behind some swish web sites there's a whole pile of ugly. We use the tools we have. Commented Jan 2, 2014 at 6:12
  • I know we use the tools we have, and being a programmer, I have learned there is always the fugly way to do things, then then the right method to do things. I just can't figure out the right way for this one. Commented Jan 2, 2014 at 6:14
  • I would try to create a custom.css file with php for each user. It would need more http requests. Commented Jan 2, 2014 at 6:18

1 Answer 1

2

The only other options would be to put it in a style tag, or have php generate an external CSS file.

For the tag you'd do

<style>
  div { background-image: url(<?php echo $imageURL;?>); }
</style>

That is a bit cleaner than inline HTML styling.

Sign up to request clarification or add additional context in comments.

2 Comments

This is indeed much cleaner, though, wouldn't it be heavier to load like this? I mean bringing css from a stylesheet and then rendering css from a <style></style> tag? I'm all for efficiency.
I wouldn't suggest having an external CSS file, though I did mention that possibility. Doing this method, an inline style tag, shouldn't be any less performant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.