I'm drawing a tooltip box with Css. Now I would like to apply a gradient to the whole box including the triangle. But the gradient is always only applied to the box.
I tried to put the gradient into the after element, but that doesnt work either. I would just color the triangle in one color, but I need to be able to move it to other places too.
This isn't a duplicate question, as in other answers, the triangle is just colored in the end color of the gradient. But I would like the gradient to also apply to the triangle.
Here's an example:
body {
background-color: tomato;
}
#bubble {
position: absolute;
top: 50px;
left: 50px;
width: 154px;
height: 140px;
padding: 0px;
background: #FFFFFF;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
text-align: center;
font-size: 1em;
font-weight: 500;
background: -webkit-linear-gradient(rgba(223, 204, 206, 0.9) 0%, rgba(242, 144, 127, 1) 100%);
background: -o-linear-gradient(rgba(223, 204, 206, 0.9) 0%, rgba(242, 144, 127, 1) 100%);
background: linear-gradient(rgba(223, 204, 206, 0.9) 0%, rgba(242, 144, 127, 1) 100%);
/ filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#e6dfccce', endColorstr='#f2907f', GradientType=0);
}
#bubble:after {
content: '';
position: absolute;
border-style: solid;
border-width: 15px 16px 0px;
border-color: #FFFFFF transparent;
display: block;
width: 0;
z-index: 1;
bottom: -15px;
left: 59px;
}
#bubble > :first-child {
margin-top: 19px;
}
.answerWrapper {
width: 100%;
display: flex;
justify-content: space-between;
}
.answerWrapperText {
width: 40px;
height: 38px;
color: #131313;
border: solid 2px #131313;
}
.answerWrapper > :first-child {
margin: 0 0 0 18px;
}
.answerWrapper > :nth-child(2) {
margin: 0 18px 0 0;
}
.answerWrapperText p {
margin: 0;
margin-top: 9px;
}
<div id="bubble">
<p>Have you
<br>chosen wisely ?</p>
<div class="answerWrapper">
<div class="answerWrapperText">
<p id="yes">yes</p>
</div>
<div class="answerWrapperText">
<p id="no">no</p>
</div>
</div>
</div>
So my solution is to draw it with canvas.
var bubbleBackground;
function initCanvas () {
bubbleBackground = document.createElement('canvas');
bubbleBackground.id = "BubbleId";
bubbleBackground.width = 184;
bubbleBackground.height = 170;
bubbleBackground.style.position = "absolute";
document.body.appendChild(bubbleBackground);
}
function drawCanvas (drawDirection) {
var ctx = document.getElementById('BubbleId').getContext("2d");
//clear canvas before draw
ctx.clearRect(0, 0, bubbleBackground.width, bubbleBackground.height);
//set to draw normally
ctx.globalCompositeOperation="source-over";
var posX = 15;
var posY = 15;
ctx.roundRect(posX, posY, 154, 140, 6);
ctx.fillStyle = 'rgba(0, 0, 0, 1)';
ctx.fill();
switch(drawDirection) {
case "top":
ctx.beginPath();
ctx.moveTo(106,15);
ctx.lineTo(78,15);
ctx.lineTo(92,3);
ctx.fill();
break;
case "right":
ctx.beginPath();
ctx.moveTo(169,71);
ctx.lineTo(181,85);
ctx.lineTo(169,99);
ctx.fill();
break;
case "bottom":
ctx.beginPath();
ctx.moveTo(78,155);
ctx.lineTo(106,155);
ctx.lineTo(92,167);
ctx.fill();
break;
case "left":
ctx.beginPath();
ctx.moveTo(15,71);
ctx.lineTo(15,99);
ctx.lineTo(3,85);
ctx.fill();
break;
default:
console.log("no arrow")
}
//draw gradient
ctx.globalCompositeOperation="source-in";
var grd=ctx.createLinearGradient(0,0,0,170);
grd.addColorStop(0, 'rgba(223,204,206,0.9)');
grd.addColorStop(1, 'rgba(242,144,127,1)');
ctx.fillStyle=grd;
ctx.fillRect(0,0,184,170);
}
CanvasRenderingContext2D.prototype.roundRect = function (x, y, width, height, radius) {
if (width < 2 * radius) radius = width / 2;
if (height < 2 * radius) radius = height / 2;
this.beginPath();
this.moveTo(x + radius, y);
this.arcTo(x + width, y, x + width, y + height, radius);
this.arcTo(x + width, y + height, x, y + height, radius);
this.arcTo(x, y + height, x, y, radius);
this.arcTo(x, y, x + width, y, radius);
this.closePath();
return this;
}
initCanvas();
drawCanvas("right");