【问题标题】:Remove border when hover on item将鼠标悬停在项目上时删除边框
【发布时间】:2019-11-09 01:00:27
【问题描述】:

我希望在将鼠标悬停在每个项目上时移除红色边框。

您可以从我附在此处的jsFiddle 中看到。它只适用于左侧的第一个 div,也适用于右侧的其余 div。

我这样做的原因是因为我希望它灵活,以便我可以在 HTML 上添加或删除而不影响 CSS 部分。

.wrapper {
  height: 100px;
  display: flex;
  background-color: lightblue;
}
.item {
  background-color: lightgreen;
  flex: 0 0 15%;
}
.item-left + .item-right {
  margin-left: auto;
}
.item-left{
	border-left:1px solid red;
}
.item-left:first-of-type{
	border:none;
}
.item-left:last-of-type{
	border-right:1px solid red;
}
.item-right{
	border-left:1px solid red;
}
.item:hover{
	/* box-shadow: x y blur spread color; */
	box-shadow:0px 0px 0px -1px rgba(0, 0, 0, 0.2), 0px 0px 10px 0px rgba(0, 0, 0, 0.19);
	z-index:1;
}
.item:hover + .item.item-left, .item.item-right {
    border:none;
}
<div id="bottom" class="bottom">
  <div class="wrapper">
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
  </div>
</div>

感谢您的帮助!

【问题讨论】:

  • 我只能在小提琴的第二个和第三个元素上看到红色边框。那么移除不存在的红色边框是什么意思?
  • 我希望将鼠标悬停在第二个和第三个边框上时将其删除。就像您将鼠标悬停在第一个项目上一样。
  • 你需要纯css解决方案?
  • 是的。纯 CSS。从上面的脚本可以看出,它只适用于它之后的元素,而不是前一个元素。
  • @Zze 在这种情况下,两组项目之间存在间隙,这使兄弟选择器方法变得复杂。

标签: html css


【解决方案1】:

在你的 CSS 中使用它

.item:hover{
  border:none;
}

.wrapper {
  height: 100px;
  display: flex;
  background-color: lightblue;
}
.item {
  background-color: lightgreen;
  flex: 0 0 15%;
}
.item-left + .item-right {
  margin-left: auto;
}
.item-left{
	border-left:1px solid red;
}
.item-left:first-of-type{
	border:none;
}
/*.item-left:last-of-type{
	border-right:1px solid red;
}*/

.last-left{
	border-right:1px solid red;
}

.item-right{
	border-left:1px solid red;
}
.item:hover{
	/* box-shadow: x y blur spread color; */
	box-shadow:0px 0px 0px -1px rgba(0, 0, 0, 0.2), 0px 0px 10px 0px rgba(0, 0, 0, 0.19);
	z-index:1;
}
.item:hover + .item.item-left, .item.item-right, .item:hover {
    border:none;
}
<div id="bottom" class="bottom">
  <div class="wrapper">
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-left last-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
  </div>
</div>

【讨论】:

  • 您可以考虑使用border-color:transparent 而不是border:none,这样标签的位置就不会在悬停时移动。
  • 顺便说一句,我的 css (.item-left:last-of-type) 有一行不起作用。你介意帮我看看吗?我想在最后一项左侧添加边框,但它似乎不起作用。
  • @showdev 谢谢你。我会改变我的剧本。 :-)
  • @StacieT。 last-of-type 不考虑类,只考虑元素类型(例如&lt;div&gt;)。见How do I select the “last child” with a specific class name in CSS?
  • @showdev 我尝试了所有可能的方法,即border:0px solid #fff;、border:none 和border:1px solid transparent;,但几乎你的建议也可以。
【解决方案2】:

相邻项目的边框can be tricky,尤其是当将鼠标悬停在某个项目上时这些边框发生变化时。

使用outline 代替border 可能有效。轮廓“从不占用空间,因为它们是在元素内容之外绘制的”,因此它们不会被加倍(参见 outline)。

轮廓出现在元素的所有边上,而不是可以放置在特定边(例如左、右、上、下)的边框。在.wrapper 上设置overflow:hidden 会隐藏外边缘的轮廓。这样,只有项目之间的轮廓是可见的。

.wrapper {
  height: 100px;
  display: flex;
  background-color: lightblue;
  overflow: hidden;
}

.item {
  background-color: lightgreen;
  flex: 0 0 15%;
  outline: 1px solid red;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item-left+.item-right {
  margin-left: auto;
}

.item:hover {
  z-index: 1;
  outline: none;
  background-color: mediumseagreen;
  color: white;
  box-shadow: 0px 0px 0px -1px rgba(0, 0, 0, 0.2), 0px 0px 10px 0px rgba(0, 0, 0, 0.19);
  cursor: pointer;
}
<div id="bottom" class="bottom">
  <div class="wrapper">
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-left">
      <div class="item-label">Left</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
    <a class="item item-right">
      <div class="item-label">Right</div>
    </a>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多