Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to center background image in window resize

Tags:

html

css

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

like image 302
Himberjack Avatar asked Aug 23 '11 17:08

Himberjack


People also ask

How do I make the background image fit my screen size?

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.

How do I keep the background of a picture in the center?

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.

Which position adjust the background image in the center of the desktop?

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.

How do I stretch a background image in CSS to fit the screen?

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.


2 Answers

Use a percentage: background-position: 50% 50% will center the background image.

like image 69
GolezTrol Avatar answered Oct 06 '22 21:10

GolezTrol


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;
like image 43
Arsen K. Avatar answered Oct 06 '22 22:10

Arsen K.