【问题标题】:How to make text fit the full height of a container element如何使文本适合容器元素的整个高度
【发布时间】:2020-08-27 06:17:18
【问题描述】:

我想要实现的是将文本着色为红色、黄色、绿色蓝色。现在没有红色,如下图所示。文本元素的高度从屏幕顶部到文本底部,容器元素也是如此。为了获得红色显示,我需要让文本达到元素的整个高度,在图片中它将是黑色背景的顶部。我尝试了几种变体,试图用 flex 和 table 来设置它的样式。

如果我从容器元素中删除高度,它会使文本垂直居中,但正如这张图片所示,它仍然不是全高,以显示更多颜色的顶部和底部。有什么想法吗?

@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;800&display=swap');
body {
  background-color: black;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#container {
  height: 8rem;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: stretch;
  /* this changes nothing */
  align-self: stretch;
  /* this also changes nothing */
}

#header {
  /* I've tried flex and align stretch etc. here also without success */
  text-align: center;
  font-family: 'Orbitron', sans-serif;
  font-size: 8rem;
  font-weight: 800;
  background: linear-gradient(to bottom, red 0% 25%, yellow 25% 50%, green 50% 75%, blue 75%);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}
<div id="container">
  <div id="header">WORD</div>
</div>

【问题讨论】:

  • 您是否尝试过调整line-height 属性?
  • 我在这个例子中将line-height 设置为 1,但是我不知道字体是否打算看起来像这样。 codepen.io/MrFuze/pen/yLObYop
  • 我没有尝试过,但我只是这样做了,line-height 不会改变文本的高度,它只会改变它的位置。
  • 因此将line-height 设置为1 并将font-size 设置为7rem 可以充分显示文本,但是它与容器的大小不同。 codepen.io/MrFuze/pen/yLObYop
  • 行高通常比字体大小大 20%。你能做的就是把这个高度倒回去。如果您希望您的字体高度等于元素的大小,您可以将其值增加 20%。通过调整一些数字,你可以得到:jsfiddle.net/csmLnwzk 所以最好调整背景位置。

标签: html css


【解决方案1】:

这是由于字体的性质造成的。如果您添加 Â 之类的字符,您将看到红色部分,因此您的文本已经占用了所需的空间。

@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;800&display=swap');
body {
  background-color: black;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#container {
  height: 8rem;
  display: flex;
  justify-content: center;
}

#header {
  text-align: center;
  font-family: 'Orbitron', sans-serif;
  font-size: 8rem;
  font-weight: 800;
  background: linear-gradient(to bottom, red 0% 25%, yellow 25% 50%, green 50% 75%, blue 75%);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}
<div id="container">
  <div id="header">WORÂÄ</div>
</div>

如果你添加像j 这样的字符,你也会对一些溢出感到惊讶:

@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;800&display=swap');
body {
  background-color: black;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#container {
  height: 8rem;
  display: flex;
  justify-content: center;
}

#header {
  text-align: center;
  font-family: 'Orbitron', sans-serif;
  font-size: 8rem;
  font-weight: 800;
  background: linear-gradient(to bottom, red 0% 25%, yellow 25% 50%, green 50% 75%, blue 75%);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}
<div id="container">
  <div id="header">WjRÂÄ</div>
</div>

在您的特殊情况下,您可以使用background-size 来获得您想要的颜色,因为您不会有像j 这样的字符

@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;800&display=swap');
body {
  background-color: black;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#container {
}

#header {
  text-align: center;
  font-family: 'Orbitron', sans-serif;
  font-size: 8rem;
  font-weight: 800;
  background: linear-gradient(to bottom, red 0% 25%, yellow 25% 50%, green 50% 75%, blue 75%);
  /* an approxiamtion based on your case, you may need to change it */
  background-size:100% 60%; 
  background-position:0px 54%;
  /**/
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}
<div id="container">
  <div id="header">WORD</div>
</div>

【讨论】:

  • 投反对票。因为这两个例子都剪掉了底部的文字,我猜这不是他的本意。
  • @Simplicius 嗯,如果我在第一个示例中没有进行任何更改,如何剪切文本?同样在第二个中(仅更改背景属性)
  • 好了,运行代码sn-p。但是对你来说,第一个 sn-p 中的文本因为特殊字母“”和“Ä”而被推下,在第二个上,再次运行代码,容器的大小是肉,因为text 被定义为背景图像,它也结束了。
  • @Simplicius 字母无法推送文本,字体度量定义文本的位置/大小,无论您使用的字母是什么.. 不确定您所看到的.. 第一个 sn-p 给出确切的与问题中的 sn-p 结果相同,因为没有更改 CSS
  • @TemaniAfif 感谢您的浏览。您的第三个 sn-p 接近我想要实现的目标。但是,颜色不是文本高度的 25%,它们接近但有点偏离。我尝试弄乱背景大小和位置百分比,但只能关闭。如果我可以让文本填充元素,那么所有颜色都将是文本高度的 25%。
猜你喜欢
  • 1970-01-01
  • 2021-07-26
  • 2014-08-05
  • 2020-04-01
  • 1970-01-01
  • 2015-01-28
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多