将字体真棒跨度图标放置在锚点内的文本上方
Posted
技术标签:
【中文标题】将字体真棒跨度图标放置在锚点内的文本上方【英文标题】:Position font awesome span icon above text within an anchor 【发布时间】:2017-09-29 19:17:43 【问题描述】:我正在尝试制作一些按钮,其文本位于字体真棒图标下方,两者都位于列表项的中心位置,我需要锚元素,它还包含字体真棒图标的 span 元素,以填充列表项的整个大小。
我可以用锚点填充列表项没有问题,但是我无法将图标的跨度定位在包含它的锚点中的文本上方。
JSFiddle:https://jsfiddle.net/qod142fz/.
html:
<div id="sidebarPrimary">
<ul id="sidebarPrimaryNav">
<li class="navButton"><a href="#"><span class="fa fa-home"></span>Home</a></li>
<li class="navButton"><a href="#"><span class="fa fa-user"></span>Personal Details</a></li>
<li class="navButton"><a href="#"><span class="fa fa-briefcase"></span>Company</a></li>
<li class="navButton"><a href="#"><span class="fa fa-gbp"></span>Invoices</a></li>
<li class="navButton"><a href="#"><span class="fa fa-address-book"></span>Contacts</a></li>
<li class="navButton"><a href="#"><span class="fa fa-minus"></span>Expenses</a></li>
<li class="navButton"><a href="#"><span class="fa fa-list"></span>Payslips</a></li>
<li class="navButton"><a href="#"><span class="fa fa-cog"></span>Settings</a></li>
</ul>
</div>
CSS:
/* SIDEBAR PRIMARY */
#sidebarPrimary
position: fixed;
width: 15vw;
height: 100%;
top: 0;
left: 0;
background: #2F323E;
#sidebarPrimary > ul
margin: 0;
padding: 0;
list-style-type: none;
#sidebarPrimary > ul > li.navButton
width: 100%;
height: 15vw;
#sidebarPrimary > ul > li.navButton > a
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
color: #687381;
font-size: 12px;
font-weight: 500;
transition: all linear 0.05s;
-webkit-transition: all linear 0.05s;
-moz-transition: all linear 0.05s;
#sidebarPrimary > ul > li.navButton > a:hover
text-decoration: none;
color: #fff;
background: #E95656;
#sidebarPrimary > ul > li.navButton > a > span
display: block;
text-align: center;
margin-bottom: 5px;
【问题讨论】:
【参考方案1】:#sidebarPrimary
position: fixed;
width: 15vw;
height: 100%;
top: 0;
left: 0;
background: #2F323E;
#sidebarPrimary>ul
margin: 0;
padding: 0;
list-style-type: none;
#sidebarPrimary>ul>li.navButton
width: 100%;
height: 15vw;
#sidebarPrimary>ul>li.navButton>a
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
color: #687381;
font-size: 12px;
font-weight: 500;
transition: all linear 0.05s;
-webkit-transition: all linear 0.05s;
-moz-transition: all linear 0.05s;
#sidebarPrimary>ul>li.navButton>a:hover
text-decoration: none;
color: #fff;
background: #E95656;
#sidebarPrimary>ul>li.navButton>a>span
display: block;
text-align: center;
margin-bottom: 5px;
width: 20px;
height: 20px;
background-color: red;
position: absolute;
top: 10px;
<div id="sidebarPrimary">
<ul id="sidebarPrimaryNav">
<li class="navButton"><a href="#"><span class="fa fa-home"></span>Home</a></li>
<li class="navButton"><a href="#"><span class="fa fa-user"></span>Personal Details</a></li>
<li class="navButton"><a href="#"><span class="fa fa-briefcase"></span>Company</a></li>
<li class="navButton"><a href="#"><span class="fa fa-gbp"></span>Invoices</a></li>
<li class="navButton"><a href="#"><span class="fa fa-address-book"></span>Contacts</a></li>
<li class="navButton"><a href="#"><span class="fa fa-minus"></span>Expenses</a></li>
<li class="navButton"><a href="#"><span class="fa fa-list"></span>Payslips</a></li>
<li class="navButton"><a href="#"><span class="fa fa-cog"></span>Settings</a></li>
</ul>
</div>
【讨论】:
【参考方案2】:问题来自flex
。我建议将另一个 div
包裹在应该在 a
标签内居中的元素周围
/* SIDEBAR PRIMARY */
#sidebarPrimary
position: fixed;
width: 15vw;
height: 100%;
top: 0;
left: 0;
background: #2F323E;
#sidebarPrimary > ul
margin: 0;
padding: 0;
list-style-type: none;
#sidebarPrimary > ul > li.navButton
width: 100%;
height: 15vw;
#sidebarPrimary > ul > li.navButton > a
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
color: #687381;
font-size: 12px;
font-weight: 500;
text-decoration: none;
transition: all linear 0.05s;
-webkit-transition: all linear 0.05s;
-moz-transition: all linear 0.05s;
#sidebarPrimary > ul > li.navButton > a:hover
text-decoration: none;
color: #fff;
background: #E95656;
#sidebarPrimary > ul > li.navButton > a .fa
display: block;
height: 1em;
margin: 0 auto 5px;
text-align: center;
width: 1em;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div id="sidebarPrimary">
<ul id="sidebarPrimaryNav">
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-home"></span>Home
</div>
</a></li>
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-user"></span>Personal Details
</div>
</a></li>
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-briefcase"></span>Company
</div>
</a></li>
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-gbp"></span>Invoices
</div>
</a></li>
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-address-book"></span>Contacts
</div>
</a></li>
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-minus"></span>Expenses
</div>
</a></li>
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-list"></span>Payslips
</div>
</a></li>
<li class="navButton"><a href="#">
<div class="navButtonContent">
<span class="fa fa-cog"></span>Settings
</div>
</a></li>
</ul>
</div>
【讨论】:
【参考方案3】:只需将position: absolute; top: 40px;
添加到字体很棒的图标中
【讨论】:
以上是关于将字体真棒跨度图标放置在锚点内的文本上方的主要内容,如果未能解决你的问题,请参考以下文章