Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

Please how do I position a form to be in the center of a webpage, using HTML & CSS?

Kindly visit this site for an example: http://www.rhapsodyofrealities.org/onlinereachout2013/[^]

Thank you.
Posted
Comments
maneavnash 18-Jul-13 10:15am    
use center tag
Sergey Alexandrovich Kryukov 18-Jul-13 10:45am    
No, this tag will center all the elements in the form, while you often just want to shift the form (or other block element) to the center. Please see my solution.
—SA
Arpit Shrivastava 18-Jul-13 10:20am    
I have analyze your form, and it is centered, but the Label you use for like "Your Name", "Location", you have set your label a width (198px) which you need to minimize.

You can also use
text-align: center;
in your CSS file for the form.
Sergey Alexandrovich Kryukov 18-Jul-13 10:46am    
It won't work, be the reason I explained above. Please see my solution.
—SA
Arpit Shrivastava 18-Jul-13 10:53am    
But still you use
text-align: center;
in your style tag

I finally did it!

Here is the solution, it fits the <div> tag exactly in the center of the page.

HTML
<div class="centered"></div>



CSS
.centered{
    position:absolute;
    width:100px;
    height:100px;
    left:50%;
    top:50%;
    margin-left:-50px;
    margin-top:-50px;
}



Thank you so much guys!
 
Share this answer
 
v2
This is a bit harder to achieve than it may seem at first glance, and center tag won't really work. Please consider this code:

HTML
<html >
	<head>
		<title>???</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<style type="text/css"><!--
			.center {
				margin-top:10px;
				margin-left:auto;
				margin-right:auto;
				text-align:center;
				border:solid 1px black;
				width:828px;
				height:20px;}
		--></style>
	</head>
<body>

<div class="center">It will work for any inner HTML you want to place here...</div>

</body>
</html>


Got the idea?

—SA
 
Share this answer
 
v2
Comments
Valery Possoz 18-Jul-13 10:59am    
The problem is that there's a huge fixed size width for the label and he's right aligned then in the CSS:

form.register label{
color:#444;
width:198px;
float: left;
text-align: right;
margin-right: 16px;
margin-top:2px;
}

So even if the form is centered, the CSS for the labels will push everything to the right, and it will look weird.
Sergey Alexandrovich Kryukov 18-Jul-13 11:19am    
Well, you don't follow my advice exactly. Center just the block element you need to center. Say, format outer div separately, its inner HTML separately. What's the problem?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900