【问题标题】:Weird artefact showing behind only one of before/after pseudo selector in my breadcrumbs在我的面包屑中仅显示之前/之后伪选择器之一的奇怪伪影
【发布时间】:2019-04-29 20:24:42
【问题描述】:

我正在尝试使用箭头分隔线创建 3 部分面包屑类型元素。这是一个简单的模型。

在第 2 部分和第 3 部分之间显示了一个奇怪的人工制品。在这种情况下,第 2 部分的蓝色箭头左侧(稍微)可以看到第 3 部分的红色背景,但是第 2 部分的背景不会出现同样的问题干扰第 1 节箭头。谢天谢地,但奇怪的是。对我来说,这也只发生在 Chrome for Mac 上(浏览器窗口也没有放大/缩小)。还没有测试过 Windows 等。关于如何解决这个奇怪的问题有什么建议吗?

第 2 部分和第 3 部分之间发生的奇怪人工制品(红色垂直线)的放大视图:

在第 1 部分和第 2 部分之间没有出现这种奇怪的人工制品。

密码笔:

https://codepen.io/reacting/pen/xeewdO

html:

<div class="container">
  <div class="section">section one</div>
  <div class="section two">section two</div>  
  <div class="section">section three</div> 
</div>

css/scss:

.container {
  background-color: white;
  border: 1px solid grey;
  box-sizing: border-box;
  width: 100%;
  display: flex;
}
.section {
  flex: 1;
  position: relative;
  height: 100px;
  background-color: black;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  &:before {
    content:"";
    background-color: grey;
    -webkit-clip-path: polygon(0% 100%, 100% 50%, 0% 0%);
    clip-path: polygon(0% 100%, 100% 50%, 0% 0%);
    z-index: 1;
    position: absolute;
    right: -26px;
    top: 0;
    width: 25px;
      height: 100px;
  }
  &:after {
    content:"";
    background-color:black;
     -webkit-clip-path: polygon(0% 100%, 100% 50%, 0% 0%);
    clip-path: polygon(0% 100%, 100% 50%, 0% 0%);
    z-index: 2;
    position: absolute;
    right: -25px;
    top: 0;
    width: 25px;
    height: 100px;
  }
  &:last-of-type {
    background-color: red;
    color: black;
  }
  &:last-of-type:before {
   display: none;
  }
  &:last-of-type:after {
   display: none;
  }
  &.two {
   background-color: blue;
    &:after {
      background-color: blue;
    }
  }
}

body {
  background-color: #333;
}

我不只是想将 before/after 伪选择器的 right: 属性更改为小于 1 像素,因为这只是感觉不妥和错误。

非常感谢!

编辑:我想知道这个问题是否与我的 Mac 显示器的高分辨率有关 - 因为当我稍微调整 chrome 浏览器窗口的大小时,问题来来去去,并且在第 1/2 节或第 1/2 节都发生变化和 2/3 或没有。取决于浏览器窗口的大小。但奇怪的是,在 Firefox 和 Safari 中,拖动窗口时根本不会发生这种情况。

【问题讨论】:

  • 这是一个亚像素渲染,您可以通过小重叠修复
  • 为什么更改 right 属性会让人觉得很麻烦?

标签: html css clip-path


【解决方案1】:

您可以优化代码并在不需要伪元素的元素上考虑clip-path,然后考虑一些背景颜色来模拟边框

.container {
  background-color: white;
  border: 1px solid grey;
  box-sizing: border-box;
  display: flex;
  height: 100px;
}

.section {
  flex: 1;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left:-25px; /* Create an overlap with the previous element */
  /* the clip path (note the 25px that is the same as margin*/
  -webkit-clip-path: polygon(100% 0, 100% 50%, 100% 100%, 0% 100%, 25px 50%, 0% 0%);
  clip-path: polygon(100% 0, 100% 50%, 100% 100%, 0% 100%, 25px 50%, 0% 0%);
  /* the border (note the 25px in the gradient)*/
  border-left:3px solid grey; /* this will push the background and control the thickness of the border*/
  background:
    linear-gradient(to top right,    grey 48%,transparent 50%) top left   /25px 50%,
    linear-gradient(to bottom right, grey 48%,transparent 50%) bottom left/25px 50%;
  background-repeat:no-repeat;
  background-color: black;
}
.section:last-of-type {
  background-color: red;
  color: black;
}
.section:first-of-type {
  /* Remove everything from the first element */
  clip-path:none; 
  margin-left:0;
  border-left:0;
  background:black;
}
.section.two {
  background-color: blue;
}

body {
  background-color: #333;
}
<div class="container">
  <div class="section">section one</div>
  <div class="section two">section two</div>
  <div class="section">section three</div> 
</div>

【讨论】:

  • OP 的角度也有灰色边框。
  • 真的,谢谢你,但我还想要在每个部分(箭头形状的边框)之间有一个轻微的灰色边框(与容器边框的颜色相同),这就是我在之前/之后选择的原因.
  • 谢谢,这看起来绝对完美。惊人。如果我想让那个灰色边框变窄一点,我将如何更改线性渐变代码?无需更改演示,只要您可以在 cmets 中告诉我即可。
  • @MartinS 添加了一些 cmets。你只需要调整边框的值(这里是3px)它会推动背景来定义厚度..改变值来看看效果
  • @TemaniAfif - 谢谢,对不起,我完全错过了左边框线。
猜你喜欢
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
相关资源
最近更新 更多