I'm trying to make a CSS button . As you can see below, I'm not able to create a gradient for the second HTML-element for the corners. Normally I would use borders or simply rotate a element to create a triangle with a gradient, but the problem is that my triangle isn't a 90 degree square.
Desired design: (Grayscale, retina 200% zoomed)
My CSS button: (Blue, retina 200% zoomed)
Is there a better way to create this button with CSS?
http://jsfiddle.net/G8ZBz/ or simply read below:
HTML-code
<div class="button">
<div class="button-inside"></div>
</div>
CSS
.button {
width: 225px;
height: 60px;
background: #2983d2;
}
.button:before {
position: absolute;
width: 0px;
height: 0px;
left: -13px;
content: "";
display: block;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
border-right: 13px solid #2983d2;
}
.button:after {
position: absolute;
width: 0px;
height: 0px;
top: 0;
right: -13px;
content: "";
display: block;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
border-left: 13px solid #2983d2;
}
.button .button-inside {
position: relative;
width: 221px;
height: 55px;
margin-left: 2px;
background: #469bf9; /* Old browsers */
background: -moz-linear-gradient(top, #469bf9 50%, #1e80f7 50%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(50%,#469bf9), color-stop(50%,#1e80f7)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #469bf9 50%, #1e80f7 50%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #469bf9 50%, #1e80f7 50%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #469bf9 50%, #1e80f7 50%); /* IE10+ */
background: linear-gradient(to bottom, #469bf9 50%, #1e80f7 50%); /* W3C */
}
.button .button-inside:before {
position: absolute;
width: 0px;
height: 0px;
left: -13px;
content: "";
display: block;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
border-right: 13px solid #1e80f7; /* should be a gradient */
}
.button .button-inside:after {
position: absolute;
width: 0px;
height: 0px;
top: 0;
right: -13px;
content: "";
display: block;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
border-left: 13px solid #1e80f7; /* should be a gradient */
}
You can use the skew property to make flat triangles.
In your case, I would use the skew()
properties and background-shadows to make the bottom border effect :
DEMO
Output :
The following image explains what is what and how each element, pseudo elements and background-shadows are used to form the button. Pseudo elements and background-shadows are used to minimize markup:
HTML :
<div class="button top">
<div class="button bottom"></div>
</div>
CSS :
.top{
position:relative;
margin-left:150px;
width: 225px;
height: 60px;
}
.button:before, .button:after{
position: absolute;
width:70%; height:50%;
content: "";
z-index:-1;
}
.top:before {
left:0; top:0;
-webkit-transform:skewX(-20deg);
-ms-transform:skewX(-20deg);
transform:skewX(-20deg);
background: #469BF9;
box-shadow: -5px 10px 0px -5px #104f96;
z-index:-2;
}
.top:after {
right:0; top:0;
-webkit-transform:skewX(20deg);
-ms-transform:skewX(20deg);
transform:skewX(20deg);
background: #469BF9;
box-shadow: 5px 10px 0px -5px #104f96;
z-index:-2;
}
.bottom:before{
left:0; top:50%;
-webkit-transform:skewX(20deg);
-ms-transform:skewX(20deg);
transform:skewX(20deg);
background: #1E80F7;
box-shadow: -4px 5px 0px 0px #104f96;
}
.bottom:after{
right:0; top:50%;
-webkit-transform:skewX(-20deg);
-ms-transform:skewX(-20deg);
transform:skewX(-20deg);
background: #1E80F7;
box-shadow: 4px 5px 0px 0px #104f96;
}
You may also check this codepen with a few different triangles.
You can achieve this shape using a single element by doing the following:
- a single parent element with a
perspective
assigned to it.
- two pseudo-elements which are both half the height of the single parent element and are rotated in opposite directions. The perspective on the parent means they get displayed like trapezoids.
- positioning the two pseudo-elements exactly one below the other to make them look like they are two pieces of the same shape.
- box shadow on the pseudo-element to produce the shadow effect on the bottom side.
.hex-with-shadow {
position: relative;
width: 225px;
height: 60px;
margin: 0px auto;
-webkit-perspective: 10px;
-moz-perspective: 10px;
perspective: 10px;
}
.hex-with-shadow:before {
position: absolute;
content: '';
top: -1px;
left: 0px;
width: 100%;
height: 50%;
background: #469BF9;
box-shadow: 0px 3.5px 0px 0px #104f96;
-webkit-transform: rotateX(3deg);
-moz-transform: rotateX(3deg);
transform: rotateX(3deg);
}
.hex-with-shadow:after {
position: absolute;
content: '';
bottom: 0px;
left: 0px;
width: 100%;
height: 50%;
background: #1E80F7;
border-bottom: 1px solid #104f96;
box-shadow: -4px 3px 0px #104f96, 4px 3px 0px #104f96;
-webkit-transform: rotateX(-3deg);
-moz-transform: rotateX(-3deg);
transform: rotateX(-3deg);
}
<div class="hex-with-shadow"></div>
Final Output:
Original Answer:
You can use the below method to create the triangle and also give a gradient to it.
Demo
Note to Future readers: The second approach is very hacky and not re-usable. Also, this one causes slight distortions in Firefox.