2

I need to create a box with a diagonal edge. Below is the picture and here is a link to it.

enter image description here

How would I do this using only CSS and HTML?

I'm aware its possible to create triangles in CSS, so maybe I create one div with the rounded corners for the 'yellow' portion of the box. Not sure how to do the inside grey part though.

I'd like to avoid the multiple images solution because this will be on mobile so that it loads as quickly as possible.

I'm looking into a solution with 3 divs inside and one being a triangle, I found a triangle maker here, then maybe relative position on the yellow div, and absolute position the content after that?

Harry
  • 87,580
  • 25
  • 202
  • 214
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154

4 Answers4

4

There is a way to achieve this shape without having to use extra elements also. I can understand the reluctance in using multiple images but this approach only makes use of multiple backgrounds and should not have any impact on page load time.

In this approach, we create a linear-gradient background of a smaller size and position it at the right top of the container to achieve the triangle effect.

This approach can also be used with media queries without much issues.

.shape {
  height: 400px;
  width: 50vw;
  background: linear-gradient(225deg, #F3D56A 50%, transparent 50%), #EFEFEF;
  background-size: 75px 75px;
  background-repeat: no-repeat;
  background-position: right top;
  border-radius: 4px;
  box-shadow: 4px 4px 4px rgba(0, 0, 0, .5);
  border-top: 5px #F3D56A solid;
}
@media (max-width: 300px) {
  .shape {
    width: 150px;
    background-size: 50px 50px;
  }
}
@media (min-width: 301px) and (max-width: 600px) {
  .shape {
    width: 300px;
    background-size: 50px 50px;
  }
}
<div class="shape"></div>
Harry
  • 87,580
  • 25
  • 202
  • 214
  • 1
    That is pretty interesting also, I will have to play around with this way of doing it also, thanks for posting this. – Joseph Astrahan May 13 '15 at 22:19
  • 1
    Your answer I got working and doesn't use floats and was much cleaner so I've accepted it as the new answer. Works in Mobile iOS and Android browsers too! :). – Joseph Astrahan May 13 '15 at 22:35
3

HTML:

<div id="content">
    <span></span>
    <p class="title">Gold</p>
    <p class="subtitle">Starting at</p>
    <p class="price">$69.99/mo</p>
    <a href="#" class="button">More Info</a>
</div>

CSS

#content {
    font-family: Helvetica, Serif;
    width: 440px;
    height: 460px;
    background: #EFEFEF;
    text-align: center;
    border-radius: 5px;
    -webkit-box-shadow: 4px 6px 5px 1px rgba(0,0,0,0.41);
    -moz-box-shadow: 4px 6px 5px 1px rgba(0,0,0,0.41);
    box-shadow: 4px 6px 5px 1px rgba(0,0,0,0.41);
    border-top:7px #F3D56A solid;
}
#content span {
    border-style: solid;
    border-width: 0 110px 110px 0;
    border-color: transparent #f3d56a transparent transparent;
    line-height: 0px;
    _border-color: #000000 #f3d56a #000000 #000000;
    _filter: progid:DXImageTransform.Microsoft.Chroma(color='#000000');
    float: right;
}
.title {
    position: relative;
    left: 50px;
}
.title, .price{
    color:#2C3E50;
    font-size: 45px;
    font-weight: 700;
    text-align: center;
}
.subtitle {
    color: #7A828B;
    font-size: 30px;
    font-weight: 300;
}
a.button {
    text-decoration: none;
    color:#FFF;
    background: #F3D56A;
    padding:20px 30px;
    border-radius: 5px;
}

WORKING DEMO

Update:

Media query to 320x480:

@media only screen and (min-width: 320px) and (max-width: 480px) {
    #content {
        width: calc(100% - 5px);
        height: 400px;
    }
    #content span {
        border-width: 0 90px 90px 0;
    }
}

Result:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
  • wow, very interesting solution, and very little html as well, very clever. Playing around with this to see if it plays nice on mobile devices. – Joseph Astrahan May 13 '15 at 01:07
  • I'll try to set some media queries to this, and before I'll update my answer. – Ferrmolina May 13 '15 at 01:09
  • I updated your answer to fit to all devices also, check this out (https://jsfiddle.net/x11joex11/6bpjc96u/1/), you can stretch the screen for example and it will adapt. – Joseph Astrahan May 13 '15 at 01:13
  • 1
    https://jsfiddle.net/x11joex11/6bpjc96u/3/, I fixed a bug with the title, just clear:both so you don't have to use position:relative; that was stretching the screen causing a scroll bar to the right, now its good though :) – Joseph Astrahan May 13 '15 at 02:13
  • I also forgot to mention to line it backup I used position:relative back on it and top:-40px – Joseph Astrahan May 13 '15 at 02:33
0

Why don't you just create an image and use that as the background. You could make the image look exactly like the gray and yellow above and then just add it to your "box".

NendoTaka
  • 1,224
  • 8
  • 14
  • I'd like to avoid this because of the load times on mobile devices, I will be using this box idea in multiple places and it would really add up, for example, silver service, bronze service, and a few other areas etc. – Joseph Astrahan May 13 '15 at 00:29
  • 1
    Well I haven't had a chance to try this but I found a link that may be useful. https://css-tricks.com/snippets/css/css-triangle/ – NendoTaka May 13 '15 at 00:34
  • I did some testing and I couldn't figure it out the best I got was `div { width: 100px; height: 0; background-color: gray; border-style: solid; border-width: 0 200px 200px 0; border-color: transparent #fff200 transparent transparent; }` There are problems when you round the edges and make it taller though. – NendoTaka May 13 '15 at 00:44
  • This website https://css-tricks.com/examples/ShapesOfCSS/ has a lot of css shapes but there was not a rounded triangle. I don't know if this will be possible with just css. – NendoTaka May 13 '15 at 00:50
  • The top answer on http://stackoverflow.com/questions/22930838/how-to-create-triangle-with-only-one-rounded-corner-using-css-or-css3 has a way to make a triangle with a curved point but I don't know if it will work for you. – NendoTaka May 13 '15 at 00:52
0

here is the link to a js fiddle I have mocked up - this works pretty well, though I didn't do the entire style https://jsfiddle.net/6pcrneat/

.container { 
    width:200px; 
    height:250px; 
    background:#ffffff;
    border:1px solid #000;
    position:relative;
}
.shape {
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    position: absolute;
    right: -45px;
    top: -10px;
    border-style: solid;
    width: 0px;
    height: 0px;
    line-height: 0px;
    border-width: 0px 70px 70px 70px;
    border-color: transparent transparent rgb(243, 213, 106) transparent;  
    moz-transition: .3s;
    -ms-transition: .3s;
    -o-transition: .3s;
}
.text {
  margin-top: 100px;
  text-align: center;
}

Edit: I'll get back to you on the rounded corner of the triangle; I didn't notice that initially

Matt
  • 392
  • 1
  • 18
  • Thanks, I'm working on a JSFiddle as well to try some things. – Joseph Astrahan May 13 '15 at 01:02
  • 1
    https://jsfiddle.net/6pcrneat/2/ Here you go, I added the curve into it here. It could do with touching up a little bit but it's on the right track haha – Matt May 13 '15 at 01:09