Transparent text cut out of background

后端 未结 12 687
无人及你
无人及你 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: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;
    }

    ABCDEFGHIKJ

    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;
    }
    
    

    ABCDEFGHIKJ

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

提交回复
热议问题