简体   繁体   English

如何在 hover 上使用深色叠加背景的图像上叠加透明文本?

[英]How to overlay transparent text on an image with a dark overlay background on hover?

Title is a bit of a mouthful.标题有点拗口。 I've just started with CSS and am trying to achieve the effect a text overlay while the image is still transparent behind the text.我刚刚开始使用 CSS 并试图实现文本覆盖的效果,而图像在文本后面仍然是透明的。

Below is what I've managed to achieve by snipping together various bits of code I've found.下面是我通过将我找到的各种代码片段剪在一起而设法实现的目标。 I am struggling to get the dark overlay the same size as the image.我正在努力使深色叠加层与图像大小相同。 I haven't used any margin or padding on the overlay or image so have no clue why it's happening.我没有在叠加层或图像上使用任何边距或填充,所以不知道为什么会发生这种情况。 I've also tried several ways to align the text so it sits vertically in the middle but have had no such luck.我还尝试了几种对齐文本的方法,使其垂直位于中间,但没有这样的运气。

 .image-container { position: absolute; width: 200px; height: 200px; border-radius: 50%; }.border { border-radius: 50%; }.image-container.after { position: absolute; left: 0; bottom: 0; width: 100%; height: 100%; display: none; color: #FFF; border-radius: 50%; }.image-container:hover.after { display: block; background: rgba(0, 0, 0, .6); border-radius: 50%; } #title { background: url('https://bopepor.es/wp-content/uploads/2018/08/Logo-200x200PX.png'); background-size: cover; color: #fff; -webkit-text-fill-color: transparent; -webkit-background-clip: text; padding: 0; display: flex; } h1 { font-size: 80px; font-family: sans-serif; text-align: center; text-shadow: 0 0 1px rgba(0, 0, 0, .1); margin: 0; padding: 0; letter-spacing: -1px; line-height: 0.8; }
 <div class="image-container"> <img src="https://bopepor.es/wp-content/uploads/2018/08/Logo-200x200PX.png" class='border' /> <div class="after"> <div id="title"> <h1><b>ONE<br>TWO</b></h1> </div> </div> </div>

  1. For the first issue You will not able to center the overlay on to the image because the image isn't actually 200px x 200px because there are transparent pixels around the image.对于第一个问题,您将无法将叠加层置于图像的中心,因为图像实际上不是 200 像素 x 200 像素,因为图像周围有透明像素。 so first crop the transparent pixels around the image and get it's real size.所以首先裁剪图像周围的透明像素并获得它的真实尺寸。 Then replace the 200px size in the css below to the appropriate size.然后把下面css中的200px大小替换成合适的大小。

  2. I have corrected your code snippet to be able to center the text by adding display: flex and align-content: center (for vertical alignment) and justify-content: center (for horizontal alignment),我已经更正了您的代码片段,以便能够通过添加display: flexalign-content: center (用于垂直对齐)和justify-content: center (用于水平对齐)来使文本居中,

  3. I have also added overflow: hidden to the .image-container.after to prevent overflowed text and changed the text size to 60px for better visibility.我还添加了overflow: hidden.image-container.after以防止溢出的文本并将文本大小更改为60px以获得更好的可见性。

 .image-container { position: absolute; width: 200px; height: 200px; border-radius: 50%; }.image-container.after { position: absolute; display: none; left: 0; bottom: 0; overflow: hidden; color: #FFF; }.image-container:hover.after { display: flex; background: rgba(0, 0, 0, 0.6); border-radius: 50%; border-width: 0px; width: 200px; height: 200px; } #title { background: url('https://bopepor.es/wp-content/uploads/2018/08/Logo-200x200PX.png'); background-size: cover; color: #fff; -webkit-text-fill-color: transparent; -webkit-background-clip: text; padding: 0; display: flex; align-content: center; justify-content: center; width: 100%; } h1 { font-size: 60px; font-family: sans-serif; text-align: center; text-shadow: 0 0 1px rgba(0, 0, 0, .1); margin: 0; padding: 0; display: flex; align-items: center; letter-spacing: -1px; line-height: 0.8; }
 <div class="image-container"> <img src="https://bopepor.es/wp-content/uploads/2018/08/Logo-200x200PX.png" class='border' /> <div class="after"> <div id="title"> <h1><b>ONE<br>TWO</b></h1> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM