How to Center a Button In CSS and HTML

Centering a button in CSS is a common task when creating websites. When a button is centered, it is aligned horizontally in the center of a containing element. This helps to make the webpage look more visually appealing and professional.

There are several ways to center a button in CSS, and this can be achieved using different CSS properties and techniques. In this response, we will explore some of the ways to center a button in CSS.

Button center horizontally with text-align

One simple way to center a button horizontally in CSS is to use the text-align property. This property can be used to horizontally align text and inline elements, including buttons, within a container. Here is an example code snippet that demonstrates how to center a button using text-align:

<div style="text-align: center;">
  <button>Click Me</button>
</div>

In this example, we use the text-align property on the containing div element and set its value to center. This centers the button within the div horizontally. Note that the button element is an inline element by default, which allows it to be centered using the text-align property.

Using text-align is a simple and effective way to center a button horizontally in CSS. However, it only works for inline and inline-block elements, so if your button is a block element or has a fixed width, you may need to use other techniques that explain below to center it.

Button center horizontally using margin auto

If the button is a block element, you can use margin auto techniques to center it horizontally. This technique is very useful and you can use it to center align the Image within the element.

By using this method, We will Set the left and right margins to auto, and the browser will automatically calculate equal margins on both sides, centering the button within its parent element. Here is an example code snippet:

<div style="text-align: center;">
  <button style="display: block; margin: 0 auto;">Click Me</button>
</div>

In this example, we set the display property of the button to block to make it a block-level element, and then set its left and right margins to auto.

How to center button horizontally and vertically

To center a button both horizontally and vertically, you can use the CSS Flexbox layout. Flexbox is a powerful layout tool in CSS that allows you to easily align and distribute elements in a flexible way.

Here is an example code snippet that uses Flexbox to center a button both horizontally and vertically:

<div style="display: flex; justify-content: center; align-items: center; height: 100vh;">
  <button>Click Me</button>
</div>

In this example, we set the display property of the containing element to flex and used the justify-content property to center the button horizontally and the align-items property to center the button vertically. The height property is also set to 100vh to make the containing element take up the full height of the viewport.

By setting both the justify-content and align-items properties to center, the button will be centered both horizontally and vertically within the containing element. You can adjust the styling properties as needed to fit your specific layout requirements.

Note: Note that this technique works for both inline and block-level elements, so it can be used to center buttons of any type or size.

Use Flex If Two buttons are Side by Side

If you have two buttons that you want to center side by side, you can use CSS Flexbox to achieve this layout. Here is an example code snippet that demonstrates how to center two buttons side by side using Flexbox:

<div style="display: flex; justify-content: center; align-items: center;">
  <button>Button 1</button>
  <button>Button 2</button>
</div>

In this example, we set the display property of the containing element to flex and used the justify-content property to center the buttons horizontally and the align-items property to center the buttons vertically. The buttons are simply placed one after the other in the order they appear in the HTML code.

By setting the justify-content property to center, the buttons will be centered horizontally within the containing element. By default, Flexbox aligns items along the main axis (which is horizontal by default), so the buttons will be positioned side by side.

You can adjust the spacing between the buttons by adding padding or margin to the buttons themselves or by adjusting the gap property of the containing element.

In this way, you can center two buttons side by side using CSS Flexbox.

Conclusion

In conclusion, there are different techniques in CSS to center buttons horizontally, vertically, or side by side, depending on your specific layout requirements. For a single button, you can use text-align or margin properties to center the button horizontally. For a block-level button, you can use margin or layout tools such as Flexbox or CSS Grid to center the button horizontally.

You Might Be Interested In:

Ashfaq Ahmed is a freelance WordPress developer and owner of codeconvey.com. I developed modern and highly interactive websites. I always focus on improving my development and design skills.

Leave a Comment