I'm learning Front end development and I'm trying to code my first site using bootstrap. I got stuck on a pretty simple thing I guess. How do I center a text within a button ?
This is the button, the default one using an "a" tag.
<a class="btn btn-default btn-work" href="#">Check my work</a>
and gave it a width and a height and the text remains stuck on the top..
.btn-work { width: 250px; height: 50px; margin: 0 auto; vertical-align: middle; }
I'm also trying to align the button to the center of the container and I can't figure that out either.
Thanks !
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.
Basically what you need to do is to set display flex and flex-direction as a column to the parent and add a 'margin-top: auto' to its child which needs to be floated to the bottom of the container Note: I have used bootstrap and its classes. Show activity on this post.
Answer: Use the text-center Class You can simply use the built-in class . text-center on the wrapper element to center align buttons or other inline elements in Bootstrap. It will work in both Bootstrap 3 and 4 versions.
You can do this by setting the display property to “flex.” Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
To center the text within the button. Use this code.
.btn-work{ text-align: center; }
To center the button, use a parent div to align it to center.
CSS:
div.parentElement{text-align: center;} .btn-work{ display: inline-block; }
HTML:
<div class="parentElement"> <a class="btn btn-default btn-work" href="#">Check my work</a> </div>
Full CSS:
div.parentElement{text-align: center;} .btn-work { width: 250px; height: 50px; margin: 0 auto; padding: 0; display: inline-block; line-height: 50px; text-align: center; }
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