I'm trying to horizontally align a button using CSS. Here's my markup:
<button type="button" class="primary-button download-button">Download</button>
And my relevant CSS:
.primary-button {
font-family: Arial, sans-serif;
font-size: 1.3em;
color: #fff;
border: 1px solid #004487;
background-image: -webkit-gradient(linear, top, bottom, from(#5288bd), to(#2f659a));
background-image: -webkit-linear-gradient(top, #5288bd, #2f659a);
box-shadow: inset 0px 1px 0px rgba(255, 255, 255, 0.4), inset 0px 0px 0px 1px rgba(255, 255, 255, 0.2), 0px 1px 0px rgba(255, 255, 255, .7);
text-shadow: 0px -1px 0px black;
cursor: pointer;
}
.download-button {
padding: 10px 20px;
border-radius: 5px;
display: block;
margin: 10px auto 0px auto;
}
However, it's still aligned to the left. I thought setting margin: 10px auto 0px auto;
would horizontally align it seen as it's now a block element but apparently not.
I've also tried wrapping it in a div and using text-align: center
but that doesn't work either.
We can align the buttons horizontally as well as vertically. We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.
Definition English: Horizontal alignment is the positioning of a roadway, as shown in the plan view, using a series of straight lines called tangents connected by circular curves.
Text alignment is a paragraph formatting attribute that determines the appearance of the text in a whole paragraph. For example, in a paragraph that is left-aligned (the most common alignment), text is aligned with the left margin. In a paragraph that is justified, text is aligned with both margins. Align text left.
How do I align two buttons on the same line? If you have multiple buttons that should sit side-by-side on the same line, add the data-inline="true" attribute to each button. This will style the buttons to be the width of their content and float the buttons so they sit on the same line.
Give the button a width. See this jsFiddle example.
.download-button {
padding: 10px 20px;
border-radius: 5px;
display: block;
width: 130px;
margin: 10px auto 0px auto;
}
Without a width, you're trying to center a block level element that is the full width of it's container. By explicitly setting a width (for example 130px on your button) the margin centering rule works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With