html - 相对定位的 child 覆盖父轮廓

标签 html css

我遇到了以下问题。可以接收选项卡焦点的父元素内相对定位的子元素略微溢出父 div,并隐藏一些父元素的选项卡焦点轮廓。在 Chrome 上,这个轮廓被部分隐藏,而在 IE 上,这个轮廓被子元素完全隐藏。

请参阅附带的 JSFiddle:https://jsfiddle.net/55h4wtrc/7/ .

.parent {
  padding: 0;
  border: none;
  margin-bottom: 10px;
}

.child {
  background: #fff;
  border: 1px solid #eee;
  position: relative;
  height: 40px;
  line-height: 40px;
}
<div class='parent' tabindex='0'>
  <div class='child'>text</div>
</div>

<div class='parent' tabindex='0'>
  <div class='child'>text</div>
</div>

<div class='parent' tabindex='0'>
  <div class='child'>text</div>
</div>

.child 中删除 CSS 行 position: relative; 可修复此错误行为。

在我的例子中,我需要轮廓完全可见,我还需要子元素相对定位。我还需要子元素具有比其父元素更高的 z-index,因此修复此错误的 z-index 技巧将不起作用。

有什么想法吗?

最佳答案

原因

CSS Basic User Interface Level 3 specification 开始:

  1. Outlines do not take up space.

以后

The stacking of the rendering of these outlines is explicitly left up to implementations to provide a better user experience per platform. [...]

这意味着浏览器在渲染轮廓方面相当自由。如果在相关元素的边界内呈现它们,它们可能会被子元素覆盖。

缓解

您可以将自定义样式应用于轮廓,使用 outlineoutline-offset 属性在焦点期间手动设置颜色、样式和位置,如 this JSFiddle 所示,例如

.parent:focus {
  outline: hotpink dashed 2px;
  outline-offset: 2px;
}

如果这还不行,你不妨使用一个完全不同的工具,比如box-shadow,如this JSFiddle所示,例如

.parent:focus {
  outline: none;
  box-shadow: 0 0 2px 2px teal;
}

关于html - 相对定位的 child 覆盖父轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48818664/

相关文章:

html - 将鼠标悬停在侧边导航中的 mat-item 上时,工具提示不起作用

html - Ul Li 标签不能右对齐

css - 向 div 添加文本使 div 向下移动

jquery - 您可以为一组 div 创建彩盒幻灯片吗?

html - 网站的移动 View 菜单

html - 单选按钮选中的属性不起作用

javascript - AngularJS Collapse Expanded ng-在另一个展开上重复项目

html - css 左右简单菜单

javascript - Internet Explorer 不剪切溢出的内容

javascript - 如何格式化 bootstrap 4 按钮以在按钮的不同位置显示部分文本