Transparent text cut out of background

后端 未结 12 631
无人及你
无人及你 2020-11-22 06:05

Is there any way to make a transparent text cut out of a background effect like the one in the following image, with CSS?
It would be sad to lose all pr

相关标签:
12条回答
  • 2020-11-22 06:14

    mix-blend-mode is also a possibility for that kind of effect .

    The mix-blend-mode CSS property sets how an element's content should blend with the content of the element's parent and the element's background.

    h1 {
    background:white;
    mix-blend-mode:screen;
    
    /* demo purpose from here */
    padding:0.25em;
    mix-blend-mode:screen;
    }
    
    
    html {
    background:url(https://i.picsum.photos/id/1069/367/267.jpg?hmac=w5sk7UQ6HGlaOVQ494mSfIe902cxlel1BfGUBpEYoRw)center / cover ;
    min-height:100vh;
    display:flex;
    }
    body {margin:auto;}
    h1:hover {border:dashed 10px white;background-clip:content-box;box-shadow:inset 0 0 0 2px #fff, 0 0 0 2px #fff}
    <h1>ABCDEFGHIJKLMNOPQRSTUVWXYZ</h1>

    0 讨论(0)
  • 2020-11-22 06:16

    I guess you could achieve something like that using background-clip, but I haven't tested that yet.

    See this example:
    http://www.css3.info/wp-content/uploads/2008/03/webkit-backgroundcliptext_color.html
    (Webkit only, I don't know yet how to change the black background to a white one)

    0 讨论(0)
  • 2020-11-22 06:16

    just put that css

        .banner-sale-1 .title-box .title-overlay {
          font-weight: 900;
          overflow: hidden;
          margin: 0;
          padding-right: 10%;
          padding-left: 10%;
          text-transform: uppercase;
          color: #080404;
          background-color: rgba(255, 255, 255, .85);
    
          /* that css is the main think (mix-blend-mode: lighten;)*/
          mix-blend-mode: lighten;
    
        }
    
    0 讨论(0)
  • 2020-11-22 06:19

    You can use myadzel's Patternizer jQuery plugin to achieve this effect across browsers. At this time, there is no cross-browser way to do this with just CSS.

    You use Patternizer by adding class="background-clip" to HTML elements where you want the text to be painted as an image pattern, and specify the image in an additional data-pattern="…" attribute. See the source of the demo. Patternizer will create an SVG image with pattern-filled text and underlay it to the transparently rendered HTML element.

    If, as in the question's example image, the text fill pattern should be a part of a background image extending beyond the "patternized" element, I see two options (untested, my favourite first):

    • Use masking instead of a background image in the SVG. As in web-tiki's answer, to which using Patternizer will still add automatic generation of the SVG and an invisible HTML element on top that allows text selection and copying.
    • Or use automatic alignment of the pattern image. Can be done with JavaScript code similar to the one in Gijs's answer.
    0 讨论(0)
  • 2020-11-22 06:24

    Not possible with CSS just now I'm afraid.

    Your best bet is to simply use an image (probably a PNG) and and place good alt/title text on it.

    Alternatively you could use a SPAN or a DIV and have the image as a background to that with your text you want for SEO purposes inside it but text-indent it off screen.

    0 讨论(0)
  • 2020-11-22 06:26

    It's possible with css3 but it's not supported in all browsers

    With background-clip: text; you can use a background for the text, but you will have to align it with the background of the page

    body {
        background: url(http://www.color-hex.com/palettes/26323.png) repeat;
        margin:10px;
    }
    h1 { 
        background-color:#fff;
        overflow:hidden;
        display:inline-block; 
        padding:10px; 
        font-weight:bold;
        font-family:arial;
        color:transparent;
        font-size:200px;
    }
    span { 
        background: url(http://www.color-hex.com/palettes/26323.png) -20px -20px repeat;
        -webkit-text-fill-color: transparent;
        -webkit-background-clip: text;
        display:block;
    }
    <h1><span>ABCDEFGHIKJ</span></h1>

    http://jsfiddle.net/JGPuZ/1337/


    Automatic Alignment

    With a little javascript you can align the background automatically:

    $(document).ready(function(){
      //Position of the header in the webpage
      var position = $("h1").position();
      var padding = 10; //Padding set to the header
      var left = position.left + padding;
      var top = position.top + padding;
      $("h1").find("span").css("background-position","-"+left+"px -"+top+"px"); 
    });
    body {
        background: url(http://www.color-hex.com/palettes/26323.png) repeat;
        margin:10px;
    }
    h1 { 
        background-color:#fff;
        overflow:hidden;
        display:inline-block; 
        padding:10px; 
        font-weight:bold;
        font-family:arial;
        color:transparent;
        font-size:200px;
    }
    span { 
        background: url(http://www.color-hex.com/palettes/26323.png) -20px -20px repeat;
        -webkit-text-fill-color: transparent;
        -webkit-background-clip: text;
        display:block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <h1><span>ABCDEFGHIKJ</span></h1>

    http://jsfiddle.net/JGPuZ/1336/

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