Search code examples
csscss-shapes

Div with cut out edges, border and transparent background


I've been trying to figure out how to make a custom shape in CSS that visually will look like this:

shape with cut out edges and border

With the property of background:rgba(44, 44, 48, 0.9) and border:6px solid rgba(29, 30, 35, 0.9);

My problem is that I cannot find a way to make the top-right and bottom-left border look like the image I provided. Tried the tips on CSS Custom Shape on CSS-Tricks but it doesn't seem to solve the problem as it cannot have background. Any ideas?


Solution

  • Unfortunately you cannot have a pseudo-element added to a pseudo-element (i.e. :after:after{} will not work) - with a complex shape like yours, you might have to cheat a little and rely on pseudo-elements of its children.

    <div class="fancy-box">
        <h2>Title</h2>
        <p>Content</p>
    </div>
    
    .fancy-box{/*container, top+bottom borders*/}
    .fancy-box:before{/*left-top "square" corner*/}
    .fancy-box:after{/*right-bottom "square" corner*/}
    .fancy-box>p:before{/*left-bottom "dog ear" border*/}
    .fancy-box>p:after{/*right-top "dog ear" border*/}
    .fancy-box>h2:before{{/*left-bottom "dog ear" background*/}
    .fancy-box>h2:after{/*right-top "dog ear" background*/}
    

    Again, this fiddle shows you how it would work with solid colors (reasonably well, although I don't like the "thinner" angles) - but this would fail when you apply opacity. Your best would probably be around having "dog ears" made into pre-rendered semitransparent PNGs, for extra credit you could base64-encode them.

    The "solution" above is a complete semantic horror though - you may have better luck using multiple backgrounds with pre-rendered graphics.