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?
To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.
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).
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.
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 .
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/
In CSS3, you can achieve this with a flexbox without any extra elements.
.link { display: flex; justify-content: center; align-items: 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