111

I have a very basic HTML which mix plain text and icon fonts. The problem is that icons are not exactly rendered at the same height than the text:

<div class="ui menu">
  <a href="t" class="item"><i class="large home basic icon"></i><span class="nav-text"> Accueil</span></a>
  <a href="" class="item"><i class="large camera retro icon"></i><span class="nav-text"> Créations</span></a>
  <a class="item"><span class="nav-text">Qui-suis je </span><i class="large help basic icon"></i></a>
</div>

enter image description here

Any suggestion to fix it?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Fractaliste
  • 5,777
  • 11
  • 42
  • 86

10 Answers10

161

In this scenario, since you are working with inline-level elements, you could add vertical-align: middle to the span elements for vertical centering:

.nav-text {
  vertical-align: middle;
}

Alternatively, you could set the display of the parent element to flex and set align-items to center for vertical centering:

.menu {
  display: flex;
  align-items: center;
}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
  • 1
    From the question it seems that he wants to align the icons with the text instead of the text with the icons. – Arbel Mar 20 '14 at 21:29
  • 4
    @Arbel I'm not sure - the title says: *"align text with icons"* .. it's essentially the same thing though. – Josh Crozier Mar 20 '14 at 21:34
  • 1
    @Arbel All right I forgot to write if I needed a baseline or center alignment, but both proposal provide a **much more cleaner** result than my initial view so I consider your both answers as good one's. – Fractaliste Mar 20 '14 at 21:40
59

There are already a few answers here but I found flexbox to be the cleanest and least "hacky" solution:

parent-element {
  display: flex;
  align-items: center;
}

To support Safari < 8, Firefox < 21 and Internet Explorer < 10 (Use this polyfill to support IE8+9) you'll need vendor prefixes:

parent-element {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}
Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58
sam
  • 1,711
  • 1
  • 17
  • 24
  • Is the idea to apply this rules to the `a` element? If it is, you are changing the `display` of the link so it's going to convert an inline element into a block one. I don't agree that this is the best solution. – Gorzas May 11 '20 at 10:10
  • @Gorzas In that case, I believe you could use `inline-flex` instead of just `flex` – Moon Cheesez Jun 20 '20 at 03:00
38

vertical-align can take a unit value so you can resort to that when needed:

{
  display:inline-block;
  vertical-align: 5px;
}

{
  display:inline-block;
  vertical-align: -5px;
}
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
getsetbro
  • 1,798
  • 1
  • 22
  • 33
6

To expand on Marian Udrea's answer: In my scenario, I was trying to align the text with a material icon. There's something weird about material icons that prevented it from being aligned. None of the answers were working, until I added the vertical-align to the icon element, instead of the parent element.

So, if the icon is 24px in height:

.parent {
    line-height: 24px; // Same as icon height

    i.material-icons {  // Only if you're using material icons
      display: inline-flex;
      vertical-align: top;
    }
}
Original BBQ Sauce
  • 527
  • 1
  • 11
  • 24
5

Add this to your CSS:

.menu i.large.icon,
.menu i.large.basic.icon {
    vertical-align:baseline;
}

DEMO

Arbel
  • 30,599
  • 2
  • 28
  • 29
5

Set line-height to the vertical size of the picture, then do vertical-align:middle like Josh said.

so if the picture is 20px, you would have

{
line-height:20px;
font-size:14px;
vertical-align:middle;
}
almalki
  • 4,595
  • 26
  • 30
Marian Udrea
  • 111
  • 3
3

to center vertically and horizontally use this:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
1

Adding to the spans

vertical-align:baseline;

Didn't work for me but

vertical-align:baseline;
vertical-align:-webkit-baseline-middle;

did work (tested on Chrome)

Tomer Almog
  • 3,604
  • 3
  • 30
  • 36
1

Using CSS Grid

HTML

<div class="container">
    <i class="fab fa-5x fa-file"></i>
    <span>text</span>
</div>

CSS

.container {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
}

Working example

yooloobooy
  • 493
  • 6
  • 12
0

You can use this property : vertical-align:middle;

.selector-class { 
    float:left;
    vertical-align:middle; 
} 
kishan Radadiya
  • 788
  • 6
  • 14