【问题标题】:Remove trailing space when CSS pseudo-element ::after is not at the end of line当 CSS 伪元素 ::after 不在行尾时删除尾随空格
【发布时间】:2020-05-25 20:36:26
【问题描述】:

当我的::after 元素添加到一行中间而不是行尾时,会添加一个尾随空格。我尝试删除它。

通过将元素放在 inline-block 中确实会删除空白,但随后高度也会更改,我想防止这种情况发生。

new Vue({
  el: '#app',
});
.test {
  
  position: relative;
  padding: 0px 3px;
  z-index: 100 !important;
}

.test::after {
  content: "";
  width: 100%;
  height: 100%;
  z-index: -1;
  position: absolute;
  background: var(--color);
  -ms-transform: skewX(-10deg); /* IE 9 */
  -webkit-transform: skewX(-10deg); /* Safari 3-8 */
  transform: skewX(-10deg);
  top: 0px;
  left: 0px;
}
p {
  font-size : 12px;
}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app" style="font-size: 24px; padding: 10px">

#1 <span class="test" :style="'z-index: 100;--color:' + '#ff00ff' + ';'">
    test
  </span>
  <br>
  <br>
  <br>
#2 In a  <span class="test" :style="'z-index: 100;--color:' + '#ff00ff' + ';'">
    test
  </span> sentence.  





<p>
<br>
  <br>
In case #1 after element is at the end of the line and does not add a space.
<br>
<br>
<br>
In case #2 the element is in the middle of the line and add unwanted trailing space.
</p>
</div>

【问题讨论】:

  • 两个粉红色的盒子对我来说都是一样的。
  • 我尝试使用 firefox 和 chrome,我在 #2 的末尾有一个尾随空格。
  • 在 Firefox 中查看 #1 和 #2 在粉红色框中的test 之后都有一个空格。在 chrome #1 中没有。
  • 这很奇怪。你的版本是什么?
  • Firefox 72、Chrome 80(都在 macOS 10.15.3 上)

标签: javascript html css pseudo-element


【解决方案1】:

它是 HTML 中的换行符。当我删除文本和 &lt;/span&gt; 之间的换行符时,它似乎可以工作

new Vue({
  el: '#app',
});
.test {
  position: relative;
  padding: 0px 3px;
  z-index: 100 !important;
}

.test::after {
  content: "";
  width: 100%;
  height: 100%;
  z-index: -1;
  position: absolute;
  background: var(--color);
  -ms-transform: skewX(-10deg);
  /* IE 9 */
  -webkit-transform: skewX(-10deg);
  /* Safari 3-8 */
  transform: skewX(-10deg);
  top: 0px;
  left: 0px;
}

p {
  font-size: 12px;
}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app" style="font-size: 24px; padding: 10px">

  #1 <span class="test" :style="'z-index: 100;--color:' + '#ff00ff' + ';'">
    test
  </span>
  <br>
  <br>
  <br> #2 In a <span class="test" :style="'z-index: 100;--color:' + '#ff00ff' + ';'">test</span> sentence.





  <p>
    <br>
    <br> In case #1 after element is at the end of the line and does not add a space.
    <br>
    <br>
    <br> In case #2 the element is in the middle of the line and add unwanted trailing space.
  </p>
</div>

【讨论】:

  • 是的,这是一个解决方案,但它不是一个完美的解决方案。我想取消这种行为。
  • 没有直接的解决方案。当您在 HTML 中进行换行时,它会注册为空格。不管后面有多少空格,它都会注册一个空格。因此,当跨度关闭之前有换行符/空格时,它将具有文本“test”。并基于这些字符/宽度,样式将实现。
  • 这会在#1 中的test 之后呈现一个空格,但不会在#2 中呈现。
猜你喜欢
  • 2015-04-27
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多