Centre Image

Associate
Joined
6 Feb 2003
Posts
751
Location
Sutton
I am looking to do a simple webpage with just an image in the centre of the page. Something that will work across most web browsers. The code I have used is below.

<html><style type="text/css">
.image {
position: absolute;
left: 50%;
top: 50%;
right: 50%;
bottom: 50%;
}
</style>
<body class="image">
<img src="images/256px-Bebo.svg.png" alt="Bebo" width="75" height="75" class="image">
</body>
</html>

Is this the best method to use to do this. Thank you for any help.
 
Oh yes, sorry! In that case the method you have used is good. Centering is something that has never been well handled in web development. It may have been tackled in CSS3? But I haven't really looked into it recently.
 
Made some changes...basically changed the image. The code is now this


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
.image {
position: absolute;
left: 50%;
top: 50%;
right: 50%;
bottom: 50%;
}
</style>
</head>

<body bgcolor="#000000">
<img src="images/Mindset-Logo-Animate400px.gif" width="400" height="171" class="image">
</body>
</html>


the page is here
http://www.mindsetacademy.co.uk/index2.html

prob is image is now not centre.....also got an annoying line/border around the image...any help appreciated thanks
 
You'll need to add a negative margin to the top/left of the image which is half it's width and height, try this.

Code:
.image {
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -85px 0 0 -200px;
}

I also removed the right and bottom settings as you only need top and left.
 
Thank you spunkey you really came thru for me there....lol

Would this code still work if the image size was changed.
 
Last edited:
Back
Top Bottom