CSS button centering

CSS Button Centering

CSS Button Centering

In web design, buttons are a commonly used element used to trigger user interaction. In many cases, it’s necessary to center a button on a page to make it easier for users to find and click. This article will detail how to achieve this using CSS.

Why Center a Button?

In web design, buttons are often used to trigger specific actions, such as submitting a form, performing a search, or redirecting to another page. To improve the user experience, buttons should be placed in a prominent and easily accessible position on the page. Centering a button makes it easier for users to find and click without causing them to get lost somewhere on the page.


Horizontally Centering a Button

To horizontally center a button, we can use the following common methods:

Inline Block-Level Element + Horizontal Centering

Set the button element as an inline block-level element and apply the text-align: center; style to the parent container to achieve horizontal centering. For example:

<div class="container"> 
<button class="btn">Button</button> 
</div> 
.container { 
text-align: center; 
} 

.btn { 
display: inline-block; 
} 

By enclosing the button in a parent container with the text-align: center; style and setting the button as an inline-block element, the button will be horizontally centered within the parent container.

Absolute positioning + left and right offsets

Another common method to achieve horizontal centering is to use absolute positioning. We can set the button element’s position property to absolute and move the button to the left by half its width by setting left: 50%; and a negative margin-left value. For example:

<div class="container"> 
<button class="btn">Button</button> 
</div> 
.container { 
position: relative; 
} 

.btn { 
position: absolute; 
left: 50%; 
transform: translateX(-50%); 
} 

By setting the button’s container to relative positioning position: relative;, then setting the button to absolute positioning position: absolute;, setting the left distance to 50%, and finally using transform: translateX(-50%); to move the button half its width to the left, the horizontal centering effect is achieved.

Flexbox + Centering

Using Flexbox is another common method for horizontal centering. We can apply the display: flex; style to the parent container of the button and set justify-content: center; to achieve horizontal centering. For example:

<div class="container"> 
<button class="btn">button</button>

</div>

.container {
display: flex;
justify-content: center;

}

Set the button’s container to a flex container with display: flex; and use the justify-content: center; property to center the button horizontally.

Vertically Centering a Button

In some cases, we may want to center a button not only horizontally but also vertically. Here are a few ways to achieve this:

Line Height + Vertical Centering

Setting the button’s container’s line-height to the container’s height allows you to center the button vertically. For example:

<div class="container"> 
<button class="btn">Button</button> 
</div> 
.container { 
height: 100px; 
line-height: 100px; 
} 

.btn { 
display: inline-block; 
} 

By setting the height of the button container to be equal to the line height, the button will be vertically centered.

Absolute Positioning + Up and Down Offsets

In addition to the absolute positioning mentioned in the horizontal centering method, we can also set the button element’s position property to absolute and move the button up by half its height by setting top: 50%; and a negative margin-top value. For example:

<div class="container"> 
<button class="btn">Button</button> 
</div> 
.container { 
position: relative; 
height: 200px; 
} 

.btn { 
position: absolute; 
top: 50%; 
transform: translateY(-50%); 
} 

By setting the button’s container to relative positioning position: relative;, then setting the button to absolute positioning position: absolute;, setting the top distance to 50%, and finally using transform: translateY(-50%); to move the button up by half its height, the vertical centering effect is achieved.

Flexible layout + Centering

Similarly, vertical centering can be achieved using Flexbox. We can apply the display: flex; style to the parent container of the button and set align-items: center; to achieve vertical centering. For example:

<div class="container">
<button class="btn">Button</button>
</div>
.container {
display: flex;
align-items: center;
}

By setting the button’s container to display: flex; and using the align-items: center; property, the button is vertically centered.

Horizontally and vertically centered buttons

Centering buttons both horizontally and vertically is a common requirement. Here are several methods for achieving this:

Table + Centering

By placing the button element within an outer container with display: table;, setting the container to display: table-cell;, and applying the text-align: center; vertical-align: middle; styles, you can achieve horizontal and vertical centering. For example:

<div class="container"> 
<div class="table-cell"> 
<button class="btn">Button</button> 
</div> 
</div> 
.container { 
display: table; 
width: 100%; 
} 

.table-cell { 
display: table-cell; 
text-align: center; 
vertical-align: middle; 
} 

Set the button container to table layout display: table;, set the button container to table cell display: table-cell;, and use text-align: center; vertical-align: middle; to achieve horizontal and vertical centering.

Absolute positioning + vertical, horizontal, and left/right offsets

Similar to the absolute positioning method described previously, absolute positioning can be used to achieve horizontal and vertical centering. We can set the button element’s position property to absolute, and then

One comment
  1. The text carries an organic energy, alive yet serene. It moves naturally, encouraging reflection while maintaining a sense of ease and flow.

Leave a Reply

Your email address will not be published. Required fields are marked *