I am trying to put in my homepage an image using CSS's background-image and position to change its offset.
problem is, when I resized the browser size, the image will stay static and will not move accordingly
how can that be achieved?
thanks
Using CSS, you can set the background-size property for the image to fit the screen (viewport). The background-size property has a value of cover . It instructs browsers to automatically scale the width and height of a responsive background image to be the same or bigger than the viewport.
You need to specify the position of the image through the "center" value of the background shorthand property. Set the width of your image to "100%". In this example, we also specify the height and background-size of our image.
You can use a combination of position keywords: center , top , bottom , left and right . background-position: center center; The background image will be positioned in the center of the element.
When you work with background images, you may want an image to stretch to fit the page despite the wide range of devices and screen sizes. The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover.
Use a percentage: background-position: 50% 50%
will center the background image.
A few variants to do this:
background: url(../images/bodyBG2.gif) center center no-repeat;
or
background: url(../images/bodyBG2.gif) 50% 50% no-repeat;
or
background-image: url(../images/bodyBG2.gif);
background-repeat: no-repeat;
background-position: 50% 50%;
If you need to position the background vertically to a certain point (150 pixels from the page top, for example), change the code:
background: url(../images/bodyBG2.gif) 50% 150px no-repeat;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With