The Canvas fillStyle property controls the paint used inside filled shapes and text. Set it to a CSS color, a CanvasGradient, or a CanvasPattern before calling methods such as fill(), fillRect(), or fillText().
The default fill style is black.
<canvas id="my_canvas" width="400" height="160"></canvas>
<script>
const canvas = document.getElementById('my_canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ff00ff';
ctx.fillRect(20, 20, 100, 70);
</script>
fillStyle is a property, not a function. Assign a supported value with =.
ctx.fillStyle = 'red'; // CSS color
ctx.fillStyle = gradient; // CanvasGradient
ctx.fillStyle = pattern; // CanvasPattern
| Value | What it does |
|---|---|
| CSS color | Uses a valid CSS color such as a named color, hexadecimal, RGB, HSL or other supported CSS color syntax. |
CanvasGradient | Uses a gradient created with createLinearGradient(), createRadialGradient(), or createConicGradient(). |
CanvasPattern | Uses a pattern created with createPattern(). |
Invalid values are ignored, so the previous valid fill style remains in effect.
Change the rectangle position, size and color. This preserves the original page's interactive purpose while replacing the generated color table and jQuery UI sliders with native browser controls.
The simplest use is to assign a valid CSS color string.
ctx.fillStyle = 'blue';
ctx.fillRect(20, 20, 80, 60);
ctx.fillStyle = 'rgb(220 53 69)';
ctx.fillRect(120, 20, 80, 60);
ctx.fillStyle = 'hsl(45 100% 51%)';
ctx.fillRect(220, 20, 80, 60);
For the rectangle method itself, see fillRect().
Create a gradient object, add color stops, assign it to fillStyle, and then draw the filled shape.
const gradient = ctx.createLinearGradient(0, 0, 250, 0);
gradient.addColorStop(0, 'blue');
gradient.addColorStop(1, 'red');
ctx.fillStyle = gradient;
ctx.fillRect(20, 20, 230, 90);
Continue with Canvas linear gradients or Canvas radial gradients.
createPattern() returns a CanvasPattern. Assign the returned pattern to fillStyle before filling a shape. If the pattern uses an image, wait until the image is ready before creating the pattern.
const image = new Image();
image.src = 'pattern.png';
image.addEventListener('load', () => {
const pattern = ctx.createPattern(image, 'repeat');
if (pattern) {
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
});
Common repetition values include 'repeat', 'repeat-x', 'repeat-y', and 'no-repeat'.
| Property | Controls |
|---|---|
fillStyle | The inside of shapes and text produced by fill(), fillRect(), and fillText(). |
strokeStyle | Lines and outlines produced by stroke(), strokeRect(), and strokeText(). |
Canvas drawing properties are stateful. Once set, fillStyle is reused by later fill operations until you assign another value or restore an earlier drawing state.
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 70, 50);
ctx.fillRect(100, 10, 70, 50); // still green
Canvas fills are pixels and do not create semantic HTML by themselves. If fill colors communicate categories, warnings, status, values or other important information, provide the same meaning in accessible text or HTML and do not rely on color alone.
fillStyle as a function. It is a property, so assign a value with =.fillStyle after fill() or fillRect() and expecting it to recolor pixels that are already drawn.strokeStyle when you want to color the inside of a shape.beginPath() to be required for fillRect(). Rectangle convenience methods draw directly.The default fill style is black.
Yes. fillStyle can use valid CSS color strings, including named colors, hexadecimal, RGB and HSL forms.
Yes. Create a CanvasGradient, add its color stops, and assign the gradient object to fillStyle.
Yes. Create a CanvasPattern with createPattern() and assign it to fillStyle.
No. strokeRect() uses strokeStyle. fillRect() uses fillStyle.
Canvas tutorial | fillRect() | strokeStyle | Linear gradient | Radial gradient | rect()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.