Search code examples
csspathbackgroundimagesrc

does the src of a background image relate to its path from the css or the file its being used in?


When you use a background image and set the src are you making that the src from the css file the path or the source from the file that the background image will actually be active in ?


Solution

  • The src attribute in HTML is relative to the rendered document. If you had a file /index.html and the img you wanted to inlude was at /img/1.png, the attribute would read src="img/1.png".

    Likewise, the url attribute in CSS is relative to the CSS document. If your CSS file was at /css/styles.css the attribute would read background:url('../img/1.png').