悬停时 CSS 边框出现问题


Issue with the CSS border on hover

我正在自定义样式,但边框有问题。当我飞过元素时,它不是静态的(由于边框,它向右移动)。

我在IP上也注意到了这个问题。董事会和我找不到解决方案:http://screencast.com/t/49DgJmXuCN0v 并删除边框,一切都很完美:http://screencast.com/t/n3JVAYFQRxK

如果有人可以帮助我,谢谢。

只需将box-sizing: border-box;添加到您的元素中即可。 有关更多信息 http://www.w3schools.com/cssref/css3_pr_box-sizing.asp

边框通常会增加元素所占区域的尺寸。这会导致后续内容向前推送,尽管确切的效果取决于许多设置。试图避免这种情况会导致问题,因为如果在添加边框时总维度保持不变,上下文区域维度就会缩小。但还有其他几种选择:

  1. 最初设置一个透明边框,并在悬停时仅更改其颜色。
  2. 最初设置与元素背景颜色相同的颜色。这只是上述内容的变体。
  3. 根本不设置边框,而是轮廓。轮廓出现在其他任何内容的顶部(在 z 方向),可能覆盖某些内容,但不会更改布局。这比前两个选项稍微简单一些,但浏览器支持稍微有限一些(你知道,旧版本的IE)。

<style>
body {
  background: white;
  color: black;
}
.a:hover { 
  border: solid red medium;
}
.b {
  border: solid transparent medium;
}
.b:hover { 
  border-color: red;
}
.c {
  border: solid white medium;
}
.c:hover { 
  border-color: red;
}
.d:hover { 
  outline: solid red medium;
}
</style>
<p><span class=a>Illustrating the problem.</span> What happens on mouseover?
<p><span class=b>This has transparent border initially.</span> What happens on mouseover?
<p><span class=c>This has white border initially.</span> What happens on mouseover?
<p><span class=d>No border, but outline.</span> What happens on mouseover?
<p>That’s all folx!