CSS background image on laravel 5.4 with {{ asset('path/image.png') }}

15,743

background images are relative to the css file. Ordinarily you would just specify the path like

.some-class{
background-image: url("/img/background-header.jpg"); 
}

The above css code works if you are accessing images from public folder like for example

public/
    img/
       background-header.jpg
Share:
15,743

Related videos on Youtube

Bariq Dharmawan
Author by

Bariq Dharmawan

60% web developer, 30% football, 10% unexpected things

Updated on June 04, 2022

Comments

  • Bariq Dharmawan
    Bariq Dharmawan almost 2 years

    Can I write css for background-image like this on laravel project?

    .some-class {
      background-image: url('{{ asset('path/image.png') }}');
    }
    
    • thefallen
      thefallen about 6 years
      You can write this inline in the view.
    • Bariq Dharmawan
      Bariq Dharmawan about 6 years
      Ok, thank for your suggestion
  • Bariq Dharmawan
    Bariq Dharmawan about 6 years
    so, the slash on before img it mean access photo from root, to the path of image?
  • Poornima Subramani Naidu
    Poornima Subramani Naidu almost 6 years
    Exactly , / means the root of the current drive; ./ means the current directory; ../ means the parent of the current directory.
  • Synapsido
    Synapsido almost 3 years
    Console error shows GET localhost:8000/css/img/background-header, note the "css" folder, the correct solution is move up one level like this: url("../img/background-header.jpg");