简体   繁体   English

CSS 渐变顶部的文本

[英]Text on Top of CSS Gradient

I have the following code with various gradients, which uses -webkit-mask .我有以下带有各种渐变的代码,它使用-webkit-mask I want to be able to add some text on top of the gradient (where I have TEXT).我希望能够在渐变顶部添加一些文本(我有 TEXT)。

Here is the current code:这是当前代码:

 .test { background: radial-gradient(120% 120%, red 30%, #000); height: 50vh; }.test:before { margin-top: 7.5vh; height: 50vh; content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: radial-gradient(120% 120%, blue 30%, #000); -webkit-mask: linear-gradient(transparent, #fff); mask: linear-gradient(transparent, #fff); }.test-two { height: 50vh; position: relative; background: radial-gradient(120% 120%, green 30%, #000); -webkit-mask: linear-gradient(to right, transparent, #fff); mask: linear-gradient(to right, transparent, #fff); }.test-two:after { height: 50vh; content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: radial-gradient(120% 120%, gold 30%, #000); -webkit-mask: linear-gradient(transparent, #fff); mask: linear-gradient(transparent, #fff); color: white; } body { margin: 0; }
 <div class="test"> <div class="test-two"> <p>TEXT</p> </div> </div>

It seems like it won't be possible to put the text on top since you're using a mask on those containers.由于您在这些容器上使用了掩码,因此似乎无法将文本放在顶部。 But it might be possible to change the mask's linear gradient starting point from left to right so that the transparent part might fall on top of the text.但是可以从左到右更改蒙版的线性渐变起点,以便透明部分可能落在文本的顶部。

Thus I tried to reverse it -webkit-mask: linear gradient(to left, transparent, #fff);mask: linear-gradient(to left, transparent, #fff);因此我试图扭转它-webkit-mask: linear gradient(to left, transparent, #fff);mask: linear-gradient(to left, transparent, #fff); and it displayed the text.它显示了文本。

I hope that's helpful.我希望这会有所帮助。

This is the formatted code.这是格式化的代码。

<div class="test">
    <div class="test-two">
      <p>TEXT</p>
    </div>
  </div>
  <style>

    p{
        font-size: 5rem;
        position: absolute;
        z-index: 99;
       
    }
  .test {
    background: radial-gradient(120% 120%, red 30%, #000);
    height: 50vh;
   }
  .test:before {
    /* margin-top: 7.5vh; */
    height: inherit;
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(120% 1200%, blue 30%, #000);
    -webkit-mask: linear-gradient(to top,transparent, #fff);
    mask: linear-gradient(to top, transparent, #fff);
  }
  .test-two {
    
    height: 100%;
    position: relative;
    background: radial-gradient(120% 120%, green 30%, #000);
    -webkit-mask: linear-gradient(to left, transparent, #fff);
    mask: linear-gradient(to left, transparent, #fff);
    z-index: 99;
  }
  .test-two:after {
    height: 50vh;
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(120% 120%, gold 30%, #000);
    -webkit-mask: linear-gradient(transparent, #fff);
    mask: linear-gradient(transparent, #fff);
    color: white;
  }
  body {
    margin: 0;
  }

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

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