# How to set the background colour of text lines with Text using FabricJS?

In this tutorial, we are going to learn how to set the background colour of text lines with Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. Similarly we can also set the background colour of text lines by using the textBackgroundColor property.

## Syntax

new fabric.Text(text: String , { textBackgroundColor : String }: Object)


## Parameters

• text − This parameter accepts a String which is the text string that we want to display.

• options (optional) − This parameter is an Object which provides additional customizations to our text. Using this parameter colour, cursor, border width and a lot of other properties can be changed related to the object of which textBackgroundColor is a property.

## Options Keys

• textBackgroundColor − This property accepts a String value which allows us to set the background colour of text lines.

## Example 1

Passing the textBackgroundColor property as key with a hexadecimal value

Let’s see a code example to assign a background colour to our Triangle object using hexadecimal value of colour. In this example, we have used the hex colour code of #ebdef0 which is a lilac colour.

<!DOCTYPE html>
<html>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
<body>
<h2>Passing the textBackgroundColor property as key with a hexadecimal value</h2>
<p>You can see the background colour of the text lines</p>
<canvas id="canvas"></canvas>

<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);

// Initiate a text object
var text = new fabric.Text("Add sample text here.", {
width: 300,
left: 60,
top: 70,
fill: "green",
textBackgroundColor: "#ebdef0"
});

// Add it to the canvas
</script>
</body>
</html>


## Example 2

Passing the textBackgroundColor property as key with a rgba value

We can use an RGBA value, instead of a hexadecimal colour code, which stands for: red, green, blue and alpha. The alpha parameter specifies the opacity of a colour. In this example, we have used the rgba value (255,20,147,0.8) which is the colour pink with 0.8 opacity.

<!DOCTYPE html>
<html>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
<body>
<h2>Passing the textBackgroundColor property as key with a RGBA value</h2>
<p>You can see the new background colour of the text lines</p>
<canvas id="canvas"></canvas>

<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);

// Initiate a text object
var text = new fabric.Text("Add sample text here.", {
width: 300,
left: 60,
top: 70,
fill: "green",
textBackgroundColor: "rgba(255,20,147,0.2)"
});

// Add it to the canvas
</html>