I can not center HTML form elements

8

I'm with something simple, but for some reason I can not achieve it. As the title says, I can not center the elements of an HTML form. I wanted to see if they could give me a hand. Here the code:

<body>
    <object id="header" data="header.html" onload="resizeObj(this);"></object>

            <form id="formulario" action="enviado.php" method="post">
                <div id="content">
                    <label>Nombre</label><br>
                    <input id="nombre" name="nombre" type="text" /> <br>
                    <label>Email</label><br>
                    <input id="email" name="email" type="text" /> <br>
                    <label>Contenido</label><br>
                    <textarea id="contenido" name="comment" cols="30" rows="5">Ingresa aqui el mensaje...</textarea><br>

                    <input id="campo3" name="enviar" type="submit" value="Enviar" />
                </div>
            </form>

     <object id="footer" data="footer.html" onload="resizeObj(this);" ></object>
</body>

And the css:

*{
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body{
    padding: 0px;
    margin: 0px;
    border:none;
    width: 100%;
}

#header{
    width: 100%;
    padding: 0px;
    margin: 0px;
    overflow:hidden;
}

#footer{
    position: fixed;
    bottom:0;
    left:0;
    width: 100%;
    padding: 0px;
    margin: 0px;
    overflow:hidden;
}

#formulario{
    margin: 0 auto;
    align-content: center;
    border-radius: 10px;
    border: 1px solid #666666;
    width: 500px;
}

label {
    font-weight: bold;
    color: #444;
    font-size: 14px;
}

input {
    font-size: 14px;
}

input[type="text"] {
    border-radius: 5px;
    margin: 10px 0;
    background: #fff;
    border: 1px solid #ccc;
    color: #777;
    max-width: 100%;
    outline: none;
    padding: 7px 8px;
}

input[type="submit"] {
    border-radius: 5px;
    margin: 10px 0;
    background: #0088B2;
    color: #fff;
    padding: 8px 14px;
    font-weight: bold;
    border: none;
    cursor: pointer;
}

textarea{
    border-radius: 5px;
    margin: 10px 0;
    background: #fff;
    border: 1px solid #ccc;
    color: #777;
    max-width: 100%;
    outline: none;
    padding: 7px 8px;
}

input[type="submit"]:hover {
    background: #444;
}

Here is a screenshot of how the site is:

    
asked by Facundo Curti 03.10.2016 в 20:22
source

5 answers

9

I think you're using the wrong property, the one you need in this case for the form is text-align instead of align -content (which is a property of a flexbox container):

*{
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body{
    padding: 0px;
    margin: 0px;
    border:none;
    width: 100%;
}

#header{
    width: 100%;
    padding: 0px;
    margin: 0px;
    overflow:hidden;
}

#footer{
    position: fixed;
    bottom:0;
    left:0;
    width: 100%;
    padding: 0px;
    margin: 0px;
    overflow:hidden;
}

#formulario{
    margin: 0 auto;
    text-align: center;
    border-radius: 10px;
    border: 1px solid #666666;
    width: 500px;
}
<body>
    <object id="header" data="header.html" onload="resizeObj(this);"></object>

            <form id="formulario" action="enviado.php" method="post">
                <div id="content">
                    <label>Nombre</label><br>
                    <input id="nombre" name="nombre" type="text" /> <br>
                    <label>Email</label><br>
                    <input id="email" name="email" type="text" /> <br>
                    <label>Contenido</label><br>
                    <textarea id="contenido" name="comment" cols="30" rows="5">Ingresa aqui el mensaje...</textarea><br>

                    <input id="campo3" name="enviar" type="submit" value="Enviar" />
                </div>
            </form>

     <object id="footer" data="footer.html" onload="resizeObj(this);" ></object>
</body>
    
answered by 03.10.2016 / 20:26
source
2
#formulario{
    margin: 0 auto;
    text-align: center;
    border-radius: 10px;
    border: 1px solid #666666;
    width: 500px;
}
    
