所以我有一些文本,我将其设为upright,我希望每个字母后面都有不同的background.

对于我的实际用例,背景列表是由一个函数随机生成的,该函数获得字母的数量.

现在的问题是.how much vertical space does a letter occupy?我在这里使用等宽字体,并将line-height设置为1(这是我可以 Select 的值,所以我 Select 了1,因为我认为这会简化事情).

以下是代码的模拟萨斯版本:

$c: #448aff, #1565c0, #009688, #8bc34a, #ffc107, #ff9800, #f44336, #ad1457;
$n: length($c);
$m: .5*$n;
$l: ();

@for $i from 0 to $m {
    $l: $l, 
        conic-gradient(from random(360)*1deg 
                at random(100)*1% random(100)*1%, 
                nth($c, $i + 1), nth($c, $i + $m + 1), 
                nth($c, $i + 1))
            0 $i*1em
}

div {
    box-shadow: 0 0 0 6px;
    background: $l;
    background-repeat: no-repeat;
    background-size: 100% 1em;
    font: 900 clamp(1em, 18vmin, 18em)/ 1 monospace;
    writing-mode: vertical-lr;
    text-orientation: upright;
    text-transform: uppercase
}

所以,如果line-height1,我会认为一个字母占1lh1em...但这还不够!

div {
  box-shadow: 0 0 0 6px;
  background: 
    conic-gradient(from 37deg at 54% 48%, #448aff, #ffc107, #448aff), 
    conic-gradient(from 264deg at 8% 31%, #1565c0, #ff9800, #1565c0) 0 1em, 
    conic-gradient(from -61deg at 92% 2%, #009688, #f44336, #009688) 0 2em, 
    conic-gradient(from -5deg at 41% 95%, #8bc34a, #ad1457, #8bc34a) 0 3em;
  background-repeat: no-repeat;
  background-size: 100% 1em;
  font: 900 clamp(1em, 18vmin, 18em)/1 monospace;
  writing-mode: vertical-lr;
  text-orientation: upright;
  text-transform: uppercase
}
<div>play</div>

What am I missing? How much vertical space does a letter occupy in this scenario? How do I size and position my gradients so that I have one behind each letter?


将文本拆分成字母,每个字母都包装在一个元素中,每个元素都有自己的background个元素,在这里绝对不能这样做.对那个不感兴趣.

推荐答案

line-height不会工作,因为它会调整水平空间,而不是垂直的

div {
  box-shadow: 0 0 0 6px;
  font: 900 clamp(1em, 18vmin, 18em)/4 monospace;
  writing-mode: vertical-lr;
  text-orientation: upright;
  text-transform: uppercase
}
<div>play</div>

因此,我认为您将保留默认行为1.2em,但不确定是否可以依赖它,因为它取决于字体和浏览器实现

取决于用户代理.桌面浏览器(包括Firefox)使用的默认值约为1.2,具体取决于元素的字体系列.ref

如果你把它和the use of percentage on background-position结合在一起,你将只有一个值可以"猜测",所以如果你知道字体家族,它就可以工作.

div {
  box-shadow: 0 0 0 6px;
  background: 
    conic-gradient(from 37deg at 54% 48%, #448aff, #ffc107, #448aff) 0 calc(0*100%/3), 
    conic-gradient(from 264deg at 8% 31%, #1565c0, #ff9800, #1565c0) 0 calc(1*100%/3), 
    conic-gradient(from -61deg at 92% 2%, #009688, #f44336, #009688) 0 calc(2*100%/3), 
    conic-gradient(from -5deg at 41% 95%, #8bc34a, #ad1457, #8bc34a) 0 calc(3*100%/3);
  background-repeat: no-repeat;
  background-size: 100% 1.2em; /* you will have to adjust this */
  font: 900 clamp(1em, 18vmin, 18em)/1 monospace;
  writing-mode: vertical-lr;
  text-orientation: upright;
  text-transform: uppercase
}
<div>play</div>

Edit:根据@Ana的 comments ,我们也可以使用大小的百分比,而不再依赖于字体指标

div {
  box-shadow: 0 0 0 6px;
  background: 
    conic-gradient(from 37deg at 54% 48%, #448aff, #ffc107, #448aff) 0 calc(0*100%/3), 
    conic-gradient(from 264deg at 8% 31%, #1565c0, #ff9800, #1565c0) 0 calc(1*100%/3), 
    conic-gradient(from -61deg at 92% 2%, #009688, #f44336, #009688) 0 calc(2*100%/3), 
    conic-gradient(from -5deg at 41% 95%, #8bc34a, #ad1457, #8bc34a) 0 calc(3*100%/3);
  background-repeat: no-repeat;
  background-size: 100% calc(100%/4); 
  font: 900 clamp(1em, 18vmin, 18em)/1 monospace;
  writing-mode: vertical-lr;
  text-orientation: upright;
  text-transform: uppercase
}
<div>play</div>


这就是说,你可以用不同的方式创造同样的东西.有点老生常谈,但它可以是一个很好的解决方法:

div {
  width: 1ch; /* limit the width to one character */
  word-break: break-all; /* allow the word to break */
  /* you can use 1lh everywhere */
  background: 
    conic-gradient(from 37deg at 54% 48%, #448aff, #ffc107, #448aff), 
    conic-gradient(from 264deg at 8% 31%, #1565c0, #ff9800, #1565c0) 0 1lh, 
    conic-gradient(from -61deg at 92% 2%, #009688, #f44336, #009688) 0 2lh, 
    conic-gradient(from -5deg at 41% 95%, #8bc34a, #ad1457, #8bc34a) 0 3lh;
  background-repeat: no-repeat;
  background-size: 100% 1lh;
  /* you can use percentage with background-position as well
  background-position:
   0 calc(0*100%/3),
   0 calc(1*100%/3),
   0 calc(2*100%/3),
   0 calc(3*100%/3); */
  font: 900 clamp(1em, 18vmin, 18em)/1 monospace;
  
  display: inline-block;
  margin: 5px;
  padding-inline: .2em;
  box-shadow: 0 0 0 6px;
  vertical-align: top;
  text-transform: uppercase
}
<div>play</div>
<div style="line-height: 2">play</div>
<div style="line-height: .9">play</div>

Css相关问答推荐

如何改变图标的 colored颜色 时悬停在

如何使用TailwindCSS溢出?

向左或向右浮动时的不同图像边距

如果`line-Height:1`,则垂直字母所占的高度

覆盖现有像素的混合

CSS Select 标签旁边的文本并输入

如何使用来自单独模块的代码将自定义标头插入到使用 DT Shiny 呈现的表中?

如何在 VS Code 中获得 JavaFX CSS 属性的自动完成功能?

如何简化CSS?

带有 gatsby-plugin-image 的 bootstrap 卡 ImgOverlay 未按预期调整大小

Angular 导航栏问题

CSS和 Select 器 - 我可以 Select 具有多个类的元素吗?

firefox overflow-y 不能使用嵌套的 flexbox

如何使表格中一行的最后一个单元格占据所有剩余宽度

创建 CSS 全局变量:样式表主题管理

什么是 ::content/::slotted 伪元素,它是如何工作的?

如何在 html/css 中的图像旁边垂直居中文本?

如何在 javascript 中获取 HTML 元素的样式值?

将 webkit 滚动条样式应用于指定元素

CSS(webkit):在绝对定位元素上用底部覆盖顶部