Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontally aligning button

Tags:

html

css

button

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.

like image 713
James Dawson Avatar asked Jun 07 '12 16:06

James Dawson


People also ask

How do I align a button horizontally?

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.

What is horizontally align?

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.

What is an alignment button?

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 you align a button in a line?

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.


1 Answers

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.

like image 82
j08691 Avatar answered Oct 05 '22 11:10

j08691