2

Is it possible to create a box like the one below with notches in the center of the top and bottom line? (including the border inset?)

enter image description here

InvalidSyntax
  • 9,131
  • 20
  • 80
  • 127
  • 6
    Use the pseudo selectors `:before` / `:after` and use a CSS triangle/arrow. Then add shadows/borders. – Josh Crozier Sep 27 '13 at 00:02
  • By CSS-only do you mean no images? Because if so, its probably very unlikely to be possible. –  Sep 27 '13 at 00:03
  • You can at least with using canvas: http://stackoverflow.com/questions/18659926/creating-a-transparent-inner-notch/18664225#18664225 –  Sep 27 '13 at 00:10

3 Answers3

2

Here's a Working Fiddle Tested On: Ie10, FF, Chrome, Safari

just put your content inside the .Content div. (should support any size of content)

HTML:

<div class="SpecialBox">
    <div class="TopTriangle">
        <div class="Gray Border">
            <div class="Black Border">
                <div class="White Border"></div>
            </div>
        </div>
    </div>

    <div class="Content">
        Some Content
        <br />
        And another line of Content
    </div>

    <div class="BottomTriangle">
        <div class="Gray Border">
            <div class="Black Border">
                <div class="White Border"></div>
            </div>
        </div>
    </div>
</div>

CSS:

*
{
    margin: 0;
    padding: 0;
}

.SpecialBox
{
    background-color: black;
    -webkit-box-shadow: inset 0 0 0 5px black, inset 0 0 0 6px gray;
    box-shadow: inset 0 0 0 5px black, inset 0 0 0 6px gray;
    display: inline-block;
    color: white;
    overflow: hidden;
    position: relative;
}
.Content
{
    padding: 20px;
}

.Border
{
    width: 0;
    border-left: 35px solid transparent;
    border-right: 35px solid transparent;   
    position: absolute;
}

.Gray
{
    position: relative;
    margin: 0 auto;
}
.TopTriangle .Gray
{
    border-top: 25px solid gray;
}
.BottomTriangle .Gray
{
    border-bottom: 25px solid gray;
    top: -1px;
}

.Black
{
    left: -35px;
}
.TopTriangle .Black
{
    border-top: 25px solid black;
    top: -27px;
}
.BottomTriangle .Black
{
    border-bottom: 25px solid black;
    top: 1px;
}

.Black:before
{
    content: '';
    position: absolute;
    left: -35px;
    width: 70px;
    height: 6px;
    background-color: black;
}
.TopTriangle .Black:before
{
    top: -24px;
}
.BottomTriangle .Black:before
{
    top: 19px;
    z-index: 1;
}

.White
{
    left: -35px;
}
.TopTriangle .White
{
    border-top: 25px solid white;
    top: -30px;
}
.BottomTriangle .White
{
    border-bottom: 25px solid white;
    top: 6px;
    z-index: 2;
}

Notice: It can probably done with few elements in the DOM (using more pseudo elements)

avrahamcool
  • 13,888
  • 5
  • 47
  • 58
  • Could this be used with a border-top-image for transparent notches? – InvalidSyntax Sep 29 '13 at 00:48
  • I've tried adding `border-top-image: url(clear.gif) 100% repeat;` instead of `border-top` but it doesn't seem to be working. – InvalidSyntax Sep 29 '13 at 19:53
  • If you have an image that creates the triangle, You will need to change the CSS a little bit. because right now we are building a triangle with pure CSS, and the `border-top` is part of that. Post your transparent Image as an edit to your question, and I'll show you how to build the box with it. – avrahamcool Sep 29 '13 at 19:57
  • if you're going to use images for transparency, you will also have to create a chunk of black with the gray border (for top, bottom and sides) – avrahamcool Sep 29 '13 at 20:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38294/discussion-between-imran-and-avrahamcool) – InvalidSyntax Sep 29 '13 at 20:08
1

Trying to do it with minimal markup , and the notches and the inset transparent:

CSS

.test {
    position: absolute;
    width: 200px;
    height: 400px;
    top: 40px;
    left: 40px;
    background-image: linear-gradient(-135deg, transparent 30px, black 30px), 
    linear-gradient(135deg, transparent 30px, black 30px), 
    linear-gradient(-45deg, transparent 30px, black 30px), 
    linear-gradient(45deg, transparent 30px, black 30px), 
    radial-gradient(200px 5px ellipse at 50% 50%, transparent 70px, 
               black 73px);
    background-size: 50% 20%, 50% 20%, 50% 72%, 50% 72%, 100% 10%;
    background-position: left top, right top, left bottom, right bottom, left 20%;
    background-repeat: no-repeat;
}

And, as promised, a simple HTML

<div class="test"></div>

fiddle

NOTE: the example uses the latest gradient syntax. Can be made to work in any browser that supports multiple backgrounds, adapting the gradient syntax

vals
  • 61,425
  • 11
  • 89
  • 138
  • Can work in Safari adapting the gradient syntax. Will give you the answer tomorrow, I don't have enough time today. – vals Sep 28 '13 at 05:02
  • Not working great with dynamic content. when the content is bigger then the box. [here](http://jsfiddle.net/avrahamcool/m4DJj/2/) – avrahamcool Sep 28 '13 at 16:21
  • still, the notch hover the content. and missing the gray border. – avrahamcool Sep 29 '13 at 18:47
1

This solution is using an image (a very wide one) to cover all the possible width that a box will ever take. and using border-image. (Currently not supported in IE)

HTML:

<div class="SpecialBorder">
    <div class="Content">
        Some Content
    </div>
</div>

CSS:

.SpecialBorder
{
    border: 20px solid black; /* fallback for IE*/
    -moz-border-image: url(https://i.stack.imgur.com/ZB9vk.png) 20 20 repeat;
    -o-border-image: url(https://i.stack.imgur.com/ZB9vk.png) 20 20 repeat;
    -webkit-border-image: url(https://i.stack.imgur.com/ZB9vk.png) 20 20 repeat;
    border-image: url(https://i.stack.imgur.com/ZB9vk.png) 20 20 repeat;

}
.Content
{
    background-color: #1d1d1d; /* same BG as the image*/
}

Working Fiddle

avrahamcool
  • 13,888
  • 5
  • 47
  • 58