center image on a page

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.
bhomassAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stergiumCommented:
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
0
lwadwellCommented:
<style type="text/css">
  img.centred {
    display:block;
    margin-left:auto;
    margin-right:auto;
  }
</style>

<img src="logo.png" alt="alt description" class="centred">
0
Kyle HamiltonData ScientistCommented:
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.
0
Determine the Perfect Price for Your IT Services

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

rbudjCommented:
#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.
0
Kyle HamiltonData ScientistCommented:
@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;
}
0
rbudjCommented:
Should not need a height unless you prefer, but you will need to adjust the margins to get the exact result you need.
0
Kyle HamiltonData ScientistCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
COBOLdinosaurCommented:
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&
0
bhomassAuthor 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.