Masks

Bootstrap 5 Masks

Masks alter the visibility of an element by either partially or fully hiding it. Masks are used to make content more visible by providing a proper contrast. They are most often used on images.

Without mask

Louvre Museum

Can you see me?

With mask

Louvre Museum

Can you see me?


Basic example

A simple example of mask usage.

        
            
          <div class="bg-image">
            <img src="https://mdbcdn.b-cdn.net/img/new/standard/city/053.webp" class="w-100" />
            <div class="mask" style="background-color: hsla(0, 0%, 0%, 0.6)"></div>
          </div>
        
        
    

How it works

A detailed explanation of how the masks work in MDB:

  • Masks require .bg-image wrapper which sets a position to relative, overflow to hidden, and properly center the image.
  • The inside .bg-image wrapper as the first child we place an img element with the source link
  • Below is the actual mask. We set a color and opacity via hsla code and inline CSS. In the section, you will find a detailed explanation of how HSLA colors work with masks
  • If you want to put a text on the image you have to place it within the .mask wrapper. To center it you have to use flex utilities (explained in the section below).

HSLA

By manipulating HSLA code you can change the color and opacity of the mask.

Color

hsla(217, 89%, 51%, 0.6)
hsla(276, 98%, 61%, 0.6)
hsla(144, 100%, 36%, 0.6)
hsla(349, 94%, 58%, 0.6)
hsla(0, 0%, 98%, 0.6)
hsla(195, 83%, 58%, 0.6)

You can even set a fancy gradient as a mask.

        
            
            <div class="bg-image">
              <img src="https://mdbcdn.b-cdn.net/img/new/standard/city/053.webp" class="w-100" />
              <div
                class="mask"
                style="
                  background: linear-gradient(
                    45deg,
                    hsla(168, 85%, 52%, 0.7),
                    hsla(263, 88%, 45%, 0.7) 100%
                  );
                "
              ></div>
            </div>
          
        
    

Opacity

By changing the last value in the HSLA color you can manipulate the opacity of the mask.

0.0 means fully transparent and 1.0 fully opaque. You can set any value between 0.0 and 1.0.

hsla(0, 0%, 0%, 0.0) - fully transparent

hsla(0, 0%, 0%, 1.0) - fully opaque

0.1
0.3
0.55
0.7
0.8
0.9

Content

The main goal of the masks is to provide a proper contrast between the image and the content placed on it. Most commonly we put a text on the images with masks.

Within .mask wrapper place a div and apply flexbox utilities to center the content vertically and horizontally. Then put the text inside.

        
            
          <div class="bg-image">
            <img
              src="https://mdbcdn.b-cdn.net/img/new/standard/city/053.webp"
              class="w-100"
              alt="Louvre Museum"
            />
            <div class="mask" style="background-color: hsla(0, 0%, 0%, 0.6)">
              <div class="d-flex justify-content-center align-items-center h-100">
                <p class="text-white mb-0">Can you see me?</p>
              </div>
            </div>
          </div>
        
        
    

Ripple

You can easily add a ripple effect to the image with a mask.

Simply add .ripple class next to the .bg-image class.

        
            
          <!-- Default dark color ripple -->
          <div class="bg-image ripple">
            <img src="https://mdbcdn.b-cdn.net/img/new/standard/city/053.webp" class="w-100" />
            <div class="mask" style="background-color: hsla(0, 0%, 98%, 0.6)"></div>
          </div>

          <!-- Light color ripple changed via data-mdb-attribute -->
          <div class="bg-image ripple" data-mdb-ripple-color="light">
            <img src="https://mdbcdn.b-cdn.net/img/new/standard/city/053.webp" class="w-100" />
            <div class="mask" style="background-color: hsla(0, 0%, 0%, 0.6)"></div>
          </div>
        
        
    


Gradient

These are linear, gradient masks (a variation of our standard masks) that allow you to darken the background around the text in an image, but do not cover the entire image, and therefore partially retain its natural colors.

Alternative text

Can you see me?

Alternative text

Can you see me?

Alternative text

Can you see me?

        
            
          <!--Grid row-->
          <div class="row">
            <div class="col-lg-4 col-md-12 mb-4 mb-lg-0">
              <!-- Image with no mask -->
              <div class="bg-image rounded-6">
                <img
                  src="https://mdbootstrap.com/img/new/ecommerce/vertical/010.jpg"
                  class="w-100"
                  alt="Alternative text"
                />
                <!-- Mask -->
                <div class="mask">
                  <div
                    class="
                      bottom-0
                      d-flex
                      align-items-end
                      h-100
                      text-center
                      justify-content-center
                    "
                  >
                    <div>
                      <h2 class="fw-bold text-white mb-4">Can you see me?</h2>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-lg-4 mb-4 mb-lg-0">
              <!-- Image with black mask -->
              <div class="bg-image rounded-6">
                <img
                  src="https://mdbootstrap.com/img/new/ecommerce/vertical/010.jpg"
                  class="w-100"
                  alt="Alternative text"
                />
                <!-- Mask -->
                <div
                  class="mask"
                  style="
                    background: linear-gradient(
                      to bottom,
                      hsla(0, 0%, 0%, 0) 50%,
                      hsla(0, 0%, 0%, 0.5)  
                    );
                  "
                >
                  <div
                    class="
                      bottom-0
                      d-flex
                      align-items-end
                      h-100
                      text-center
                      justify-content-center
                    "
                  >
                    <div>
                      <h2 class="fw-bold text-white mb-4">Can you see me?</h2>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-lg-4 mb-4 mb-lg-0">
              <!-- Image with violet mask -->
              <div class="bg-image rounded-6">
                <img
                  src="https://mdbootstrap.com/img/new/ecommerce/vertical/010.jpg"
                  class="w-100"
                  alt="Alternative text"
                />
                <!-- Mask -->
                <div
                  class="mask"
                  style="
                    background: linear-gradient(
                      to bottom,
                      hsla(0, 0%, 0%, 0),
                      hsla(263, 80%, 20%, 0.5)
                    );
                  "
                >
                  <div
                    class="
                      bottom-0
                      d-flex
                      align-items-end
                      h-100
                      text-center
                      justify-content-center
                    "
                  >
                    <div>
                      <h2 class="fw-bold text-white mb-4">Can you see me?</h2>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!--Grid row-->