Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 799
  • Last Modified:

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.
0
bhomass
Asked:
bhomass
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now