Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS : center form in page horizontally and vertically

Tags:

html

css

How can i center the form called form_login horizontally and vertically in my page ?

Here is the HTML I'm using right now:

<body>     <form id="form_login">         <p>             <input type="text" id="username" placeholder="username" />         </p>         <p>             <input type="password" id="password" placeholder="password" />         </p>         <p>             <input type="text" id="server" placeholder="server" />         </p>         <p>             <button id="submitbutton" type="button">Se connecter</button>         </p>     </form> </body> 

I have tried to do the following css but my form is never centered :

#form_login {     left: 50%;     top: 50%;     margin-left: -25%;     position: absolute;     margin-top: -25%; } 
like image 987
wawanopoulos Avatar asked Mar 26 '14 10:03

wawanopoulos


People also ask

How do I center both vertically and horizontally in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do you center a form vertically in CSS?

align="center" attribute is now obsolete. You can use text-align attribute for this as following. This will center all the content inside the parent DIV. An optional way is to use margin: auto CSS attribute with predefined widths and heights.

How do I center a form on a page?

Wrap your form element in a div tag. Add a class to the div html tag that will be used to center the form. Add the CSS flexbox to the class form-center. Test your form.

How do I center text in the middle of the page CSS?

To center text in CSS, use the text-align property and define it with the value 'center. ' You can use this technique inside block elements, such as divs. You can also center text in HTML, which is useful if you only want to center individual elements on the page on a case-by-case basis.


1 Answers

you can use display:flex to do this : http://codepen.io/anon/pen/yCKuz

html,body {   height:100%;   width:100%;   margin:0; } body {   display:flex; } form {   margin:auto;/* nice thing of auto margin if display:flex; it center both horizontal and vertical :) */ } 

or display:table http://codepen.io/anon/pen/LACnF/

body, html {        width: 100%;     height: 100%;     margin: 0;     padding: 0;     display:table; } body {     display:table-cell;     vertical-align:middle; } form {     display:table;/* shrinks to fit content */     margin:auto; } 
like image 72
G-Cyrillus Avatar answered Sep 28 '22 06:09

G-Cyrillus