8

Ok so say i'm using the follow setup for my divs:

.background will contain an image. .overlay will have a semitransparent white background for the overlay .inner would effectively mask out a rectangle the size of the div? So that the background is transparent and cuts through the overlay div.

<div class="background">
    <div class="overlay">
        <div class="inner">
        </div>
    </div>
</div>

Is this possible with just css?

Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
PahPow
  • 203
  • 2
  • 3
  • 8

4 Answers4

4

Looks like you can achieve that adding a thick border with some opacity (Fiddle). The border widths will determine size of rectangle desired:

<div class="background">
  <div class="inner">
  </div>
</div>

and CSS:

html, body {
  height: 100%;
  width: 100%;
}
.background {
  width: 100%;
  height: 100%;
  background: url('http://farm9.staticflickr.com/8242/8558295633_f34a55c1c6_b.jpg');
}
.inner {
  width: 100%;
  height: 100%;
  border-top: 130px solid rgba(255, 255, 255, 0.5);
  border-bottom: 130px solid rgba(255, 255, 255, 0.5);
  border-left:  100px solid rgba(255, 255, 255, 0.5);
  border-right:  100px solid rgba(255, 255, 255, 0.5);
  box-sizing: border-box;
}
deebs
  • 1,390
  • 10
  • 23
1

YES, if you use a PNG image for the masking. It is possible to clip the background div using it's children. What you would need to do it use a PNG with transparent area in the middle or where ever you want.

Newbie
  • 77
  • 1
  • 3
1

While you can't mask complex shapes, you can mask simple shapes like a cube or their rounded edges.

Just use: overflow-x or overflow-y or overflow

Which according to an inspect of Google Chrome, can be set to: auto, hidden, inherit, initial, overlay, revert, scroll, unset, or visible

Although, I find that: inherit, initial, revert, unset and visible do not provide a mask in most cases.

Finally, if you want to mask on a curve, simply set a border-radius property. Remember, in a border-radius, you can use:

1 value: A; all corners

2 values: A, B; A=Top-Left & Bottom-Right B=Top-Right & Bottom-Left

3 values: A, B, C; A=Top-Left B=Top-Right & Bottom-Left C=Bottom-Right

4 values: A, B, C, D; A=Top-Left B=Top-Right C=Bottom-Right D=Bottom-Left

example of unmasked div example of overflow: hidden Example of overflow: hidden, border-radius: 100%

-tested in Google Chrome Version 96.0.4664.45 (Official Build) (64-bit) with HTML and CSS on the date of this posting.

0

Short answer is - no, you could not clip div by it's children.

But you can solve your problem without clipping. As I understand, you just need white border around inner div. You may use border or box-shadow. Also you can create such border with 4 divs on each side

Denis Sheremet
  • 2,453
  • 2
  • 18
  • 34