Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can CSS be used to mask background images?

Tags:

jquery

css

This is what I have presently:

Image of current code

Here's my code so far:

<style type="text/css">
    .main
    {
        background:url(bg.jpg);
        height:250px;
        }
    .ban
    {
        background-color:#333;
        height:150px;
    }
    .mask
    {
        width:75px;
        height:75px;
        float:left; 
        border:#fff solid 1px;

        margin:20px;
    }
</style>

<div class="main">
    <div class="ban">
         <div class="mask"></div> 
         <div class="mask"></div>
         <div class="mask"></div>
         <div class="mask"></div>
         <div class="mask"></div>    
    </div>
</div>

Here's what I am aiming for:

enter image description here

I am looking to create a mask using CSS - what do I need for this?

like image 810
mcxxx Avatar asked Jul 08 '12 14:07

mcxxx


People also ask

Can you set a background image in CSS?

The background-image property in CSS is used to set an image as the background of an element. Using this CSS property, we can set one or more than one background image for an element. By default, the image is positioned at the top-left corner of an element and repeated both horizontally as well as vertically.

What is masking in CSS?

The mask CSS shorthand property hides an element (partially or fully) by masking or clipping the image at specific points. Note: As well as the properties listed below, the mask shorthand also resets mask-border to its initial value.

How do I mask an image?

Quick steps for creating a clipping mask: Select a text or graphic layer to fill with an image. Click Fill with image on the tool palette & choose an image. Select Edit image fill on the Text Tools panel. Adjust the image behind your text or shapes, then click Done.


1 Answers

If you don't find the solution in the comments above, then I've got one for you.

Instead of trying to create the svg or png image to position against, you could use borders (if you're using a solid color that's easy to work with) to replicate this.

You can see a working jsFiddle here

Edit

The old jsFiddle died out, and in order to recreate, accompanying code is required. Provided below is the 'mask' element that makes this all work.

.mask
{
    width:50px; //non-essential
    height:50px; //non-essential
    float:left; //non-essential
    background:transparent;
    border:1px solid #333;
    border-top:20px solid #333;
    border-left:10px solid #333;
    border-right:10px solid #333;
    border-bottom:50px solid #333;
}
like image 154
Ohgodwhy Avatar answered Oct 14 '22 20:10

Ohgodwhy