0
0
SASSmarkup~5 mins

RGBA and opacity manipulation in SASS

Choose your learning style9 modes available
Introduction

RGBA lets you add color with transparency. Opacity controls how see-through an element is. Both help make designs look nice and layered.

You want a button background that is partly see-through so the page behind shows a bit.
You want text color with some transparency to soften it on a busy background.
You want to fade an image or element smoothly using opacity.
You want to create hover effects that change transparency.
You want to layer colors with different transparency to create depth.
Syntax
SASS
$color: rgba(red, green, blue, alpha);

Red, green, blue values go from 0 to 255.

Alpha is the transparency from 0 (fully transparent) to 1 (fully visible).

Examples
This creates a blue color that is half transparent.
SASS
$soft-blue: rgba(0, 0, 255, 0.5);
This is black with 30% opacity, good for overlays.
SASS
$transparent-black: rgba(0, 0, 0, 0.3);
This makes the whole box 70% visible, including text and background.
SASS
.box { opacity: 0.7; }
Red text with 80% opacity.
SASS
.text { color: rgba(255, 0, 0, 0.8); }
Sample Program

This code creates a red square box with 60% transparency. The border is a light transparent black. The white text inside is mostly visible. When you hover over the box, the red becomes fully visible.

SASS
@use 'sass:color';

$base-color: rgba(255, 0, 0, 0.6);

.box {
  width: 10rem;
  height: 10rem;
  background-color: $base-color;
  border: 0.2rem solid rgba(0, 0, 0, 0.3);
  color: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 1.5rem;
  border-radius: 0.5rem;
  transition: background-color 0.3s ease;

  &:hover {
    background-color: rgba(255, 0, 0, 1);
  }
}
OutputSuccess
Important Notes

Opacity affects the whole element including text and border.

RGBA transparency only affects the color, not the whole element.

Use hover effects with opacity or RGBA to create interactive designs.

Summary

RGBA lets you add color with transparency using red, green, blue, and alpha values.

Opacity changes how see-through the entire element is.

Use these to create layered, soft, or interactive visual effects in your designs.