Chris Millard
asked on
Center image in page both horizontally and vertically
Using either PHP, HTML or CSS, how can I center an image exactly in a web page? I want to take into account that if the window gets resized, the image should still appear in the center of the windows new size.
I've tried setting a background image using CSS
and while this looks OK in my browser if I am loading the file locally, if I upload the same page to the internet and try to view it in IE, Safari or Firefox, the image is centered horizontally, but appears to be disappearing off the top of the screen.
I've tried setting a background image using CSS
body {
background-image:url('myimage.png');
background-position:center center;
background-repeat:no-repeat;
}
and while this looks OK in my browser if I am loading the file locally, if I upload the same page to the internet and try to view it in IE, Safari or Firefox, the image is centered horizontally, but appears to be disappearing off the top of the screen.
If you are using the same browser and image and page, it should be no different coming from either place. By the way, no matter how you generate it, HTML with styling in CSS is the only way anything gets displayed. You can use PHP or any other language to generate the HTML and CSS, but HTML and CSS is all the browser understands.
Try adding
display: block;
margin: auto;
height: 100%;
width: 100%;
display: block;
margin: auto;
height: 100%;
width: 100%;
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Found my own solution on another site.