Solved

center image on a page

Posted on 2012-07-24
9
770 Views
Last Modified: 2012-07-25
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
Comment
Question by:bhomass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 13

Expert Comment

by:stergium
ID: 38221643
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
 
LVL 25

Expert Comment

by:lwadwell
ID: 38221792
<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
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38222303
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 16

Expert Comment

by:rbudj
ID: 38222843
#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
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38222936
@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
 
LVL 16

Expert Comment

by:rbudj
ID: 38223111
Should not need a height unless you prefer, but you will need to adjust the margins to get the exact result you need.
0
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 200 total points
ID: 38223235
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38223729
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
 

Author Closing Comment

by:bhomass
ID: 38225606
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question