26

I have a simple submit button. I am wanting to align it to the center. Here is my code:

<input type="submit" name="btnSubmit" value="Submit" onClick="Submit" align="center">

However, it does not work. What is the best/easiest way to do this?

Paolo Forgia
6,7568 gold badges51 silver badges60 bronze badges
asked Apr 2, 2012 at 6:27

5 Answers 5

44

You should use something like this:

<div style="text-align:center"> 
 <input type="submit" /> 
</div> 

Or you could use something like this. By giving the element a width and specifying auto for the left and right margins the element will center itself in its parent.

<input type="submit" style="width: 300px; margin: 0 auto;" />
mrtsherman
40k23 gold badges90 silver badges111 bronze badges
answered Apr 2, 2012 at 6:31
Sign up to request clarification or add additional context in comments.

1 Comment

This isn't quite right. You posted two different ways of accomplishing the task. One of them is correct, the other wrong. You can use text-align: center on the parent which is correct. But he may not want everything in the parent to be centered. Adding it just for centering would be wasteful markup. The addition of margin: 0 auto; to this solution is unnecessary. That is a separate method. It requires the submit button to have a width defined. You should correct your current answer and probably post the second solution also.
2

Here is what worked for me:

 <input type="submit" style="margin-left: 50%">

If you only add margin, without the left part, it will center the submit button into the middle of your entire page, making it difficult to find and rendering your form incomplete for people who don't have the patience to find a submit button lol. margin-left centers it within the same line, so it's not further down your page than you intended. You can also use pixels instead of percentage if you just want to indent the submit button a bit and not all the way halfway across the page.

answered Mar 15, 2017 at 1:33

Comments

2

For me it worked using flexbox, which is in my opinion the cleanest solution.

Add a css class around the parent div / element with :

.parent {
display: flex;
}

and for the button use:

.button {
justify-content: center;
}

You should use a parent div, otherwise the button doesn't 'know' what the middle of the page / element is.

If this is not working, try :

#wrapper {
 display:flex;
 justify-content: center;
}
answered Nov 1, 2017 at 11:51

Comments

0
margin: 50%; 

You can adjust the percentage as needed. It seems to work for me in responsive emails.

Cody Gray
246k53 gold badges514 silver badges591 bronze badges
answered Apr 22, 2016 at 16:59

Comments

0

Add width:100px, margin:50%. Now the left side of the button is set to the center.
Finally add half of the width of the button in our case 50px.
The middle of the button is in the center.

<input type='submit' style='width:100px;margin:0 50%;position:relative;left:-50px;'>
answered Oct 29, 2017 at 22:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.