What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

Center Div

fermlour

Member
Hello, anyone knows if it is possible to center a div vertically and horizontally? I already tried but when I change the zoom is not centered.
 
If I'm not mistaken, the easiest method is defining the div's line-height attribute through CSS. That way, the line of text will be vertically centered automatically. For instance:

HTML:
<div style="line-height:100px">
Text
</div>

That only works for 1 line of text though. If you need to vertically center images or a paragraph, this may help: http://www.vanseodesign.com/css/vertical-centering/

Hope that helps!
 
Centered with respect to what? Do you just have a single div that you want in the middle of a blank document?

Perhaps try posting the code you have so far?
 
Imagine, I have a div with an image 400px x 400px, I want to center the image in the center of the screen. I'm using the code <center>, but I can not center vertically
 
You'll want to set the padding up top to 50% in the css, so it brings down the div half way to the page.

<div id=image><img src="url" /></div>

And for CSS

.image { padding-top:50%;}
 
<html>
<head>
<style type="text/css">
.image { padding-top:50%;}

</style>
</head>

<body>
<center>
<div id=image><img src="http://www.rossmoyne.wa.edu.au/Portals/0/docs/Community/Alumni/Facebook-Icon.jpg" /></div>
</center>
</body>
</html>

Am I doing something wrong?
 
No. like so:

<html>
<head>
<style type="text/css">
#image { padding-top: 10%;}

</style>
</head>

<body>
<center>
<div id="image"><img src="http://www.rossmoyne.wa.edu.au/Portals/0/docs/Community/Alumni/Facebook-Icon.jpg" /></div>
</center>
</body>
</html>

Play around with the percentages for the padding until you get the desired results.
 
<html>
<head>
<style type="text/css">
.image { padding-top:50%;}

</style>
</head>

<body>
<center>
<div id=image><img src="http://www.rossmoyne.wa.edu.au/Portals/0/docs/Community/Alumni/Facebook-Icon.jpg" /></div>
</center>
</body>
</html>

Am I doing something wrong?

Change the .image to #image because id's use the # and classes use the . Then play around with the percentage until you get the desired results.
 
Why don't you get rid of the <center> tag and like Melvin said use "#image { padding-top:50%;}" use "#image { padding-left:50% }" as this will most likely work. Plus ID's use "#" not "." so that could be a problem with your previous code you posted.
 
Why don't you get rid of the <center> tag and like Melvin said use "#image { padding-top:50%;}" use "#image { padding-left:50% }" as this will most likely work. Plus ID's use "#" not "." so that could be a problem with your previous code you posted.

I can not yet, is not centered.
I am using the code:

<html>
<head>
<style type="text/css">
#image { padding-top:50%;}
#image { padding-left:50% }
</style>
</head>

<body>

<div id=image><img src="http://www.rossmoyne.wa.edu.au/Portals/0/docs/Community/Alumni/Facebook-Icon.jpg" /></div>

</body>
</html>

thank you!
 
HTML:
<div id="container" style="height:500px;background:url(http://www.rossmoyne.wa.edu.au/Portals/0/docs/Community/Alumni/Facebook-Icon.jpg) no-repeat 0 50%"></div>

Use the image as a background one, then set the div's height to the appropriate one.
 
Code:
<html>
<head>
<style type="text/css">
#image { padding-top:25%; padding-left:50%; }
</style>
</head>

<body>

<div id=image><img src="http://www.rossmoyne.wa.edu.au/Portals/0/docs/Community/Alumni/Facebook-Icon.jpg" /></div>

</body>
</html>

that should work
 
Back
Top