Dynamic URLs in CSS/JS

后端 未结 9 913
别跟我提以往
别跟我提以往 2021-02-05 16:14

I\'m splitting up one of my larger apps and introducing a \'cdn\' url to house common objects like CSS, javascript, and images to avoid duplication. What I need to do, though, i

9条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 16:32

    Use relative paths, not absolute paths. When inside a CSS file, the path is relative to the CSS file and not the HTML page.

    If your CSS file is here

    http://cdn.example.com/css/style.css
    

    And your class is

    .cool-button { background-image: url('../images/button.png'); }
    

    Then the browser will attempt to load the image from

    http://cdn.example.com/images/button.png
    

提交回复
热议问题