Welcome Guest, Not a member yet? Register   Sign In
CSS background images
#1

[eluser]Unknown[/eluser]
Hi,

Question for anyone who knows the answer:

If I specify a url for the background attribute in an external CSS file, what should my url look like? Is there a way I could call base_url() within the CSS file?

My folder structure has index.php and the folder public on the same level. public/css holds my css file which refers to an image in public/images.

Thanks.
#2

[eluser]sophistry[/eluser]
welcome to CI-land, samchoi... let's hope you have a long a prosperous stay here with us.

IMO here's the easiest: url("../images/myimage.jpg")

yes, you can call base_url() if you want the full path, but relative path should work in a basic set up.

you could generate a CSS file using a view file and then write it to disk, but it would likely be overkill if you can just use relative paths. or you could generate inline CSS directly in a view and use base_url() there. or you could write your url() CSS directly into the tag attribute "style" (they don't call it cascading style sheets for nothing!).

cheers.
#3

[eluser]Colin Williams[/eluser]
When a CSS file requests a background image file, the request is relative to the location of the CSS file, not the page you are viewing. So, there really isn't much need or benefit to using dynamic paths in the CSS file.
#4

[eluser]Aken[/eluser]
And you definitely can't use PHP, and subsequently the base_url() function, inside a .css file.
#5

[eluser]Colin Williams[/eluser]
You absolutely can, Aken. But PHP must be preprocessing the request (and there are a number of ways to make this happen).
#6

[eluser]Aken[/eluser]
Well duh, with a bunch of preprocessing. But a standard .css file is basically a text file. It's not designed to be dynamic. You can't take an existing one and slap some PHP tags in it and expect it to work. That's the point I'm making, to avoid any confusion.
#7

[eluser]Colin Williams[/eluser]
Just wanted to clarify to samchoi that it is possible
#8

[eluser]Flemming[/eluser]
what about doing:
Code:
background-image:url(/public/images/yourimage.gif);
?
#9

[eluser]manilodisan[/eluser]
Example of loading css from PHP:

styles.php
Code:
<?php
    header("Content-type: text/css; charset: UTF-8");
?>

body {background-image:<?=base_url ()?>/images/my_bg.jpg}

Linking the php file:

Code:
<link rel="stylesheet" href="<?=base_url () ?>/css/styles.php" type="text/css" />
#10

[eluser]Unknown[/eluser]
../images/img.jpg

works. I should have known.




Theme © iAndrew 2016 - Forum software by © MyBB