Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text inside anchor tag

Tags:

html

css

I want my anchor tag look like a button, and created this style JsFiddle

.details-button {
    background: linear-gradient(to bottom, #FFFFFF 0, #FAB149 2%, #F89406 100%) repeat scroll 0 0 transparent;
    border: 1px solid #FAB149;
    -ms-border-radius: 3px;
    border-radius: 3px;
    -webkit-box-shadow: 0 1px 2px #999999;
    -ms-box-shadow: 0 1px 2px #999999;
    box-shadow: 0 1px 2px #999999;
    color: #FFFFFF;
    cursor: pointer;
    font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 13px;
    font-weight: bold;
    -ms-text-shadow: 0 -1px #099FDF;
    text-shadow: 0 -1px #099FDF;
    margin: 4px;
    height: 28px;
    width: 85px;
    vertical-align: central;
    align-items: center;
    text-decoration: none;
    font: menu;
    display: inline-block;
    /*padding: 6px 0 5px 18px;*/
}

It looks as I want, but how to center text horizontally and vertically inside anchor tag?

like image 597
Arsen Mkrtchyan Avatar asked Jan 29 '14 22:01

Arsen Mkrtchyan


People also ask

How do I center align text in anchor tag?

text-align: center; Finally, make your height auto and instead of a line-height declaration, set the padding to taste: height:auto; padding:3px 0; That should do it!

How do I center text in a HTML tag?

One way to center text or put it in the middle of the page is to enclose it within <center></center> tags. Inserting this text within HTML code would yield the following result: Center this text!

How do you center text within an element?

To just center the text inside an element, use text-align: center; This text is centered.

How do I center an icon in anchor tag?

You should center the content using text-align . There is no need for flex here, as flex actually tries to put several elements on one line.


2 Answers

Add

text-align: center;
line-height: 28px;

working demo: http://jsfiddle.net/3SE8L/2/

like image 159
MarcinJuraszek Avatar answered Oct 09 '22 13:10

MarcinJuraszek


line-height and padding work if the height of the element is hard-coded, but I like to use

{
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}

to keep text centered in a tags where the height may change.

like image 11
Kat Avatar answered Oct 09 '22 11:10

Kat