位置字体真棒跨度图标上方的锚文本

时间:2017-05-02 09:55:32

标签: html css fonts font-awesome

我正在尝试制作一些文本位于字体真棒图标下方的按钮,其中两个都位于列表项的中央,我需要锚元素,其中还包含字体真棒图标的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;
}

3 个答案:

答案 0 :(得分:0)

#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>

答案 1 :(得分:0)

问题来自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>

答案 2 :(得分:0)

只需将position: absolute; top: 40px;添加到font-awesome图标

即可