Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why css image will not show when EnableOptimizations = true?

i used mvc4 and when i change

BundleTable.EnableOptimizations = true;

in BundleConfig.cs file my image can not load and failed.

location of my image on my solution:

 themes/default/Profile/images/abc.png

in my BundleConfig:

  bundles.Add(new StyleBundle("~/Content/profile/PRA")
  .Include("~/Content/themes/default/profile/mycss.css"));

my css :

.sampleback{
                background-image: url('images/abc.png');
            }

if i want to use BundleTable.EnableOptimizations = true; i must my css class to until it works:

.sampleback{
              background-image:url('../themes/default/Profile/images/abc.png');
         }

is it a bug?or I'm on a wrong way?

like image 793
motevalizadeh Avatar asked Apr 17 '26 01:04

motevalizadeh


1 Answers

Relative paths in external CSS resources are resolved as relative to the path of the CSS resource itself.

So if your not-bundled CSS lies in one path, and the bundled CSS gets downloaded from another path, then of course the relative path will not be correct any more. (Unless the bundling mechanism would take care of that, and correct such relative paths itself before it writes then into the bundled CSS.)

Easiest solution would be to always use paths that are relative to the domain root, so starting with a /. Of course that makes your project less flexible, should you f.e. one day move it to a sub-folder. If you want to avoid that as well, then look for a kind of solution that pre-parses your CSS, and allows you to use a variable/constant to define a base path in a central place.

like image 74
CBroe Avatar answered Apr 20 '26 09:04

CBroe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!