Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align elements inside an <a> tag vertically

Tags:

html

css

I have an <a> tag (which is part of multiple <li> tags). In the <a> tag I have an <i> tag and some text - the text of the link.

What I would like to achieve is to have the icon on top and the text under it (and then centered). So it would look like:

   ICON
MYTEXTHERE

However they are always placed next to each other. I tried using display: inline-block - because my <a> is inline and the content inside should be block but without success.

The fiddle is here: https://jsfiddle.net/6mg4vt77/5/

Edit: Thanks for the answers but sadly I forgot to mention that I must support IE9.

like image 724
Anton Avatar asked Feb 11 '26 05:02

Anton


2 Answers

try this

<a href="/items/bluetooth" style="display: inline-block; text-align:center">
  <i class="fa fa-bluetooth"></i>
  <br>
  BLUETOOTH
</a>

https://jsfiddle.net/6mg4vt77/7/

like image 156
user3896501 Avatar answered Feb 12 '26 19:02

user3896501


Quick answer, set the icon to 100% wide and center everything in the anchor.

a{
  text-align: center;
}

a .fa {
  width: 100%;
}

JSfiddle Demo

Modern Method

Flexbox:

a {
  border: 1px solid grey;
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<a href="/items/bluetooth">
  <i class="fa fa-bluetooth"></i> BLUETOOTH
</a>
like image 39
Paulie_D Avatar answered Feb 12 '26 20:02

Paulie_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!