← Back to Bootstrap Course | Chapter 4: Colors & Spacing | Lesson 7 of 7

Opacity

Opacity utilities make an element more or less see-through.

In this page:

  1. Opacity
Syntax
markup
<div class="opacity-50">content</div>

Opacity

.opacity-100, .opacity-75, .opacity-50 and .opacity-25 set the whole element's transparency. For colours only, use .text-opacity-* and .bg-opacity-*, which leave the content fully opaque. Opacity affects children too, so choose the right utility.

Note: bg-opacity changes only the background, not the text on top.

Example: Opacity

markup
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="container">
  <div class="bg-primary text-white p-2 mb-1 opacity-100">opacity-100</div>
  <div class="bg-primary text-white p-2 mb-1 opacity-50">opacity-50 (text fades too)</div>
  <div class="bg-primary bg-opacity-25 p-2 mb-1">bg-opacity-25 (text stays solid)</div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Using opacity when only the background should fade
  2. Making text unreadable
  3. Forgetting bg-opacity needs a bg colour class
Chapter Summary
  • opacity-25, 50, 75, 100
  • bg-opacity and text-opacity affect colours only
  • Opacity affects children
  • Needs a background class for bg-opacity
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.