CSS 点领导者位于复杂背景的第一行

pan*_*fil 17 html css flexbox

我需要使用 CSS 在第一行文本之后实现点引导符,以便在纹理/图像背景上使用它。

预期行为:

在此输入图像描述

我在互联网上看到了一些点领导者的实现。他们都使用以下技术之一:

  1. ::点+溢出之后
<div class=item1>
  <span class=text1>
    Text
  </span>
  <span class=price1>
    $100
  </span>
</div>
Run Code Online (Sandbox Code Playgroud)
<div class=item1>
  <span class=text1>
    Text
  </span>
  <span class=price1>
    $100
  </span>
</div>
Run Code Online (Sandbox Code Playgroud)
  1. 绝对点+白色背景
<div class=item2>
  <span class=text2>
    <span class=bg2>
      Text
    </span>
  </span>
  <span class=price2>
    <span class=bg2>
      $100
    </span>
  </span>
</div>
Run Code Online (Sandbox Code Playgroud)
.item1 {
  display: flex;
}
.text1 {
  flex-grow: 1;
  position: relative;
  overflow: hidden;
}
.text1::after {
  content: '';
  position: absolute;
  bottom: 0;
  border-bottom: 1px dotted grey;
  width: 100%;
}
.price1 {
  align-self: flex-end;
}

Run Code Online (Sandbox Code Playgroud)
  1. 愚蠢的弹性盒子
<div class=item3>
  <span class=text3>
    Text
  </span>
  <span class=dots3></span>
  <span class=price3>
    $100
  </span>
</div>
Run Code Online (Sandbox Code Playgroud)
<div class=item2>
  <span class=text2>
    <span class=bg2>
      Text
    </span>
  </span>
  <span class=price2>
    <span class=bg2>
      $100
    </span>
  </span>
</div>
Run Code Online (Sandbox Code Playgroud)

它们都显示在这里:https://jsfiddle.net/rzmLg4yu/62/

在我的例子中,这些技术中的每一种都有其自身的缺陷。

  1. 最后一行有点领导者。
  2. 不适用于复杂的背景,即使使用混合混合模式(取消注释 bg 行即可查看)。 在此输入图像描述
  3. 由于换行而存在较大的白色间隙(调整大小即可查看)。切换到网格是没有用的。 在此输入图像描述

对于这个案例还有更多的解决方案吗?

onk*_*kar 6

可以改进技术 3 以消除包裹间隙:

body {
  color: grey;
}

.width {
  width: 300px;
  padding: 5px;
  resize: horizontal;
  overflow: hidden;
  abackground: linear-gradient(45deg, yellow, black);
  background-repeat: repeat-x;
  background-size: 200px 100%;
}

.item3 {
  display: flex;
  align-items: flex-start;
}

.dots3 {
  flex-grow: 1;
  border-bottom: 1px dotted grey;
  min-width: 10px;
  height: 1em;
}

.text3 {
  padding-right: 0px;
  text-align: justify; /* <--- */
}
Run Code Online (Sandbox Code Playgroud)
<div class=width>

  <div class=item3>
    <span class=text3>
        3 Text text
      </span>
    <span class=dots3></span>
    <span class=price3>
        $100
      </span>
  </div>
  <div class=item3>
    <span class=text3>3 Text text text text text text text text text text text text text text text text</span>
    <span class=dots3></span>
    <span class=price3>
        $100
      </span>
  </div>
Run Code Online (Sandbox Code Playgroud)
这里text-align: justify;将均匀地间隔单词。


我认为你对第二种技术的问题是因为白色背景颜色,对吧?
因为.bg2 {background: white;...}
否则,您应该使用以下方法mix-blend-mode: lighten;来演示该问题:

body {
  color: grey;
  padding: 20px;
}

.width {
  width: 230px;
  padding: 5px;
  resize: horizontal;
  overflow: auto;
  background: linear-gradient(90deg, yellow, black);
  background-repeat: repeat-x;
  background-size: 200px 100%;
}

div>div {
  background-color: white;
  border: 2px solid green;
}

.simpleDiv1 {
  mix-blend-mode: darken;
}

.simpleDiv2 {
  margin-top: 20px;
  mix-blend-mode: lighten;
}
Run Code Online (Sandbox Code Playgroud)
<div class=width>
  <div class=simpleDiv1>darken blend. Simple text without any styles and stuff.</div>
  <div class=simpleDiv2>lighten blend. Simple text without any styles and stuff.</div>
</div>
Run Code Online (Sandbox Code Playgroud)

如果您使用变暗混合,那么任何普通文本都会随该背景消失,更不用说点引线了。


无背景白的技术二:

body {
  color: grey;
  padding: 20px;
}

.width {
  width: 230px;
  padding: 5px;
  resize: horizontal;
  overflow: hidden;
  abackground: linear-gradient(90deg, yellow, black);
  background-repeat: repeat-x;
  background-size: 200px 100%;
}

.item2 {
  display: flex;
  justify-content: space-between;
  position: relative;
  mix-blend-mode: darken;
  text-decoration-line: underline;
  text-decoration-style: solid;
  text-decoration-color: white;
  text-decoration-thickness: 2px;
}

.item2::before {
  content: '\00a0';
  height: calc(1em - 0.5px);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  border-bottom: 1px dotted gray;
  z-index: -1;
}
Run Code Online (Sandbox Code Playgroud)
<div class=width>

  <div class=item2>
    <span class=text2>
        2 Text text
      </span>
    <span class=price2>
        $100
      </span>
  </div>
  <div class=item2>
    <span class=text2>
        2 Text text text text text text text text text text text text text text text text
      </span>
    <span class=price2>
        $100
      </span>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在这里,我使用白色下划线而不是整个白色背景来隐藏点引线。但在使用某些混合模式时,您会得到白色下划线。


如果我们愿意在换行时打破语言的话,技术三可以得到改进:

body {
  color: grey;
}

.width {
  width: 300px;
  padding: 5px;
  resize: horizontal;
  overflow: hidden;
  abackground: linear-gradient(45deg, yellow, black);
  background-repeat: repeat-x;
  background-size: 200px 100%;
}

.item3 {
  display: flex;
  align-items: flex-start;
}

.dots3 {
  flex-grow: 1;
  border-bottom: 1px dotted grey;
  min-width: 10px;
  height: 1em;
}

.text3 {
  padding-right: 0px;
  word-break: break-all;
}
Run Code Online (Sandbox Code Playgroud)
<div class=width>

  <div class=item3>
    <span class=text3>
        3 Text text
      </span>
    <span class=dots3></span>
    <span class=price3>
        $100
      </span>
  </div>
  <div class=item3>
    <span class=text3>3 Text text text text text text text text text text text text text text text text</span>
    <span class=dots3></span>
    <span class=price3>
        $100
      </span>
  </div>
Run Code Online (Sandbox Code Playgroud)

在这里,我不是在单词边界上换行,而是使用word-break: break-all. 如果你的字体大小是 1em,那么这样最大间隙将小于 1em,因为浏览器将无法容纳小于 1em 空间的字母。