Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML-CSS Center Text horizontally and vertically in a link

I am trying to make a link which has a height and a width of 200px. The text of the link shall be centered vertically and horizontally.

This is my CSS so far:

a:link.Kachel {   width: 200px;   height: 200px;   margin: 0 auto;   display: block;   text-align: center;   background: #383838;   display: -webkit-box;   display: -moz-box;   display: box;   -webkit-box-align: center;   -moz-box-align: center;   box-align: center; } 

and this the HTML-code:

<tr>   <td>     <a class="Kachel" href="#">Test</a>   </td> </tr> 

The text is horizontally centered but not vertically.

Any idea how to get the text centered both horizontally and virtically?

like image 844
user1567896 Avatar asked Feb 13 '13 20:02

user1567896


People also ask

How do you center text both horizontally and vertically in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do I center text in a link using CSS?

1) Centering links by putting it inside of a text aligned div. Place the HTML link inside of a div. In the styles for the div, apply text-align:center and make the anchor tag an inline-block (display:inline-block).

How do you align text center horizontally and vertically in HTML?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.

How do I center a link vertically in CSS?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .


2 Answers

remove all that other nonsense, and just replace height with line-height

a:link.Kachel{    width: 200px;    line-height: 200px;    display: block;    text-align: center;    background: #383838; } 

jsfiddle: http://jsfiddle.net/6jSFY/

like image 71
Eric Goncalves Avatar answered Sep 20 '22 02:09

Eric Goncalves


In CSS3, you can achieve this with a flexbox without any extra elements.

.link {     display: flex;     justify-content: center;     align-items: center; } 
like image 31
Kilves Avatar answered Sep 18 '22 02:09

Kilves