Samuel E.
Samuel E.

Reputation: 2432

How to align fontawesome icon to the middle of a line (already tried the existing solutions)

i would like you to help me get this working:

html code:

        <div class="mainMenu">
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/">Reviews <i class="fa fa-sort-desc"></i></a></li>
                <li><a href="/">News</a></li>
                <li><a href="/">Videos <i class="fa fa-sort-desc"></i></a></li>
                <li><a href="/">Forums</a></li>
            </ul>
        </div>

css code:

.mainMenu {
    overflow: hidden;
    float: right;
}

.mainMenu ul {
    overflow: hidden;
    list-style: none;
    padding: 0;
    margin: 0;
}

.mainMenu li {
    float: left;
}

.mainMenu li a {
    line-height: 65px;
    padding: 0 10px;
    margin-left: 10px;
}

here's a link to a jsfiddle: http://jsfiddle.net/shock/r5rd3v86/

Upvotes: 0

Views: 2915

Answers (4)

Joe Czucha
Joe Czucha

Reputation: 4295

You can also just tweak the positioning using a transform.

For example, with the following HTML:

<span class="fa fa-sort-asc"></span>
<span class="fa fa-sort-desc"></span>

Add the following to your stylesheet:

.fa-sort-asc {
  transform: translateY(3px);
}

.fa-sort-desc {
  transform: translateY(-2px);
}

Upvotes: 2

Franky238
Franky238

Reputation: 519

I cant comment so: Why you using line-height: 65px;? In jsfidle its like margin-top. try to use line-height equal to font-size and use vertical-align:middle

Upvotes: 0

Marc Audet
Marc Audet

Reputation: 46825

You can get precise control of the positioning by setting position: relative to the i element that contains the font-awesome icon. Adjust the position using the top offset with a positive or negative value.

.mainMenu {
    overflow: hidden;
    float: left;
}
.mainMenu ul {
    overflow: hidden;
    list-style: none;
    padding: 0;
    margin: 0;
    border: 1px dotted gray;
}
.mainMenu li {
    float: left;
    border: 1px dotted gray;
}
.mainMenu li a {
    line-height: 65px;
    padding: 0 10px;
    margin-left: 10px;
    text-decoration: none;
    border: 1px dashed gray;
}
.mainMenu li a i {
    position: relative;
    top: -2px;
}
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="mainMenu">
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/">Reviews <i class="fa fa-sort-desc"></i></a></li>
        <li><a href="/">News</a></li>
        <li><a href="/">Videos <i class="fa fa-sort-desc"></i></a></li>
        <li><a href="/">Forums</a></li>
    </ul>
</div>

Upvotes: 1

Tony Barnes
Tony Barnes

Reputation: 2643

If you set the vertical alignment of the icon, you can apply a minus margin to it:

.fa { 
  vertical-align: middle;
  margin-top: -10px;
}

Upvotes: 2

Related Questions