How to center buttons in Twitter Bootstrap 3?
Bootstrap 3 offers a simple solution to center a button. Wrap it in a <div>
with the class .text-center
:
For individual buttons, it's more efficient to use the .text-center
class instead of .center-block
as the latter requires additional tweaks for <button>
elements. Simply put the .text-center
on a <div>
to align your button or group of buttons.
Centering with Bootstrap's Grid System
Bootstrap's grid system is your go-to tool when you want to align buttons across various viewport sizes:
The col-sm-6 col-sm-offset-3
classes create a centered column, placing your button exactly in the middle.
Grouping and Aligning Multiple Buttons
When you have multiple buttons, you can use the .btn-group
class to group them and ensure they keep their style:
Every button will align at the center for maximum swag.
Override with Direct CSS
You might run into scenarios where direct CSS overrides could come in handy:
Wrap your button in a <div>
with the .button-container
class:
Trust me; this fallback will be useful when flexbox isn't an option!
Flexbox and Bootstrap 4
Bootstrap 4 introduces flexbox utilities. Here's the syntax when using Bootstrap 4 for alignment:
The button will perfectly center within its container using Bootstrap 4's .d-flex
and .justify-content-center
classes.
Applying Styles for Better Clarity
Use classes like .btn-primary
, .btn-warning
, to apply colorful and meaningful hues to buttons.
The colors ensure that your buttons are both centered and styled for easy comprehension by users.
Containers and Full-width Forms
For forms that span full width, ensure that the container is container-fluid
for a responsive, full-width behavior:
The button maintains being centered, and the form takes up the full width.
Was this article helpful?