?
Solved

center image on a page

Posted on 2012-07-24
9
Medium Priority
?
834 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
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

569 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