margin-left: auto and margin-right: auto not working on input
CssHtmlCss Problem Overview
I have an input in a form that I am trying to align in the center. Usually margin-left:auto
and margin-right: auto
fail to respond when display: block
isn't added to the CSS. I have added display: block
to my CSS but still it isn't display as I would like it to.
I've made a JSFiddle to keep easier to understand: http://jsfiddle.net/XnKDQ/97/
Css Solutions
Solution 1 - Css
In order for margin: 0 auto;
to work, in addition to display:block
a width
needs to be specified.
Solution 2 - Css
In my case, it was float: left
that I forgot about. So, make sure you apply float: none
to your input field.
You already have display: block
and margin: 0 auto
, you just need to set width too.
Example that should work:
input{
width:50% !important;
margin:0 auto !important;
display:block !important;
float:none !important;
}
If that doesn't work try adding !important
to the values or make sure your style is not overwritten by something else.
Solution 3 - Css
I found the solution for you
you should specify element width
input {
display: block;
width: 60px;
margin: 10px auto;
padding: 3px;
}
Solution 4 - Css
You have can do something like this:
input {
width: fit-content;
margin: auto;
}
Solution 5 - Css
You want the three forms together to be aligned in the center? Wrap them in a single div and center that div instead.
Solution 6 - Css
in addition to specifying width and display block also check if the float property is set to none.
Solution 7 - Css
In addiction to everything, you may check if your body have the parameter position. It may be set to fixed.