I'd like to create a div with a background image css:
#mydiv {
background-image:url('/public/images/foo.png');
background-repeat: repeat-x;
}
Now, I can't use routes in css, so as a result I have to use a relative path, or my app will break if installed at a non-root path.
Is there a way around this? Something like
background-image:url('#{publicFolder}/images/foo.png');
I did find this thread from a year ago that claims it's impossible. Is this still the case?
Edit: I know I can inline the css in the page, that's not really an acceptable solution, but rather a work around.
Since the play framework compiles all the files in /public, I used the following statement in the css to acces background images.
background: url("/assets/images/my-image-background.jpg");
It worked for me so far, not sure if it's a good practice though! Hope it can be of help.
Cheers.
The path always goes from your stylesheet. So why don't you use relative paths?
Example, your css is in /public/css/, so your path in your css file has to be ../images. That's it.
But maybe less.css has something similiar you're looking for.
The way around /is/ possible.
The problem is that CSS files are static, so Play! does not do anything on them - except serving them to the clients.
Just make your css files into views, write a generic controller that takes the filename as a parameter and they will be served as Play! Templates.
something in the lines of (beware: PSEUDOCODE!):
ROUTE:
get /css/{filename} Application.getCSS
CONTROLLER
class public void getCSS(filename)
{
renderTemplate(filename);
}
Then in your css you can use any groovy template feature you want.
The installation path is available as http.path in the conf
This will be very inefficient though, so you will have to add nginx or similar frontend to do some caching and set high expiration values for those files.
I'm not sure if it's worth doing it just to avoid relative paths!
I definitely think that the relative path is better suited generic deployment, and you are more likely to break things with this dynamic absolute approach.
A better overall deployment strategy
Anothe important point is that if you do like absolute URLs, there's no problem.
I actually do too.
But when I deploy my web applications (be them play!, django, pure wsgi, or whatever else) I always put a web frontend.
Usually nginx.
This means that your play framework will be an upstream server, and you can do everything you want with the URLs on your :80 port - and manage several services with subdomains or subfolders...
but when you access your play application on its own port(s) everything will be absolute.
Finally, if you were deploying Play! as a war I would have the feeling you have chosen the wrong framework or the wrong deployment pattern! But then your jboss or whatever other application webserver will do some magic on the subfolders...
take a look at conf/routes file.
in it you will find the following something like
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
which means in order to access resources in the public folder,
you would use /assets in your url.
so in your css you do
url(/assets/images/myimage.png)
and it will work.
Related
I am working on a local project at the minute, I am using some background CSS images the file route is
/Volumes/Macintosh HD/Users/William/Desktop/websites/webtest/images/plane.png
if i change the route to /webtest/images/plane.png will the person receiving see the images if I copy this whole project into a zipped up file with that route?
Sorry this is a NOOB question.
It also depends how your website is structured. Assuming that your project looks like this, the paths of your images should be relative to the folder:
website
webtest
images
plane.png
css
style.css
Your paths in style.css should look like:
.class {
background: url('../images/plane.png') 0 0 no-repeat;
}
No, the recieving person will probably have problems, I think.
It'll be better to use ./images/plane.png or only images/plane.png if the page is something like /Volumes/Macintosh HD/Users/William/Desktop/websites/webtest/index.php
because / will access the root of the current IP-address or domain. It also depends on the css files location if you're using one.
Hope it helps ;)
Following is an extract from Requirejs documentation :
Ideally RequireJS could load CSS files as dependencies. However, there are issues knowing when a CSS file has been loaded, particularly in Gecko/Firefox when the file is loaded from another domain. Some history can be found in this Dojo ticket.
Can anyone please explain what is happening in Gecko/Firefox??? it says they load files from another domain??what does it mean???
please help
thanks
I believe they are just referring to when sites use cdns to host static content. Using a different usually cookie less domain to host their css, images and js files. Not that Gecko/Firefox is doing something to all css, images and js files. I'm not sure why Gecko is different from other browsers during these conditions they describe however.
I'm considering using less in an upcoming project but have been trying to figure out the best way to work with it while in development.
Usually when developing a site I'll write my html and css then start testing it in the browser, see how it looks, refine, reload, and repeat the whole process until I'm happy with how everything looks.
A crucial part of the process is using the Inspect Element feature in the browser to identify the piece of CSS I need to change. Usually just by looking at the line number I know exactly where to go in my CSS file.
But if I use Less (or any other method of combining/compressing my CSS) it makes the line numbers useless. I know I could use Find to search for the section of code but line numbers are much faster.
This is especially true when working on a project that involves other developers or large CSS files.
I just wondered how others deal with this, or maybe there's a better process?
Minified CSS really should only be going out to the production version of your site. When you're performing tests/changes etc, this should all be done in some test or development version of your site in a secondary server area where you can have the line-numbers version of css available. The same would go for JavaScript. On the production viewable copy, you want it minified. In development, you don't.
In any event, you should always have 2 copies of your css. The first copy is the primary source copy that is your development copy. It has all of your properly formatted styles with line-breaks in it. The other is the very latest minified version of your css that went to production. This will allow you to switch between the 2 files rapidly in the event that you need to work something out, assuming your site uses some sort of templated delivery (layout pages, master pages, or whatever).
The minified version will only be useful in the final delivery. All other needs should use the master copy. It might not be a bad idea to put a configuration in server-side portion of your site that determines what style sheet to use. That way you can simply change a configuration setting and go into a "debug" mode.
I agree with Joel - that's how I handle it. A build script minifies the CSS (and JS) before each release is FTP'ed to production. I just have a switch in PHP like:
if ($config->prod()) {
// incldue the minfied css
} else {
// include all the original files
}
Personnaly, I use an ant build script to make a production version:
it "condense" multiple css files in one
then it minify them with YUI compressor
same for scripts
(page recomposition to point to the newly generated files)
this way you divide your http request for those files, and gain some bandwith from 30% to 70% i'd say. depends on gzip also.
in my case, the dev version have:
18 css weighting 178ko
reduced down to 1 css at 96ko in the production version
I personally use tools to minify and inject the CSS into the browser each time i save my Less file. So i see each change immediately. This way it's pretty clear what just happened. I don't need the referencing line numbers that much any more.
I recently started using source maps, to see the correct file and line numbers (of my less files) when inspecting CSS in the browser again. I think that is what you are looking for. I personally don't need this extra fanciness that much.
I am writing an ASP.NET server control that will live in its own assembly. It's a refactoring of something already in the app, which uses CSS background images.
I don't want to require the user of the assembly to have to install a separate images folder, so -- if it's possible -- I'd like to store the images as resources in the server control's assembly. But I'd still like to use CSS to set them as background images. Unless I'm mistaken, CSS is my only option for setting background images.
Is this possible? I've seen some markup in places that suggests it might be, notably this, this, and this, but nothing sufficiently specific to CSS background images that I could be confident it would work: most articles that deal with this assume you are loading either script or CSS files.
I found the answer! The answer is: YES, you can do it! And here's how: this and this.
I am reviewing a site and i see a lot of different conventions on reviewing how images are reference in CSS on a asp.net-mvc site.
including:
Full path:
.ddTitle span.arrow {
background: url('/content/images/dd_arrow.gif') no-repeat 0 0;
}
Relative location compared to where the css is located:
#cluetip-waitimage {
background-image: url(jQueryUI/images/ui-anim_basic_16x16.gif);
}
Relative with ".."
#cluetip-waitimage {
background-image: url(../../jQueryUI/images/ui-anim_basic_16x16.gif);
}
In asp.net-mvc, with routing, etc . .is one correct and the others wrong or is this just preference or convention? Assume that this site might sit on a shared environment with other sites.
Your first option is perfectly fine if your application is always going to be in the root folder for the website (or at least your images are all going to be in the root folder). If you might have a different path in different situations (like having a shared site on development or testing), then this doesn't work.
The second and third options are basically the same thing. Which one is used is completely dependent upon where the images are located in relation to the CSS file. I personally believe that the second looks cleaner, so I try to put any images referenced by my CSS files in a folder structure relative to where the CSS is located. However, some people prefer to keep all images in one place (even mixing content images with site "chrome" images) and as such may need to use relative pathing with ../ in order to accomplish this.
I generally do it like this ...
background-image: url('/jQuery/images/ui-anim_basic_16x16.gif');
The opening / denotes the root folder, so all of your paths can be relative to the root of the program instead of the folder the page is running from. This adds a little bit of typing, but it removes a lot of the problems of parent hashing.
So if your images were like this ...
Solution
Controllers
Content
JQuery
images
Your path would be background-image: url('/content/jquery/images/ui-anim_basic_16x16.gif');
Doing it this way removes most of the implications of any sort of pathing. Because ASP.NET as a language understands the concept of relative urls, this should work on pretty much any situation unless the server you are hosting it on has something very awkwardly configured - and in that case, standards and practices won't get you too far.
Root-Relative Urls also make your application much more modular, from my experience. There may be more experienced programmers on here that can refute this with a reason, but from everything I have built, making all of my image urls root-relative has allowed me to drop my program into any folder and run it without complication.
I had a similar question. Since MVC allows the use of ~/ (in razor views for example) to denote the application root, I wondered if this should be done for image paths in my CSS files.
Of course, since CSS files are not processed on the server side, this won't work. However I think the right way is to use relative paths. This should be fine because the path to the CSS file (in a layout for example) can use ~/ and then the path from the CSS file to the image will be fixed; and it doesn't matter where the application root is... or if the layout or the main view are in a different Area.