我最近遇到了这个dribbble/landing page concept with hollow/filled text.
首先,我不完全确定这个概念是否可以在CSS中重新创建.
根据文本背后的背景是否有图像,我如何重建空心/填充文本?
我最近遇到了这个dribbble/landing page concept with hollow/filled text.
首先,我不完全确定这个概念是否可以在CSS中重新创建.
根据文本背后的背景是否有图像,我如何重建空心/填充文本?
它可以在纯HTML+CSS(没有任何SVG)中完成.
我决定使用HTML+CSS,因为我觉得这是一个挑战.
mask-image: element(#target)
which意味着我们不需要只使用掩码的文本,不幸的是Chrome似乎还不支持element()
(但Firefox支持).小wart是text-stroke
的轮廓与纯白文本不完全对齐(至少在我的电脑Helvetica
版本为96dpi的Windows10的Chrome中,但在192dpi(2x,又名Retina))下,在相同的浏览器和电脑上它看起来很完美.
Here's how it looks at different points in the animation on my machine at 96dpi:
下面的示例实现可在以下浏览器中运行(在编写本文时):
body {
background-color: #dbdac2;
--solid-white: linear-gradient(white,white);
}
#container,
#container > #div1 {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-size:
153px 302px,
148px 302px,
154px 302px;
background-position:
131px 94px,
309px 28px,
480px 94px;
background-repeat:
no-repeat,
no-repeat,
no-repeat;
animation: moveImages 2s infinite;
animation-direction: alternate;
}
#container {
border: 1px solid white;
position: relative;
width: 711px;
height: 440px;
/* These are the 3 photo images, rendered as separate background-image layers: */
background-image:
url( "https://i.stack.imgur.com/hmwyh.png" ),
url( "https://i.stack.imgur.com/JeHEg.png" ),
url( "https://i.stack.imgur.com/pVgz6.png" );
}
#container p {
margin: 0;
position: static;
padding-top: 192px;
padding-left: 62px;
overflow: hidden;
font-family: Helvetica;
font-size: 99px;
letter-spacing: -2px;
font-weight: 600;
}
#container > #pStroke {
text-stroke: 1px white;
-webkit-text-stroke: 1px white;
color: transparent;
}
#container > #div1 {
/* #div1's background-image layers must match #container's: */
background-image:
var(--solid-white),
var(--solid-white),
var(--solid-white);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
@keyframes moveImages {
/* The `@keyframes from {}` rule is optional, btw. */
to {
background-size:
53px 302px,
58px 302px,
154px 302px;
background-position:
431px 94px,
209px 28px,
280px 194px;
}
}
<div id="container">
<div id="div1">
<p id="pWhite">Fashion Give<br />Impression.</p>
</div>
<p id="pStroke">Fashion Give<br />Impression.</p>
</div>
说明:
div#container
元素将background-image
属性设置为3个不同的源图像(使用meme图像代替发布示例中的时尚照片).
background-size
和background-position
属性独立定位和zoom .另一个元素(#div1
)覆盖div#container
到absolute
定位,并且具有相同的background-size
和background-position
属性,但是使用单个纯白背景图像(来自linear-gradient(white,white)
)而不是照片,然后这3个白色背景图像层被#div1
的内部<p>
的文本使用background-clip: text;
遮蔽.
background-image: linear-gradient
(或background-image: url("1x1px-white.png");
)而不是background-color: white;
,因为它需要在3个单独的层中重复,而background-color: white;
不能用于在背景中定义矩形区域,也不能有multiple和background-color
个层(即使是半透明的).#div1
的<p>
元素仅使用其内部padding
而不是position: absolute;
来正确定位文本,因为定位的文本不能与background-clip: text;
一起使用.另一个带有文本副本的<p>
元素用于笔划文本(text-stroke: 1px white;)
)
幸运的是,当文本内容在HTML源中复制时,3个图像(以及它们各自的白色蒙版)的更繁琐的大小和位置信息不需要在CSS中复制;这要归功于CSS的 Select 器的工作方式(因为#container
和#div1
的background-size
和background-position
属性都是由单个CSS规则设置的).
可能的替代方法:
代替对纯白部分使用重复的相同大小的background-image
层,可以在#pWhite
上使用单个(但极其复杂)clip-path
跟踪3个方框(就像像蚀刻草图一样绘制的单个线条),但这将不是可行的动画.
我认为最好的方法可能是这样的:
<img />
元素(而不是background-image
),并在新的<div id="images">
中使用absolute
定位.<p>
(在div#images
的同级元素中)将被absolute
巧妙地定位在div#images
×z-index
的上方,并用mask-image: element(#images);
屏蔽element()
,只有Firefox支持.div#images
后面的重复的<p>
元素.<img/>
个元素可以使用transform: translate
来定位,而不是必须经过background-position
或position: absolute
,这将产生更好的性能和帧率.不过,我想不出任何不需要复制文本内容的方法-至少在CSS的content:
属性允许元素从其他元素复制文本之前是这样.