CSS - Center align text with icon

烈酒焚心 提交于 2019-11-27 06:24:14

问题


Using material icons with CSS, I have the following code that renders a link with icon and text.

<a href="#"><i class="material-icons">group_work</i>Groups</a>

As you can see in the image below, the text seems to be sinking down.. I would like them to be vertically aligned center together. How can i achieve this?

PS. (Not a designer!)


回答1:


To vertically center elements, you can use the vertical-algin: middle; rule. In your case, that would most propably be:

.material-icons {
  vertical-align: middle;
}

Here is an example with your dark button:

.material-icons {
  vertical-align: middle;
  margin-right: 5px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<a href="#" class="grey darken-3 btn"><i class="material-icons">group_work</i>Groups</a>



回答2:


<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<a href="#" class="grey darken-4 btn"><i class="material-icons left">group_work</i>Groups</a>



回答3:


vertical-align:middleapplied to the iconi` can be the simplest option but results can be inconsistent.

I have found better results with setting the link to display:inline-flex but the dfference is quite subtle.

vertical-align can still be used as a fallback for non-supporting browsers.

a {
  margin: 1em;
  display: inline-block;
}
a i {
  vertical-align: middle;
}
a.flex {
  display: inline-flex;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">


<a href="#" class=""><i class="material-icons">group_work</i>Groups</a>
<br/>
<a href="#" class="flex"><i class="material-icons">group_work</i>Groups</a>



回答4:


a {
  display: inline-flex;
  align-items: center;
}

Will do the trick ;)



来源:https://stackoverflow.com/questions/39979775/css-center-align-text-with-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!