113

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 precious SEO because of images replacing text.

Transparent text cut out of background

I first thought of shadows but I can't figure anything out...

The image is the site background, an absolute positioned <img> tag

web-tiki
  • 99,765
  • 32
  • 217
  • 249
Love Dager
  • 2,251
  • 2
  • 18
  • 16
  • "lose all precious SEO because of images replacing text." There are also existing image replacement techniques, that still are SO friendly. BTW: actually the background behind the letters also need to stay transparent, which is the problem here… – feeela Dec 18 '12 at 12:29
  • yeap, this would be a great practice, if possible with css... – Jessica Lingmn Dec 18 '12 at 12:35
  • I don't see a reason why `

    Some Text

    ` is any less SEO friendly than `

    Some Text

    `. Traditionally, the problem with images has been that they've just been dumped on the page with no supporting markup.
    – cimmanon Dec 18 '12 at 12:38
  • @cimmanon Yeah, you're right. I could probably use images without losing SEO points, but you can't still select text, search on page, linking will be more complicated, and it will be a lot more work to update the text... :/ – Love Dager Dec 18 '12 at 12:44
  • 2
    Just so you know, Google is totally cool with CSS image replacement techniques: http://mezzoblue.com/archives/2008/05/05/image_replac/ – cimmanon Dec 18 '12 at 14:00
  • 1
    Not sure if this is the same: https://css-tricks.com/how-to-do-knockout-text/ – mbomb007 Feb 06 '17 at 15:12

14 Answers14

54

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/

Fabian Lauer
  • 8,891
  • 4
  • 26
  • 35
Gijs
  • 2,064
  • 16
  • 21
  • you maby need to alter the code a bit, depends on the situation, now it could conflict with other elements and this one was written really fast, if you have any questions i will hear them – Gijs Dec 18 '12 at 13:32
  • Yeah sure, but I know jQuery like the palm of my hand (okay.. let's say almost). I will probably make a nifty little plugin for it! Also thank you, marked as correct! – Love Dager Dec 18 '12 at 22:38
  • 3
    Not to disregard the answer, which is a really nice solution, but `background-clip:text` is a Webkit-only, non-standard option. CSS3 does not allow the `text` value for this attribute ([see](http://stackoverflow.com/a/17224852)). – tanius Oct 17 '15 at 14:49
  • Confirmed that this unfortunately does not work in firefox – larpo Oct 14 '16 at 11:08
  • 1
    This technique allows you to see the background of the element where the text would normally be. I'm looking for a way to see what's under the element with the text through the place where the text would normally be. – Vince Feb 20 '17 at 04:06
  • @larpo Works in my Firefox... Perhaps the standard's changed? – wizzwizz4 Oct 20 '18 at 13:47
54

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 :

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>
web-tiki
  • 99,765
  • 32
  • 217
  • 249
  • 2
    This seems to be the only solution which works cross browser today: IE11, FF, Chrome, Safari. And doesn't need image as a background as many other solutions provided to the problem all around the web. – Timo Kähkönen Apr 27 '15 at 21:18
  • 1
    Inspired from this answer I made a keyframes animation, where transparent SVG-text on white rounded rectangle rotates. The background has text and images. Go and see: http://jsbin.com/nipuqu/3/ (The World championship is coming:)) – Timo Kähkönen Apr 27 '15 at 21:53
  • 2
    Nice solution, but since "select text and search on page" seem to be in OP's requirements, you may want to include the `` outside of the `` part. (btw I'm not sure how SE crawlers do deal with SVG content in defs). Here is a way to keep what OP wants, with an horrible FF bug exploit : https://jsfiddle.net/tfneqxxb/ – Kaiido Jan 22 '16 at 11:49
  • What if the text is dynamic? It dosesn't auto adjust the width. – Imran Bughio Apr 11 '16 at 07:29
  • 1
    @ImranBughio no it doesn't. SVG text doesn't act like plain HTML text. You nee to position it . For more in for info see [here](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/text) – web-tiki Apr 11 '16 at 12:40
44

There is a simple way to do this with just CSS:

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

for transparent text on a black background, or

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

for transparent text on a white background.

Put these styles on your text element with whichever background you want behind it.

Example CodePen example screenshot

Read up on mix-blend-mode and experiment with it to use different colours.

Caveats:

  1. For this to work in chrome, you also need to explicitly set a background colour on the html element.
  2. This works on basically all modern browsers except IE.
Nic
  • 640
  • 7
  • 9
  • 5
    For white background with black color use `color-dodge` , `lighten` or `screen` on `mix-blend-mode` – Imran Bughio Apr 11 '16 at 07:36
  • 1
    @ZachSaucier thanks, I've updated the answer to mention that you now need to set a background on the `html` element for this to work in Chrome. – Nic Aug 14 '21 at 19:32
  • I tried without setting a background on the `html` element and it worked fine in Chrome! – Marco Castanho Oct 04 '22 at 20:09
  • Damn, thank you so much for this. The first answer to use a background on the text element and then aligning it is by far the worst approach imo (e.g. if you're using an animated gradient as bg), using an SVG text mask is much better, but still somewhat of a hassle. This just solves the issue with two lines of CSS if you have a black or white background! – Alessandro Jun 04 '23 at 18:22
3

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/

Kyle
  • 65,599
  • 28
  • 144
  • 152
  • Yeah, that's another problem with it. It's still in development and I sincerely hope we see something like this soon! :) – Kyle Dec 18 '12 at 12:48
  • Also, you can always layer over transparent text on the image, so that it's still selectable. But I am sure that it's bad practice for SEO.. Hidden messages/keywords and such. – Kyle Dec 18 '12 at 12:56
  • I am sure it is looks very nice in Webkit browsers, but for everyone else it is a semi-transparent white box on top of some grass. – cimmanon Dec 18 '12 at 13:45
  • Without a graceful fallback, this is 100% useless in production. It's not like `box-shadow` or `border-radius` where if the browser doesn't support those properties the page is still usable, it just doesn't look as pretty. That's not the case with this property. – cimmanon Dec 18 '12 at 13:54
  • The OP's question was "is it possible." Yes, it is. In Webkit browsers only. It is in development and I hope it becomes a standard feature sometime. But to use this in production would not be a great idea. – Kyle Dec 18 '12 at 13:57
  • The fact that he is worried about SEO implies that his plan *is* to use it in production. There's also a large number of designers out there who think Webkit is the only browser that matters: http://www.glazman.org/weblog/dotclear/index.php?post/2012/02/09/CALL-FOR-ACTION:-THE-OPEN-WEB-NEEDS-YOU-NOW – cimmanon Dec 18 '12 at 14:02
  • Aye. It may have most of the market share, but there are still millions of users using other browsers, it's folly to only support one. – Kyle Dec 18 '12 at 14:09
  • 1
    Ah, come on guys... Yes I plan on using it in production. But of course not without a fallback! @cimmanon – Love Dager Dec 18 '12 at 22:41
3

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;

    }
Rashed
  • 31
  • 3
3

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>
Andria
  • 4,712
  • 2
  • 22
  • 38
3

In the near future we can use element() to achieve this

The element() function allows an author to use an element in the document as an image. As the referenced element changes appearance, the image changes as well ref

The trick is to create a common div with text then use element() combined with mask.

Here is a basic example that works only on the latest version Firefox for now.

#text {
  font-size:35px;
  font-weight:bold;
  color:#000;
  font-family:sans-serif;
  text-transform: uppercase;
  white-space:nowrap;
  /* we hide it */ 
  position:fixed;
  right:200vw;
  bottom:200vh
}


body {
  background:url(https://picsum.photos/id/1018/800/800) center/cover; 
}

.main {
  margin:50px;
  height:100px;
  background:red;
  -webkit-mask:
    -moz-element(#text) center/contain no-repeat, /* this behave like a background-image*/
    linear-gradient(#fff 0 0);
  mask-composite:exclude;
}
<div id="text">
You can put your text here
</div>

<div class="main">

</div>

It will produce the following:

CSS transparent text over background image

It's reponsive since we rely on basic background properties and we can easily update the text using basic CSS.

We can consider any kind of content and also create patterns:

#text {
  font-size:30px;
  font-weight:bold;
  color:#000;
  font-family:sans-serif;
  text-transform: uppercase;
  white-space:nowrap;
  padding:20px;
  /* we hide it */ 
  position:fixed;
  right:200vw;
  bottom:200vh
}
#text span {
  font-family:cursive;
  font-size:35px;
}


body {
  background:url(https://picsum.photos/id/1018/800/800) center/cover; 
}

.main {
  margin:50px;
  height:100px;
  background:red;
  -webkit-mask:
    -moz-element(#text) 0 0/20% auto, /* this behave like a background-image*/
    linear-gradient(#fff 0 0);
  mask-composite:exclude;
}
<div id="text">
Your <span>text</span> here 
</div>

<div class="main">

</div>

CSS text mask over background

And why not some animation to create an infinite scrolling text:

#text {
  font-size:30px;
  font-weight:bold;
  color:#000;
  font-family:sans-serif;
  text-transform: uppercase;
  white-space:nowrap;
  padding:20px 5px;
  /* we hide it */ 
  position:fixed;
  right:200vw;
  bottom:200vh
}


body {
  background:url(https://picsum.photos/id/1018/800/800) center/cover; 
}

.main {
  margin:50px;
  height:100px;
  padding-right:calc(50% - 50px);
  background:red;
  -webkit-mask:
    -moz-element(#text) 0 50%/200% auto content-box, /* this behave like a background-image*/
    linear-gradient(#fff 0 0);
  mask-composite:exclude;
  animation:m 5s linear infinite;
}

@keyframes m{
  to {-webkit-mask-position:200% 50%}
}
<div id="text">
Srolling repeating text here 
</div>

<div class="main">

</div>

animated text using CSS mask

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
2

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:

Community
  • 1
  • 1
tanius
  • 14,003
  • 3
  • 51
  • 63
1

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)

feeela
  • 29,399
  • 7
  • 59
  • 71
1

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.
Community
  • 1
  • 1
tanius
  • 14,003
  • 3
  • 51
  • 63
1

Demo Screenshot

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!

Pecacheu
  • 368
  • 1
  • 3
  • 13
1

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>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
1

This worked for me mix-blend-mode: color-dodge on the container with opposite colors.

.main{
 background: url('https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg');
 height: 80vh;
 width: 100vw;
 padding: 40px;
}

.container{
   background-color: white;
   width: 80%;
   height: 50px;
   padding: 40px;
   font-size: 3em;
   font-weight: 600;
   mix-blend-mode: color-dodge;
}

.container span{
  color: black;
}
<div class="main">
<div class="container">
<span>This is my text</span>
</div>
</div>
cozmik05
  • 477
  • 5
  • 13
-1

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.

Billy Moat
  • 20,792
  • 7
  • 45
  • 37
  • Yeah, you're right. I could probably use images without losing SEO points, but you can't still select text, search on page, linking will be more complicated, and it will be a lot more work to update the text... :/ – Love Dager Dec 18 '12 at 12:46