Link to home
Create AccountLog in
Avatar of Chris Millard
Chris MillardFlag for United Kingdom of Great Britain and Northern Ireland

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

    body {
      background-image:url('myimage.png');
      background-position:center center;
      background-repeat:no-repeat;
    }

Open in new window


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.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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%;
ASKER CERTIFIED SOLUTION
Avatar of Chris Millard
Chris Millard
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Chris Millard

ASKER

Found my own solution on another site.