bhomass
asked on
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.
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.
<style type="text/css">
img.centred {
display:block;
margin-left:auto;
margin-right:auto;
}
</style>
<img src="logo.png" alt="alt description" class="centred">
img.centred {
display:block;
margin-left:auto;
margin-right:auto;
}
</style>
<img src="logo.png" alt="alt description" class="centred">
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.
.centeredimage{
background:url(path/to/img
}
<body class="centeredimage">
....
</body>
Otherwise you an use JavaScript.
#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.
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.
@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;
}
#holder {
position: absolute;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -300px;
width: 600px;
height:300px;
background-color: #000000;
}
Should not need a height unless you prefer, but you will need to adjust the margins to get the exact result you need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A bit of irregular property use (but perfectly standards compliant. Works in Firefox,
but not in IE; and maybe not in other lesser browsers.
Really just a novelty. The correct way to do it is with a positioned background image.
Cd&
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>
Really just a novelty. The correct way to do it is with a positioned background image.
Cd&
ASKER
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.
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
hope that helps