简体   繁体   中英

How to vertically align icon font

I am trying to vertically align font icons. I have tried vertical-align: middle but I always get a little align difference. The following example has 2 different ways to use the icons and they are not aligned correctly.

 div { font-size: 50px; font-family: helvetica, arial, sans-serif; text-transform: uppercase; background: yellow; } .cart { margin-top: 20px; } .cart:before { font-family: "fanatic-icons" !important; font-weight: normal; content: "b"; margin-right: 5px; vertical-align: middle; text-transform: none; }
 <link rel="stylesheet" type="text/css" href="https://fontastic.s3.amazonaws.com/PxvnwqrSXE7pXNDNDqGp4i/icons.css"> <div> <span class="icon icon-shopping-cart"></span> Shopping Cart </div> <div class="cart"> Shopping Cart </div>

例子

You can try vertical-align: text-bottom or vertical-align: text-top , depending on which one you feel is more vertically centered.

For your shopping cart icon, it seems text-top is most vertically centered.

Example at: https://jsfiddle.net/p3g189bg/

You can try for vertical-align:middle;

or line-height:1;

or using padding property you can set icon position

example using vertical:middle; property js fiddle: http://jsfiddle.net/vrcarwrj/

Try using line-height attribute

You can set it to 0.5,1,1.5 etc

Alternatively to the above, using the span element method you describe, you could relatively position the span tag, relative to its parent div element.

Like:

div{
 position: relative;
}    
span.icon-shopping-cart{
 position: relative;
 top: 5px;
}

You can try using valign: middle

and/or then setting the line-height to 1px, 1.5px, etc.

Another nowadays example via Flexbox.

 span { font-family: helvetica, arial, sans-serif; display: inline-flex; align-items: center; padding: 0 1rem; font-size: 3rem; line-height: 4rem; border: 1px solid #ffb0d1; } /* target all Font Awesome 5 <svg> tags */ .svg-inline--fa { padding-right: 1.5rem; }
 <script src="https://use.fontawesome.com/releases/v5.12.1/js/all.js"></script> <span> <i class="fas fa-shopping-cart"></i> Shopping Cart </span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM