如何使用<i>元素悬停容器div

时间:2017-05-22 18:37:37

标签: html css css3 flexbox css-transitions

我想我正在尝试做一个非常简单的效果。我有一个带有4个图标的菜单,我希望描述在悬停时向右滑动。我不确定我能用CSS做这个,因为div在第一个div的结束标记之后,我尝试了+但没有发生任何事情。

这是display:flex的问题吗?我得说我不习惯这样做。

我的代码在这里,我降低了#mainicons i的不透明度,这样你就可以看到发生了什么,但它应该是1.

#mainicons {
  position: fixed;
  top: 250px;
  left: 0px;
  text-align: center;
}

#mainicons i {
  opacity: 0.5;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-top: 40;
  height: 40px;
  width: 50px;
  padding: 10px;
  color: white;
  background: black;
  text-align: center;
  font-size: 15px;
  line-height: 40px;
  -webkit-transition: all .5s ease;
  -moz-transition: all .5s ease;
  -o-transition: all .5s ease;
  transition: all .5s ease;
}

#mainicons i:hover {
  border: 1.5px solid black;
  color: black;
  background: white;
}

#icondesc {
  position: fixed;
  top: 250px;
  left: 0px;
  text-align: center;
}

#icondesc i {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  top: 50%;
  margin: 80;
  color: black;
  background: white;
  width: 70px;
  height: 50px;
  line-height: 40px;
  padding-left: 0px;
  font-size: 15px;
  -webkit-transition: all .5s ease;
  -moz-transition: all .5s ease;
  -o-transition: all .5s ease;
  transition: all .5s ease;
}

#mainicons i:hover+#icondesc i {
  margin-left: 50px;
}
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<div id="icondesc">
  <i>Home</i>
  <i>Ask</i>
  <i>Request</i>
  <i>Archive</i>
</div>
<div id="mainicons">
  <a href="/"><i class="fa fa-home"></i></a>
  <a href="/ask"><i class="fa fa-envelope"></i></a>
  <a href="/submit"><i class="fa fa-pencil "></i></a>
  <a href="/archive"><i class="fa fa-clock-o"></i></a>
</div>

感谢您的回复!

1 个答案:

答案 0 :(得分:2)

问题与flex无关。您正在尝试根据稍后在DOM中的元素的悬停来定位元素。这基本上是尝试创建previous sibling selector,这在CSS中本身不可能。

我会将文本描述放在带有图标的同一容器中,并使用绝对定位来实现过渡效果。这是一个草图:

&#13;
&#13;
#mainicons {
  position: fixed;
  top: 250px;
  left: 0px;
  text-align: center;
  display: flex;
  flex-direction: column;
}

#mainicons>a {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

#mainicons i {
  opacity: 1;
  height: 40px;
  width: 50px;
  padding: 10px;
  color: white;
  background: black;
  text-align: center;
  font-size: 15px;
  line-height: 40px;
  transition: all .5s ease;
}

#mainicons i:hover {
  border: 1.5px solid black;
  color: black;
  background: white;
}

#mainicons span {
  color: black;
  background: white;
  width: 70px;
  font-size: 15px;
  transition: all .5s ease;
  position: absolute;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  left: -100px;
  opacity: 0;
}

#mainicons a:hover>span {
  opacity: 1;
  left: 70px;
  transition: .5s;
  border: 1px solid black;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="mainicons">
  <a href="/"><i class="fa fa-home"></i><span>Home</span></a>
  <a href="/ask"><i class="fa fa-envelope"></i><span>Ask</span></a>
  <a href="/submit"><i class="fa fa-pencil "></i><span>Request</span></a>
  <a href="/archive"><i class="fa fa-clock-o"></i><span>Archive</span></a>
</div>
&#13;
&#13;
&#13;

https://jsfiddle.net/qeoyyryp/1/