answered by 14.10.2016 в 08:38
2

Put the form inside a div with a class and you put float none

<div class="centrar-form">
  <form>tus formularios</form>
</div>

.centrar-form{
 margin: 50px auto;
 float:none;
}

I also recommend that you work with bootstrap and that you put a class or style with resize: none is more beautiful and stylized. (in bootstrap you have predefined classes for all kinds of elements, as for example for the inputs you have the form-control class that leaves you the nice inputs)

    
answered by 14.10.2016 в 10:04
2

I would also recommend two other solutions.

The first is to add a padding to the form and add a 100% width to the input elements, it would not focus but it would give a very good appearance.

form {
  padding: 30px;
}

form input, form textarea {
  display: block;
  width: 100%;
}

Example: link

The second option is similar, but by adding a container inside the form, add a width to the container, and center it.

form {
  padding-bottom: 20px;
  padding-top: 20px;
}
form .content {
  margin: 0 auto;
  width: 90%;
}

form input, form textarea {
  display: block;
  width: 100%;
}

Example: link

    
answered by 14.10.2016 в 03:17
0

To center the elements of your form ( inputs and labels ) you can use text-align: center as you have commented previously.

However, since you are using Bootstrap you can also use the predefined class by Bootstrap called text-center directly on the form.

<form id="formulario" action="enviado.php" method="post" class="text-center">

You can really use text-align: center or text-center on the inputs and% label because they have the default property display: inline which makes them treated as if they were text and therefore can be center.

If the elements that contained the form had, in their defect, the property display: block , they would not be centered with the properties named above.

Also, you should remove the align-content: center; property from your CSS since it would only take effect if you had put the display: flex (flexbox) property on the form.

Your corrected example:

*{
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body{
    padding: 0px;
    margin: 0px;
    border:none;
    width: 100%;
}

#header{
    width: 100%;
    padding: 0px;
    margin: 0px;
    overflow:hidden;
}

#footer{
    position: fixed;
    bottom:0;
    left:0;
    width: 100%;
    padding: 0px;
    margin: 0px;
    overflow:hidden;
}

#formulario{
    margin: 0 auto;
    border-radius: 10px;
    border: 1px solid #666666;
    width: 500px;
}

label {
    font-weight: bold;
    color: #444;
    font-size: 14px;
}

input {
    font-size: 14px;
}

input[type="text"] {
    border-radius: 5px;
    margin: 10px 0;
    background: #fff;
    border: 1px solid #ccc;
    color: #777;
    max-width: 100%;
    outline: none;
    padding: 7px 8px;
}

input[type="submit"] {
    border-radius: 5px;
    margin: 10px 0;
    background: #0088B2;
    color: #fff;
    padding: 8px 14px;
    font-weight: bold;
    border: none;
    cursor: pointer;
}

textarea{
    border-radius: 5px;
    margin: 10px 0;
    background: #fff;
    border: 1px solid #ccc;
    color: #777;
    max-width: 100%;
    outline: none;
    padding: 7px 8px;
}

input[type="submit"]:hover {
    background: #444;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body>
    <object id="header" data="header.html" onload="resizeObj(this);"></object>

            <form id="formulario" action="enviado.php" method="post" class="text-center">
                <div id="content">
                    <label>Nombre</label><br>
                    <input id="nombre" name="nombre" type="text" /> <br>
                    <label>Email</label><br>
                    <input id="email" name="email" type="text" /> <br>
                    <label>Contenido</label><br>
                    <textarea id="contenido" name="comment" cols="30" rows="5">Ingresa aqui el mensaje...</textarea><br>

                    <input id="campo3" name="enviar" type="submit" value="Enviar" />
                </div>
            </form>

     <object id="footer" data="footer.html" onload="resizeObj(this);" ></object>
</body>
    
answered by 14.12.2016 в 22:37