2

I am designing a webpage with a background-image. There is a white box in the center (using div) and inside the white box is some text. What I want to do is make the text color the same as that of the background of the body. If I set the color attribute of the text to transparent , it disappears since there is a white div. How do I do this?

This is the link to JSFiddle: http://jsfiddle.net/FpJpV/

The html is

<div>
    <h1>Title</h1>
</div>

The CSS is

body {
    background-color: red;
    font-family: sans-serif;
}

div {
    width: 200px;
    background-color: white;
    margin: auto;
    text-align: center;
    padding: 1em;
}

Note: In the JSFiddle, i used a red background for simplicity but in the real webpage, the background of the body is an image.

2 Answers2

6

There are three ways to what you're looking for in HTML/CSS/JavaScript, some a little more hacky than others.

  1. Use <canvas> to draw your shape and type, and set ctx.globalCompositeOperation="xor";where ctx is your canvas context.

var c=document.getElementById('myCanvas');
var ctx=c.getContext('2d');
ctx.font = "bold 36px Proxima Nova";
ctx.fillStyle='blue';
ctx.fillText("MORE",100,87);
ctx.globalCompositeOperation='xor';
ctx.beginPath();
ctx.fillStyle='white';
ctx.arc(150,75,75,0,2*Math.PI);
ctx.fill();
body{
  background: -webkit-radial-gradient(top left,rgb(23, 39, 34) 4%,rgb(56, 99, 99) 37%,rgb(22, 27, 15) 73%,rgb(22, 27, 14) 93%,#232323 100%);
  width:100%;
  height:100%;
}

html{
  height:100%
}
<canvas id='myCanvas'></canvas>
  1. Use mix-blend-mode: screen and set the colour of the text to black.

.blend-mode {
  background:white;
  border-radius:100%;
  width:150px;
  height:150px;
  line-height:155px;
  float:left;
  margin:20px;
  font-family: "Proxima Nova", Hevetica, sans-serif;
  font-weight:bold;
  color:black;
  text-align:center;
  vertical-align:middle;
  font-size:36px;
  mix-blend-mode: screen;
}

body{
  background: -webkit-radial-gradient(top left,rgb(23, 39, 34) 4%,rgb(56, 99, 99) 37%,rgb(22, 27, 15) 73%,rgb(22, 27, 14) 93%,#232323 100%);
  width:100%;
  height:100%;
}

html{
  height:100%;
}
<div class="blend-mode">MORE</div>
  1. Use -webkit-text-fill-color:transparent and -webkit-background-clip:text to reveal a copy of the background behind it. This one requires some alignment of the background copy to match the 'original' background.

.wrap{
  margin:20px;
  float:left;
  position:relative;
}

.background {
  width: 150px;
  height: 150px;
  border-radius: 100%;
  background: white;
  position:absolute;
  top:0;
  left:0;
  z-index:4;
  
}

.text {
  position:absolute;
  top: 25px;
  left: 25px;
  z-index:5;
  background: -webkit-radial-gradient(top left, rgb(23, 39, 34) 4%, rgb(56, 99, 99) 37%, rgb(22, 27, 15) 73%, rgb(22, 27, 14) 93%, #232323 100%);
  font-family: Proxima Nova, Helvetica, sans-serif;
  font-weight:bold;
  line-height:100px;
  font-size: 36px;
  color: #666;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

body{
  background: -webkit-radial-gradient(top left,rgb(23, 39, 34) 4%,rgb(56, 99, 99) 37%,rgb(22, 27, 15) 73%,rgb(22, 27, 14) 93%,#232323 100%);
  width:100%;
  height:100%;
}

html{
  height:100%;
}
<div class="wrap">
  <div class="background"></div>
  <div class="text">MORE</div>
</div>

None of these are likely to work that well cross browser, and I haven't tested them!

SJT
  • 232
  • 2
  • 11
3

Can't do that with CSS; What I suggest:

Try using an image instead.

Use software such as Photoshop/GIMP to cut the text out from a white image so it's transparent where the text was. Save it as a PNG and set the image as the div's background.

Just make sure you remove the white background from the div as well.

EXAMPLE

Like so:

enter image description here

SaturnsEye
  • 6,297
  • 10
  • 46
  • 62