【发布时间】: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 所以最好调整背景位置。