gpt4 book ai didi

html - 重叠跨度在悬停时没有位移

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:54 25 4
gpt4 key购买 nike

好吧,这是用于文本的,这是我正在尝试的代码:

<style>
#items {
width:400px;
padding: 10px;
}

.esp {
float: left;
background-color: yellow;
position:relative;
z-index:0;
}
.le {
float: left;
position:relative;
z-index:0;

}

.esp:hover{
background-color: blue;
z-index:1;
/*width: 50px;*/
border: 20px solid;
}
</style>
<html>

<div id="items">

<span class="le">fist</span>
<span class="le">fist</span>
<span class="le">fist</span>
<span class="le">fist</span>
<span class="le">fist</span>
<span class="esp">second</span>
<span class="le">third</span>
<span class="le">fist</span>
<span class="esp">second</span>
<span class="le">third</span>
<span class="le">fist</span>
<span class="esp">second</span>
<span class="le">third</span>
<span class="le">third</span>
<span class="le">third</span>
<span class="le">third</span>

</div>


</html>

我正在寻找的是避免其他跨度的位移,似乎 z-index 属性不起作用。是这样的: enter image description here

感谢您的帮助。

最佳答案

问题是您增加了 span 框的大小,但没有告诉其他元素忽略它。 relative 仅允许您相对定位 - 它不会从流程中移除,而 z-index 仅在元素显示在您的上方或下方时才执行某些操作 - 它不影响 x/y 定位。但是,我们需要将其设置得高于我们的弟弟妹妹,以便在他们面前显示我们的轮廓效果。

与边框宽度相同的负边距可以解决这个问题。

另一个更 DRY 的解决方案是使用 outlinebox-shadow 而不是 border,因为 outlinebox-shadow 绘制在框外。

请注意,outline 也会在子项周围画出轮廓,因此如果您将任何子项绝对定位到他们在父项之外的位置,那么也会在他们周围绘制框。但是,对于您要尝试执行的操作,它在语义上是最正确的。

#items { 
width:400px;
padding: 10px;
}

.esp {
float: left;
background-color: yellow;
position:relative;
}
.le {
float: left;
position:relative;
z-index:0;

}

/*border-neg-margins*/
/*dis: neg margins*/
.bnm:hover{
background-color: blue;
z-index:1;
border: 20px solid #556;
margin: -20px;
}

/*outline*/
/*adv: no negative margins, most semantically correct*/
/*dis: will expand to outline any children that exceed the parent's size*/
.ol:hover{
background-color: blue;
z-index:1;
outline: 20px solid #556;
}

/*box-shadow*/
/*adv: no neg margins, only outlines the box*/
/*dis: not really semantically correct*/
.bs:hover{
background-color: blue;
z-index:1;
box-shadow: 0 0 0 20px #556;
}
The first one uses Border and Negative Margins<br />
The second uses Outline<br />
The third uses Box-Shadow<br />
Color used is #556 so any sibling text appearing on top of it would be visible<br />
<span class="le">fist</span>
<span class="le">fist</span>
<span class="le">fist</span>
<span class="le">fist</span>
<span class="le">fist</span>
<span class="esp bnm">second</span>
<span class="le">third</span>
<span class="le">fist</span>
<span class="esp ol">second</span>
<span class="le">third</span>
<span class="le">fist</span>
<span class="esp bs">second</span>
<span class="le">third</span>
<span class="le">third</span>
<span class="le">third</span>
<span class="le">third</span>

关于html - 重叠跨度在悬停时没有位移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36701069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com