center image on a page

bhomass
bhomass used Ask the Experts™
on
I want to place an image in the center of a blank page.

I tried a 3 x 3 table with width and height attributes (which are deprecated anyhow), and did not work.

What is a simple and clever way to achieve this? use css as needed.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
hello.
use this
css (head part)


<style type="text/css">
  .centeredImage
    {
    text-align:center;
    margin-top:0px;
    margin-bottom:0px;
    padding:0px;
    }
</style>

(body part)

<p class="centeredImage"><img src="imgName.gif" alt="image description" height="100" width="100"></p>

hope that helps
Lee WadwellProject Architect

Commented:
<style type="text/css">
  img.centred {
    display:block;
    margin-left:auto;
    margin-right:auto;
  }
</style>

<img src="logo.png" alt="alt description" class="centred">
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
You can't center vertically with CSS alone, unless you make it a background image and use background positioning properties.


.centeredimage{

background:url(path/to/img.jpg) center center no-repeat;

}
<body class="centeredimage">
....
</body>

Otherwise you an use JavaScript.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
#holder {
      position: absolute;
      top: 50%;
      left: 50%;
      margin-top: -300px;
      margin-left: -390px;
      width: 782px;
      background-color: #000000;
}

adjust the with as needed and place your image inside the holder div. This will center the image horizontally and vertically using just css.
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
@rbudj: that's really nice. (Though you need to know the dimensions of your image. And you need a height too).

#holder {
      position: absolute;
      top: 50%;
      left: 50%;
      margin-top: -150px;
      margin-left: -300px;
      width: 600px;
      height:300px;
      background-color: #000000;
}

Commented:
Should not need a height unless you prefer, but you will need to adjust the margins to get the exact result you need.
Data Scientist
Most Valuable Expert 2014
Commented:
no, i guess not, but you do need to know what it is to set the margin-top correctly.

In case you want to use the other technique, here's a revised version:


html{
   background:url(path/to/img.jpg) center center no-repeat;
   height:100%;
}

Open in new window

Top Expert 2013

Commented:
A bit of irregular property use (but perfectly standards compliant.  Works in Firefox,
but not in IE; and maybe not in other lesser browsers.

<html><head>
<title>center</title>
<style type="text/css">
#thediv {
                display:table;
                width:100%;
                height:100%;
                text-align:center;
                vertical-align:middle;
            }
#thecell    {
                    display:table-cell;
                    width:100%;
                height:100%;
                text-align:center;
                vertical-align:middle;
                }
</style>
</head>
<body>
<div id="thediv">
<div id="thecell">
<img src="beer.png" />
</div>
</div>
</body>
</html>

Open in new window


Really just a novelty.  The correct way to do it is with a positioned background image.


Cd&

Author

Commented:
lot of inputs. prob all work, but this is the simplest and works correctly. I tried it on a div, and will not work without the height:100% setting.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial