0

I want to change my html body background image

<style> 
body:after{
  content:"";
  position:fixed; /* stretch a fixed position to the whole screen */
  top:0;
  height:100vh; /* fix for mobile browser address bar appearing disappearing */
  left:0;
  right:0;
  z-index:-1; /* needed to keep in the background */
  background: url("img/001.jpg") center center;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;

}

I've tried $('body').css('background-image', 'url(img/002.jpg)') with no luck

Thank you very much

webmastx
  • 683
  • 1
  • 8
  • 30
  • 2
    Why on earth would anyone use body:after? I would really like to know... – Liron Navon Jun 24 '19 at 14:08
  • You can't target a pseudo element in JavaScript because it's not part of the DOM – metaDesign Jun 24 '19 at 14:09
  • The code is running fine probably, but your DOM is completely loaded by then, so no "Visible" changes occur... Please give some more context on what you are trying to achieve. – DIEGO CARRASCAL Jun 24 '19 at 14:11
  • @LironNavon to avoid using an extra element. I do this a lot. – Temani Afif Jun 24 '19 at 14:12
  • regarding this topic https://stackoverflow.com/a/43058483/773406 I want to centre background image of the html body and change it later! – webmastx Jun 24 '19 at 14:12
  • The question is not why use pseudo-elements, but why use pseudo-elements on the body? it seems very weird for me... – Liron Navon Jun 24 '19 at 14:24
  • 1
    @LironNavon for the same purpose of using pseudo element on any element: avoiding extra element. Suppose you want to have a blurred or rotated background image on your body, how you will do it? – Temani Afif Jun 24 '19 at 14:35

0 Answers0