How to invert stroke text color depending on background

前端 未结 1 1627
生来不讨喜
生来不讨喜 2020-12-21 10:21

I have 2 divs 50% width each. There is a huge header h1 which should have the color of these two divs. I have tried mix-blend-mode but it gives me some random colors when se

相关标签:
1条回答
  • 2020-12-21 11:02

    One idea is to duplicate the text and use CSS variable to define the color so you can easily change them in one place. I used clip-path to hide half of one text and show the other half:

    body {
      margin: 0;
      --c1:#510035;
      --c2:#E8E8E8;
    }
    body:hover {
      --c1:red;
      --c2:blue;
    }
    
    h1 {
      font-size: 4.7em;
      text-transform: uppercase;
      margin: 0;
    }
    .first {
      background:var(--c1);
      -webkit-text-stroke: 3px var(--c2);
    }
    
    .second {
      background:var(--c2);
      -webkit-text-stroke: 3px var(--c1);
      clip-path:polygon(0% 0%, 50% 0%, 50% 100%,0% 100%);
    }
    
    .lp-header {
      position:absolute;
      top:0;
      left:0;
      right:0;
      min-height:100vh;
      box-sizing:border-box;
      color: transparent;
      z-index: 1;
      padding: 50px;
      text-align: center;
      transition:0.5s;
    }
    <h1 class="lp-header first">left or right</h1>
    <h1 class="lp-header second">left or right</h1>

    0 讨论(0)
提交回复
热议问题