Transparent text cut out of background

后端 未结 12 686
无人及你
无人及你 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:31

    Although this is possible with CSS, a better approach would be to use an inline SVG with SVG masking. This approach has some advantages over CSS :

    • Much better browser support: IE10+, chrome, Firefox, safari...
    • This doesn't impact SEO as spiders can crawl SVG content (google indexes SVG content since 2010)

    CodePen Demo : SVG text mask

    transparent text clipping background

    body,html{height:100%;margin:0;padding:0;}
    body{
      background:url('https://farm9.staticflickr.com/8760/17195790401_94fcf60556_c.jpg');
      background-size:cover;
      background-attachment:fixed;
    }
    svg{width:100%;}
    <svg viewbox="0 0 100 60">
      <defs>
        <mask id="mask" x="0" y="0" width="100" height="50">
          <rect x="0" y="0" width="100" height="40" fill="#fff"/>
          <text text-anchor="middle" x="50" y="18" dy="1">SVG</text>
          <text text-anchor="middle" x="50" y="30" dy="1">Text mask</text>
        </mask>
      </defs>
      <rect x="5" y="5" width="90" height="30" mask="url(#mask)" fill-opacity="0.5"/>    
    </svg>

    If you aim on making the text selectable and searchable, you need to include it outside the <defs> tag. The following example shows a way to do that keeping the transparent text with the <use> tag:

    body,html{height:100%;margin:0;padding:0;}
    body{
      background:url('https://farm9.staticflickr.com/8760/17195790401_94fcf60556_c.jpg');
      background-size:cover;
      background-attachment:fixed;
    }
    svg{width:100%;}
    <svg viewbox="0 0 100 60">
      <defs>
        <g id="text">
          <text text-anchor="middle" x="50" y="18" dy="1">SVG</text>
          <text text-anchor="middle" x="50" y="30" dy="1">Text mask</text>
        </g>
        <mask id="mask" x="0" y="0" width="100" height="50">
          <rect x="0" y="0" width="100" height="40" fill="#fff"/>
          <use xlink:href="#text" />
        </mask>
      </defs>
      <rect x="5" y="5" width="90" height="30" mask="url(#mask)" fill-opacity="0.5"/>
      <use xlink:href="#text" mask="url(#mask)" />
    </svg>

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

    You can use an inverted / negative / reverse font and apply it with the font-face="…" CSS rule. You might have to play with letter spacing to avoid small white gaps between letters.

    If you do not require a specific font, it's simple. Download a likeable one, for example from this collection of inverted fonts.

    If you require a specific font (say, "Open Sans"), it's difficult. You have to convert your existing font into an inverted version. This is possible manually with Font Creator, FontForge etc., but of course we want an automated solution. I could not find instructions for that yet, but some hints:

    • How to convert a bitmap font into a TrueType font (plus yet another way to do that). One would first use ImageMagick commands to render the font glyphs into high-resolution raster images and to invert them, then convert them back to a TrueType font with the above instructions.
    • Is it possible to invert a font with FontForge or another PGM?
    • Creating a reverse (white on black) font
    0 讨论(0)
  • 2020-11-22 06:34

    I needed to make text that looked exactly like it does in the original post, but I couldn't just fake it by lining up backgrounds, because there's some animation behind the element. Nobody seems to have suggested this yet, so here's what I did: (Tried to make it as easy to read as possible.)

    var el = document.body; //Parent Element. Text is centered inside.
    var mainText = "THIS IS THE FIRST LINE"; //Header Text.
    var subText = "THIS TEXT HAS A KNOCKOUT EFFECT"; //Knockout Text.
    var fontF = "Roboto, Arial"; //Font to use.
    var mSize = 42; //Text size.
    
    //Centered text display:
    var tBox = centeredDiv(el), txtMain = mkDiv(tBox, mainText), txtSub = mkDiv(tBox),
    ts = tBox.style, stLen = textWidth(subText, fontF, mSize)+5; ts.color = "#fff";
    ts.font = mSize+"pt "+fontF; ts.fontWeight = 100; txtSub.style.fontWeight = 400;
    
    //Generate subtext SVG for knockout effect:
    txtSub.innerHTML =
    "<svg xmlns='http://www.w3.org/2000/svg' width='"+stLen+"px' height='"+(mSize+11)+"px' viewBox='0 0 "+stLen+" "+(mSize+11)+"'>"+
        "<rect x='0' y='0' width='100%' height='100%' fill='#fff' rx='4px' ry='4px' mask='url(#txtSubMask)'></rect>"+
        "<mask id='txtSubMask'>"+
            "<rect x='0' y='0' width='100%' height='100%' fill='#fff'></rect>"+
            "<text x='"+(stLen/2)+"' y='"+(mSize+6)+"' font='"+mSize+"pt "+fontF+"' text-anchor='middle' fill='#000'>"+subText+"</text>"+
        "</mask>"+
    "</svg>";
    
    //Relevant Helper Functions:
    function centeredDiv(parent) {
        //Container:
        var d = document.createElement('div'), s = d.style;
        s.display = "table"; s.position = "relative"; s.zIndex = 999;
        s.top = s.left = 0; s.width = s.height = "100%";
        //Content Box:
        var k = document.createElement('div'), j = k.style;
        j.display = "table-cell"; j.verticalAlign = "middle";
        j.textAlign = "center"; d.appendChild(k);
        parent.appendChild(d); return k;
    }
    function mkDiv(parent, tCont) {
        var d = document.createElement('div');
        if(tCont) d.textContent = tCont;
        parent.appendChild(d); return d;
    }
    function textWidth(text, font, size) {
        var canvas = window.textWidthCanvas || (window.textWidthCanvas = document.createElement("canvas")),
        context = canvas.getContext("2d"); context.font = size+(typeof size=="string"?" ":"pt ")+font;
        return context.measureText(text).width;
    }
    

    Just throw that in your window.onload, set the body's background to your image, and watch the magic happen!

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

    I just discovered a new way to do this while messing around, I'm not entirely sure how it works ( if someone else wants to explain please do ).

    It seems to work very well, and requires no double backgrounds or JavaScript.

    Here's the code: JSFIDDLE

    body {
      padding: 0;
      margin: 0;
    }
    
    div {
      background: url(http://www.color-hex.com/palettes/26323.png) repeat;
      width: 100vw;
      height: 100vh;
    }
    
    body::before {
      content: '$ALPHABET';
      left: 0;
      top: 0;
      position: absolute;
      color: #222;
      background-color: #fff;
      padding: 1rem;
      font-family: Arial;
      z-index: 1;
      mix-blend-mode: screen;
      font-weight: 800;
      font-size: 3rem;
      letter-spacing: 1rem;
    }
    <div></div>

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

    One way that works on most modern browsers (except ie, edge), although only with a black background, is to use

    background: black;
    color: white;
    mix-blend-mode: multiply;
    

    in your text element and then put whatever background you want behind that. Multiply basically maps the 0-255 color code to 0-1 and then multiplies that by whatever is behind it, so black stays black and white multiplies by 1 and effectively becomes transparent. http://codepen.io/nic_klaassen/full/adKqWX/

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

    It is possible, but so far only with Webkit based browsers (Chrome, Safari, Rockmelt, anything based on the Chromium project.)

    The trick is to have an element within the white one that has the same background as the body, then use -webkit- background-clip: text; on the inner element which basically means "don't extend the background beyond the text" and use transparent text.

    section
    {
        background: url(http://norcaleasygreen.com/wp-content/uploads/2012/11/turf-grass1.jpg);
        width: 100%;
        height: 300px;
    }
    
    div
    {
        background: rgba(255, 255, 255, 1);
        color: rgba(255, 255, 255, 0);
    
        width: 60%;
        heighT: 80%;
        margin: 0 auto;
        font-size: 60px;
        text-align: center;
    }
    
    p
    {
        background: url(http://norcaleasygreen.com/wp-content/uploads/2012/11/turf-grass1.jpg);
        -webkit-background-clip: text;
    }
    ​
    

    http://jsfiddle.net/BWRsA/

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