14

My designer has designed a border with a diamond shape on the border corners. See image below.

Diamond Border

The way I'd go about achieving this would be to save the diamond shape as an image, create a 1px solid border and then place the diamond shape absolutely positioned on the four corners. While this works I'm sure there is a much smarter way to do this without the additional mark up.

Maybe using something like :after in css? How would I do this, or is there a better way? I need to have this compatible with IE8+ but if it works with IE7+ even better.

6
  • I would just use your current solution. CSS3 would come with stuff to make this 'better' however not all browsers (fully) support it yey.
    – PeeHaa
    Commented Jun 24, 2011 at 11:31
  • 1
    do you plan to have always the same size (or one size for landscape and one for portrait) for every photo on your site?
    – microspino
    Commented Jun 24, 2011 at 11:34
  • The image inside the div is just an example. It may not be an image it may be text and it may expand based on what content thats inside
    – Scott
    Commented Jun 24, 2011 at 11:38
  • :after and :before would work if you only required two corners or you were working with a fixed width. As far as I know, IE7+ supports the :before and :after pseudo classes
    – Alex
    Commented Jun 24, 2011 at 11:39
  • @alex - It will be a fixed width. The height is not fixed. do you have a solution?
    – Scott
    Commented Jun 24, 2011 at 11:40

1 Answer 1

14

For a solution that's widely compatible, I think you should use four elements with position: absolute combined with position: relative and negative offsets:

See: http://jsfiddle.net/M4TC5/

@meo's demo using transform: http://jsfiddle.net/M4TC5/2/
(and my demo: http://jsfiddle.net/M4TC5/1/)

That really just shows the concept, you can generate better transform code (that doesn't look slightly "off" in IE) with this tool: http://www.useragentman.com/IETransformsTranslator/

HTML:

<div class="image">
    <span class="corner TL"></span>
    <span class="corner TR"></span>
    <span class="corner BL"></span>
    <span class="corner BR"></span>
    <img src="???" />
</div>

CSS:

.image {
    position: relative
}
.corner {
    position: absolute;
    background: url(???);
}
.TL {
    top: -10px;
    left: -10px
}
.TR {
    top: -10px;
    right: -10px
}
.BL {
    bottom: -10px;
    left: -10px
}
.BR {
    bottom: -10px;
    right: -10px
}
12
  • I didn't actually notice that you noted this idea in your question. Oh well. If you need IE7 support, then this is what I'd do. Otherwise, I'd probably go for an :after based solution.
    – thirtydot
    Commented Jun 24, 2011 at 11:35
  • is there any other method that doesn't require as much markup and will get IE7? If not please say and I will accept that there isnt any better way.
    – Scott
    Commented Jun 24, 2011 at 11:36
  • 1
    @sandeep - My markup is almost identical to thirtydot's answer.
    – Scott
    Commented Jun 24, 2011 at 11:38
  • 1
    i would not use a BG image you can set them a gray border to, and rotate them. And yes it works in IE to ;) css3please.com
    – meo
    Commented Jun 24, 2011 at 11:43
  • 1
    the skew method doesnt look so good so gonna stick with the extra markup and image. Yes the image will be part of a sprite :)
    – Scott
    Commented Jun 24, 2011 at 12:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.