Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering text within a button

Tags:

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 !

like image 830
Ovidiu G Avatar asked Aug 24 '15 17:08

Ovidiu G


People also ask

How do you center text inside a button?

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.

How do I center an item in a button CSS?

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.

How do I center text in a button in bootstrap?

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.

How do I center text inside a div?

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.


1 Answers

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; } 
like image 196
K.Shrestha Avatar answered Oct 30 '22 02:10

K.Shrestha