Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7852999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:33:22+00:00 2026-06-02T19:33:22+00:00

Problem I have a CSS file with some paths in it (for images, fonts,

  • 0

Problem

I have a CSS file with some paths in it (for images, fonts, etc.. url(..)).

My path structure is like this:

...
+-src/
| +-MyCompany/
|   +-MyBundle/
|     +-Resources/
|       +-assets/
|         +-css/
|           +-stylesheets...
+-web/
| +-images/
|   +-images...
...

I want to reference my images in the stylesheet.

First Solution

I changed all paths in the CSS file to absolute paths. This is no solution, as the application should (and has to!) be working in a subdirectory, too.

Second Solution

Use Assetic with filter="cssrewrite".

So I changed all my paths in my CSS file to

url("../../../../../../web/images/myimage.png")

to represent the actual path from my resources directory to the /web/images directory. This does not work, since cssrewrite produces the following code:

url("../../Resources/assets/")

which is obviously the wrong path.

After assetic:dump this path is created, which is still wrong:

url("../../../web/images/myimage.png")

The twig code of Assetic:

{% stylesheets
    '@MyCompanyMyBundle/Resources/assets/css/*.css'
    filter="cssrewrite"
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

Current (Third) Solution

Since all CSS files end up in /web/css/stylexyz.css, I changed all paths in the CSS file to be relative:

url("../images/myimage.png")

This (bad) solution works, except in the dev environment:
The CSS path is /app_dev.php/css/stylexyz.css and therefore the image path resulting from this is /app_dev.php/images/myimage.png, which results in a NotFoundHttpException.

Is there a better and working solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:33:24+00:00Added an answer on June 2, 2026 at 7:33 pm

    I have came across the very-very-same problem.

    In short:

    • Willing to have original CSS in an “internal” dir (Resources/assets/css/a.css)
    • Willing to have the images in the “public” dir (Resources/public/images/devil.png)
    • Willing that twig takes that CSS, recompiles it into web/css/a.css and make it point the image in /web/bundles/mynicebundle/images/devil.png

    I have made a test with ALL possible (sane) combinations of the following:

    • @notation, relative notation
    • Parse with cssrewrite, without it
    • CSS image background vs direct <img> tag src= to the very same image than CSS
    • CSS parsed with assetic and also without parsing with assetic direct output
    • And all this multiplied by trying a “public dir” (as Resources/public/css) with the CSS and a “private” directory (as Resources/assets/css).

    This gave me a total of 14 combinations on the same twig, and this route was launched from

    • “/app_dev.php/”
    • “/app.php/”
    • and “/”

    thus giving 14 x 3 = 42 tests.

    Additionally, all this has been tested working in a subdirectory, so there is no way to fool by giving absolute URLs because they would simply not work.

    The tests were two unnamed images and then divs named from ‘a’ to ‘f’ for the CSS built FROM the public folder and named ‘g to ‘l’ for the ones built from the internal path.

    I observed the following:

    Only 3 of the 14 tests were shown adequately on the three URLs. And NONE was from the “internal” folder (Resources/assets). It was a pre-requisite to have the spare CSS PUBLIC and then build with assetic FROM there.

    These are the results:

    1. Result launched with /app_dev.php/
      Result launched with /app_dev.php/

    2. Result launched with /app.php/
      Result launched with /app.php/

    3. Result launched with /
      enter image description here

    So… ONLY
    – The second image
    – Div B
    – Div C
    are the allowed syntaxes.

    Here there is the TWIG code:

    <html>
        <head>
                {% stylesheets 'bundles/commondirty/css_original/container.css' filter="cssrewrite" %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
        {# First Row: ABCDEF #}
    
                <link href="{{ '../bundles/commondirty/css_original/a.css' }}" rel="stylesheet" type="text/css" />
                <link href="{{ asset( 'bundles/commondirty/css_original/b.css' ) }}" rel="stylesheet" type="text/css" />
    
                {% stylesheets 'bundles/commondirty/css_original/c.css' filter="cssrewrite" %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
                {% stylesheets 'bundles/commondirty/css_original/d.css' %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
                {% stylesheets '@CommonDirtyBundle/Resources/public/css_original/e.css' filter="cssrewrite" %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
                {% stylesheets '@CommonDirtyBundle/Resources/public/css_original/f.css' %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
        {# First Row: GHIJKL #}
    
                <link href="{{ '../../src/Common/DirtyBundle/Resources/assets/css/g.css' }}" rel="stylesheet" type="text/css" />
                <link href="{{ asset( '../src/Common/DirtyBundle/Resources/assets/css/h.css' ) }}" rel="stylesheet" type="text/css" />
    
                {% stylesheets '../src/Common/DirtyBundle/Resources/assets/css/i.css' filter="cssrewrite" %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
                {% stylesheets '../src/Common/DirtyBundle/Resources/assets/css/j.css' %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
                {% stylesheets '@CommonDirtyBundle/Resources/assets/css/k.css' filter="cssrewrite" %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
                {% stylesheets '@CommonDirtyBundle/Resources/assets/css/l.css' %}
                    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
                {% endstylesheets %}
    
        </head>
        <body>
            <div class="container">
                <p>
                    <img alt="Devil" src="../bundles/commondirty/images/devil.png">
                    <img alt="Devil" src="{{ asset('bundles/commondirty/images/devil.png') }}">
                </p>
                <p>
                    <div class="a">
                        A
                    </div>
                    <div class="b">
                        B
                    </div>
                    <div class="c">
                        C
                    </div>
                    <div class="d">
                        D
                    </div>
                    <div class="e">
                        E
                    </div>
                    <div class="f">
                        F
                    </div>
                </p>
                <p>
                    <div class="g">
                        G
                    </div>
                    <div class="h">
                        H
                    </div>
                    <div class="i">
                        I
                    </div>
                    <div class="j">
                        J
                    </div>
                    <div class="k">
                        K
                    </div>
                    <div class="l">
                        L
                    </div>
                </p>
            </div>
        </body>
    </html>
    

    The container.css:

    div.container
    {
        border: 1px solid red;
        padding: 0px;
    }
    
    div.container img, div.container div 
    {
        border: 1px solid green;
        padding: 5px;
        margin: 5px;
        width: 64px;
        height: 64px;
        display: inline-block;
        vertical-align: top;
    }
    

    And a.css, b.css, c.css, etc: all identical, just changing the color and the CSS selector.

    .a
    {
        background: red url('../images/devil.png');
    }
    

    The “directories” structure is:

    Directories
    Directories

    All this came, because I did not want the individual original files exposed to the public, specially if I wanted to play with “less” filter or “sass” or similar… I did not want my “originals” published, only the compiled one.

    But there are good news. If you don’t want to have the “spare CSS” in the public directories… install them not with --symlink, but really making a copy. Once “assetic” has built the compound CSS, and you can DELETE the original CSS from the filesystem, and leave the images:

    Compilation process
    Compilation process

    Note I do this for the --env=prod environment.

    Just a few final thoughts:

    • This desired behaviour can be achieved by having the images in “public” directory in Git or Mercurial and the “css” in the “assets” directory. That is, instead of having them in “public” as shown in the directories, imagine a, b, c… residing in the “assets” instead of “public”, than have your installer/deployer (probably a Bash script) to put the CSS temporarily inside the “public” dir before assets:install is executed, then assets:install, then assetic:dump, and then automating the removal of CSS from the public directory after assetic:dump has been executed. This would achive EXACTLY the behaviour desired in the question.

    • Another (unknown if possible) solution would be to explore if “assets:install” can only take “public” as the source or could also take “assets” as a source to publish. That would help when installed with the --symlink option when developing.

    • Additionally, if we are going to script the removal from the “public” dir, then, the need of storing them in a separate directory (“assets”) disappears. They can live inside “public” in our version-control system as there will be dropped upon deploy to the public. This allows also for the --symlink usage.

    BUT ANYWAY, CAUTION NOW: As now the originals are not there anymore (rm -Rf), there are only two solutions, not three. The working div “B” does not work anymore as it was an asset() call assuming there was the original asset. Only “C” (the compiled one) will work.

    So… there is ONLY a FINAL WINNER: Div “C” allows EXACTLY what it was asked in the topic: To be compiled, respect the path to the images and do not expose the original source to the public.

    The winner is C

    The winner is C

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem where our CSS file is not playing nice with the
I have a script that updates a CSS file based upon some user input
I have a style sheet where I include background images. background: url(../Images/myImage.png); problem is,
I have the following problem. I have done the following: In my css file,
I have a problem with my CSS on a website I'm working on .
I have a problem with the CSS of the datepicker, I downloaded a default
i have a problem with IE8 + jQuery.css('top') function. 1) I have a DIV
I have a strange problem working with HTML,CSS in different browsers: Firefox 3.6 and
I have a weird problem with my HTML and CSS code: <header> <h1>title</h1> </header>
I have a common problem that I never know how to solve with CSS:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.