4 rounded points star shape - html

I am trying to get this star as pixel perfect as possible in CSS, here's what I tried so far, but it's a 5 angled star, and I want to have it only 4 points also how can I make the corners more rounded?
#star-five {
margin: 50px 0;
position: relative;
display: block;
color: red;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
-moz-transform: rotate(35deg);
-webkit-transform: rotate(35deg);
-ms-transform: rotate(35deg);
-o-transform: rotate(35deg);
}
#star-five:before {
border-bottom: 80px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
position: absolute;
height: 0;
width: 0;
top: -45px;
left: -65px;
display: block;
content: '';
-webkit-transform: rotate(-35deg);
-moz-transform: rotate(-35deg);
-ms-transform: rotate(-35deg);
-o-transform: rotate(-35deg);
}
#star-five:after {
position: absolute;
display: block;
color: red;
top: 3px;
left: -105px;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
-webkit-transform: rotate(-70deg);
-moz-transform: rotate(-70deg);
-ms-transform: rotate(-70deg);
-o-transform: rotate(-70deg);
content: '';
}
<div id="star-five"></div>

Maybe you could use a gradient on the Black Four Pointed Star Unicode char:
✦
It has some compatibility issues (mostly caused by text-fill-color) but depending on your requirements it could get the job done.
.four-point-star::after,
.four-point-star::before {
position: absolute;
top: 0;
left: 0;
content: "\2726";
font-size: 12rem;
}
.four-point-star::after { /* The foreground star */
background: linear-gradient(135deg, rgba(255,191,183,1) 20%, rgba(243,44,117,1) 70%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.four-point-star::before { /* The star shadow */
color: transparent;
text-shadow: 2px 3px 10px rgba(242, 96, 85, 1);
}
/* Demo styling */
body {
background: #fbd629;
padding: 0 2rem;
}
.four-point-star {
position: relative;
}
<span class="four-point-star"></span>

Clearly, for this kind of shape, and inline SVG would be the simplest and the most crossbrowser solution (and with a responsive pixel perfect output).
Here is a simple inline svg code to make a four point star filled with a gradient:
svg{
display:block; margin:0 auto;
width:30%; height:auto;
}
/* For the demo */ body{background: url('https://farm8.staticflickr.com/7187/6895047173_d4b1a0d798.jpg');background-size:cover;}
<svg viewbox="0 0 10 10">
<defs>
<linearGradient id="starGrad">
<stop offset="0%" stop-color="rgb(153,218,255)" />
<stop offset="100%" stop-color="rgb(0,128,128)"/>
</linearGradient>
</defs>
<path fill="url(#starGrad)" d="M5 1 Q5.8 4.2 9 5 Q5.8 5.8 5 9 Q4.2 5.8 1 5 Q4.2 4.2 5 1z" />
</svg>
The four points are made with the path element and using quadratic bezier curves. The star is filled with an SVG linear gradient.

You can get four point star using two rotated and skewed rectangles.
body {
/* just styles for demo */
background-color: #fdd700;
}
.four-point-star {
width: 100px;
height: 100px;
position: relative;
margin-top: 100px;
margin-left: 100px;
}
.four-point-star:before,
.four-point-star:after {
content: "";
position: absolute;
background-color: #fa5b88;
display: block;
left: 0;
width: 141.4213%; /* 100% * √2 */
top: 0;
bottom: 0;
border-radius: 5%;
transform: rotate(66.66deg) skewX(45deg);
}
/* the same but +90deg to rotate */
.four-point-star:after {
transform: rotate(156.66deg) skew(45deg);
}
<div class="four-point-star"></div>

I would suggest an SVG. If you didn't want another http request for the svg file, you could include the svg code in the 'content' attribute:
content: url("data:image/svg+xml; utf8, <svg.. code here</svg>");
.star-4-point {
background: url("data:image/svg+xml; utf8,<svg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' width='50px' height='50px' viewBox='0 0 50 50' style='enable-background:new 0 0 50 50;' xml:space='preserve'><path d='M25,50c0,0,1.325-8.413,8.957-16.043C41.587,26.325,50,25,50,25l0,0c0,0-8.413-1.325-16.043-8.957C26.325,8.413,25,0,25,0 l0,0c0,0-1.325,8.413-8.957,16.043C8.413,23.675,0,25,0,25l0,0c0,0,8.413,1.325,16.043,8.957C23.675,41.588,25,50,25,50'/></svg>");
height: 50px;
width: 50px;
}
<div class="star-4-point"></div>

Related

How to make rounded corners Hexagon with CSS? [duplicate]

This is my CSS.
CSS
#hexagon-circle {
width: 100px;
height: 55px;
background: red;
position: relative;
border-radius: 10px;}
#hexagon-circle:before {
content: "";
position: absolute;
top: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 29px solid red;
border-radius: 10px;}
#hexagon-circle:after {
content: "";
position: absolute;
bottom: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 29px solid red;
border-radius: 10px;}
The output is 4 edges of hexagon is curved, but top and the bottom is not. I want to make all edge of hexagon curved. How to make top and bottom edge to be curved? or How to make the top edge of triangle to be curved?
http://jsfiddle.net/yR7zt/1
I think you are looking for this.
.hex {
position: relative;
margin: 1em auto;
width: 10em;
height: 17.32em;
border-radius: 1em/.5em;
background: orange;
transition: opacity .5s;
}
.hex:before,
.hex:after {
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
}
.hex:before {
-webkit-transform: rotate(60deg);
transform: rotate(60deg);
}
.hex:after {
-webkit-transform: rotate(-60deg);
transform: rotate(-60deg);
}
<div class="hex"></div>
I understand this is a fairly old question, but I thought I'd add an answer to show more about how it works.
So, first off, we need to create a single element on the page. I have gone for a size of height:300px; width:180px; and a border radius of 10px. Just because I like the roundness of the number (forgive the pun). Giving this element a position:relative; means that we can herein position everything absolutely in relative terms to this div.
We then need to create two pseudo elements, with the same height and width as the parent.
Because the pseudo elements are exactly that, pseudo elements, we need to add a content: to them. And since because we can put stuff within the parent, we don't really need these, so set them to "";
this leads us onto how to create the hexagon, rather than the rectangle we currently have. To do that, we're going to have to include a rotation in order to generate the other sides of the hexagon. With there being 6 sides, and the angles needing to add to 360, we can rotate one of the pseudo element by 60 degrees. The other we'll rotate by -60 degrees (or 300degrees, if you'd prefer).
This leaves us with our 'hexagon' in which we can add some nice styling and hover animations as need be:
.roundHex {
position: relative;
margin: 0 auto;
background: rgba(0, 0, 0, 0.2);
border-radius: 10px;
height: 300px;
width: 180px;
transition: all 1s;
line-height:300px;
text-align:center;
color:white;
font-size:20px;
}
.roundHex:before,
.roundHex:after {
content: "";
position: absolute;
top: 0;
left: 0;
background: inherit;
border-radius: inherit;
height: 100%;
width: 100%;
z-index:-1;
}
.roundHex:before {
-webkit-transform: rotate(60deg);
-moz-transform: rotate(60deg);
transform: rotate(60deg);
transition: all 1s 0.5s;
}
.roundHex:after {
-webkit-transform: rotate(-60deg);
-moz-transform: rotate(-60deg);
transform: rotate(-60deg);
transition: all 1s 1s;
}
.roundHex:hover {
background: tomato;
}
<div class="roundHex">HOVER ME</div>
Jsfiddle demo also available
I will consider the same trick I used in the previous answer Where I will build the hexagon using clip-path
.hex {
width: 200px;
display: inline-block;
color:orange;
}
.hex::before {
content: "";
display: block;
background:currentColor;
padding-top: 90%;
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
<div class="hex"></div>
Then I will apply an SVG filter:
.hex {
width: 200px;
display: inline-block;
color:orange;
filter: url('#goo');
}
.hex::before {
content: "";
display: block;
background:currentColor;
padding-top: 86.6%; /* 100%*cos(30) */
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
<div class="hex"></div>
<div class="hex" style="color:blue;width:150px;"></div>
<div class="hex" style="color:red;width:100px;"></div>
<svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="goo"><feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop"/>
</filter>
</defs>
</svg>
And in the opposite direction
.hex {
width: 200px;
display: inline-block;
margin:0 5px;
color:orange;
filter: url('#goo');
}
.hex::before {
content: "";
display: block;
background:currentColor;
padding-top: 115%; /* 100%/cos(30) */
clip-path: polygon(0% 25%,0% 75%,50% 100%,100% 75%,100% 25%,50% 0%);
}
<div class="hex"></div>
<div class="hex" style="color:blue;width:150px;"></div>
<div class="hex" style="color:red;width:100px;"></div>
<svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="goo"><feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop"/>
</filter>
</defs>
</svg>
Try this way :(works in chrome and in ie 10)
<br><br><br>
<div id="hexagon-circle"></div>
<style>
#hexagon-circle {
position: relative;
margin: 1em auto;
width: 10em; height: 17.32em;
border-radius: 1em/.5em;
opacity: .25;
background: orange;
transition: opacity .5s;
cursor: pointer;
}
#hexagon-circle:before, #hexagon-circle:after {
position: absolute;
width: inherit; height: inherit;
border-radius: inherit;
background: inherit;
content: '';
}
#hexagon-circle:before {
transform: rotate(60deg);
-ms-transform:rotate(60deg); /* IE 9 */
-webkit-transform:rotate(60deg); /* Opera, Chrome, and Safari */
}
#hexagon-circle:after {
transform: rotate(-60deg);
-ms-transform:rotate(-60deg); /* IE 9 */
-webkit-transform:rotate(-60deg); /* Opera, Chrome, and Safari */
}
</style>
You can try this approach:
CSS
#hexagon-circle {
position: relative;
margin: 1em auto;
width: 10em;
height: 17.32em;
border-radius: 1em/.5em;
background: red;
transition: opacity .5s;
cursor: pointer;}
#hexagon-circle:before {
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(60deg); /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(60deg); /* IE 9 */
transform: rotate(60deg);} /* Firefox 16+, IE 10+, Opera */
#hexagon-circle:after {
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(-60deg); /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(-60deg); /* IE 9 */
transform: rotate(-60deg);} /* Firefox 16+, IE 10+, Opera */
DEMO
http://jsfiddle.net/yR7zt/4/
With your current code, using the triangle top and bottom, you can modify them slightly to give it a curved look. Add a width of 4px to #hexagon-circle:before and #hexagon-circle:after and reduce border-left and border-right by 2px each.
Js Fiddle here
#hexagon-circle {
width: 100px;
height: 55px;
background: red;
position: relative;
border-radius: 10px;
}
#hexagon-circle:before {
content: "";
position: absolute;
top: -25px;
left: 0;
width: 4px;
height: 0;
border-left: 48px solid transparent;
border-right: 48px solid transparent;
border-bottom: 29px solid red;
border-radius: 10px;
}
#hexagon-circle:after {
content: "";
position: absolute;
bottom: -25px;
left: 0;
width: 4px;
height: 0;
border-left: 48px solid transparent;
border-right: 48px solid transparent;
border-top: 29px solid red;
border-radius: 10px;
}
It's not a true curve as it creates a straight line, however it gives the impression it might be curved when viewed in the context of the shape.

How to inscribe the following shape with CSS inside div?

FIDDLE
HTML
<div id="DiamondCenter">
<div id="triangle-topleft"></div>
</div>
CSS
#DiamondCenter {
position:fixed;
top:2%;
left:48%;
background: #24201a;
height:40px;
width:40px;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
z-index:20 !important;
}
#triangle-topleft {
width: 0;
height: 0;
border-top: 40px solid gray;
border-right: 40px solid transparent;
}
Using SVG:
While using SVG, you could make use of path and polyline elements to draw the required shape. As indicated by Paulie_D in comments, SVG is the better choice for such complex shapes instead of CSS (though this can be achieved with CSS also).
The approach is very simple and is as follows:
One path element for the top polygon which is drawn by joining the points at coordinates (0,50), (50,0), (100,50) and (50,70).
Another path element for the bottom polygon which is drawn by joining the points at (0,50), (50,70) and (100,50).
One polyline element for the orange border which is nothing but a line connecting (0,50), (50,70) and (100,50).
svg {
height: 100px;
width: 100px;
}
path#top {
fill: gray;
}
path#bottom {
fill: black;
}
polyline#border {
stroke: orange;
stroke-width: 2;
fill: none;
}
<svg viewBox="0 0 100 100">
<path id="top" d="M0,50 L50,0 100,50 50,70z" />
<path id="bottom" d="M0,50 L50,100 100,50 50,70z" />
<polyline id="border" points="0,50 50,70 100,50" />
</svg>
Using CSS:
You can achieve the shape provided in question by using 2 pseudo-elements which are both rotated and skewed. The dimension of each pseudo-element is calculated using Pythagoras theorem.
The shape produced using this method is responsive and can adapt to changes in dimension. Hover the shape within the snippet to see how it adapts.
*,
*:after,
*:before {
box-sizing: border-box;
}
#DiamondCenter {
position: fixed;
top: 2%;
left: 48%;
background: #24201a;
height: 40px;
width: 40px;
transform: rotate(45deg);
z-index: 20 !important;
overflow: hidden;
}
#DiamondCenter:after {
position: absolute;
content: '';
bottom: 0px;
left: -1px; /* half the width of border-left */
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: black;
border-left: 2px solid orange;
transform: rotate(40deg) skewX(-20deg);
transform-origin: bottom left;
}
#DiamondCenter:before {
position: absolute;
content: '';
top: -1px; /* half the width of border-top */
right: 0px;
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: black;
border-top: 2px solid orange;
transform: rotate(-40deg) skewY(-20deg);
transform-origin: top right;
}
/* Just for demo */
#DiamondCenter{
transition: all 1s;
}
#DiamondCenter:hover{
top: 5%;
height: 80px;
width: 80px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div id="DiamondCenter"></div>
In the below snippet, I have set different background colors for the pseudo-elements to illustrate how the shape is achieved.
*,
*:after,
*:before {
box-sizing: border-box;
}
#DiamondCenter {
position: fixed;
top: 2%;
left: 48%;
background: #24201a;
height: 40px;
width: 40px;
transform: rotate(45deg);
z-index: 20 !important;
overflow: hidden;
}
#DiamondCenter:after {
position: absolute;
content: '';
bottom: 0px;
left: -1px;
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: seagreen;
border-left: 2px solid orange;
transform: rotate(40deg) skewX(-20deg);
transform-origin: bottom left;
}
#DiamondCenter:before {
position: absolute;
content: '';
top: -1px;
right: 0px;
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: skyblue;
border-top: 2px solid orange;
transform: rotate(-40deg) skewY(-20deg);
transform-origin: top right;
}
/* Just for demo */
#DiamondCenter{
transition: all 1s;
}
#DiamondCenter:hover{
top: 5%;
height: 80px;
width: 80px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div id="DiamondCenter"></div>
This is quite a complicated shape to complete in CSS, but as shown by others it is possible.
A good alternative though would be to use SVG. Its a vector graphic so it scales brilliantly for responsiveness and is very well supported (CanIUse)
<svg width="50%" height="50%" viewBox="0 0 10 10">
<path d="M5,1
L9,5
L5,9
L1,5z" fill="grey" />
<path d="M1,5
L5,6
L9,5
L5,9z" stroke="orange" stroke-width=".1" stroke-dasharray="0,0,8.23,15" />
</svg>
You can create diamond shape using this css , suppose this is div where you want above shape id should be same as css (cut-diamond)
#cut-diamond {
border-style: solid;
border-color: transparent transparent red transparent;
border-width: 0 25px 25px 25px;
height: 0;
width: 50px;
position: relative;
margin: 20px 0 50px 0;
}
#cut-diamond:after {
content: "";
position: absolute;
top: 25px;
left: -25px;
width: 0;
height: 0;
border-style: solid;
border-color: red transparent transparent transparent;
border-width: 70px 50px 0 50px;
}
JSFiddle Demo

Rounding a corner that's already been affected by a border-radius [duplicate]

I'm having a slight problem with css. I need a trapezoid div which upper left corner(the one with the angle above 90 degrees) is rounded. I already know that this:
HTML:
<div style="margin:30px">
<div class="trapezoid">
</div>
</div>
CSS:
.trapezoid{
vertical-align: middle;
border-bottom: 31px solid red;
border-left: 25px solid transparent;
height: 0;
width: 150px;
}
produces a trapezoid. I tried the border-top-left-radius property, however the effect is not sufficent enough.
Here's a jsfiddle with the above code to, well, fiddle with: http://jsfiddle.net/n3TLP/5/
I there is more info needed just comment.
Thanks in advance :)
Not that you should ever do this, but you can also create a rounded corner trapezoid with a single element by applying CSS 3d transformations:
.trapezoid {
position: relative;
width: 300px;
height: 200px;
overflow: hidden;
}
.trapezoid:after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 200%;
height: 100%;
background: red;
border-radius: 20px 0 0 0;
-webkit-transform-origin: 100% 100%;
-moz-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
-o-transform-origin: 100% 100%;
transform-origin: 100% 100%;
-webkit-transform: perspective(400px) rotateX(45deg);
-moz-transform: perspective(400px) rotateX(45deg);
-ms-transform: perspective(400px) rotateX(45deg);
-o-transform: perspective(400px) rotateX(45deg);
transform: perspective(400px) rotateX(45deg);
}
​
http://jsfiddle.net/RzJTP/
Although I think you're better off using <canvas>/SVG to draw this shape, this is close to what you want:
.trapezoid{
vertical-align: middle;
border-bottom: 120px solid red;
border-left: 200px solid transparent;
border-top-left-radius:30px;
height: 0;
width: 150px;}
/* ---------- */
.trapezoid {
position:relative;
}
.trapezoid:after {
content:' ';
left:-14px;
top:-10px;
position:absolute;
background:red;
border-radius:40px 0 0 0;
width:164px;
height:40px;
display:block;
}
Demo: http://jsfiddle.net/n3TLP/20/
It's not perfect, and you'll have to play with the numbers to get your desired dimensions, it's very finicky. You might be interested in something like Raphaël for drawing, CSS doesn't really have the capacity for complex shapes (at least not intentionally).
Voila:
css:
.trapezoid{
vertical-align: middle;
background: red;
padding-left: 200px;
height: 120px;
width: 150px;
position: relative;
border-top-left-radius: 40px;
overflow: hidden;
background-clip: content-box;
}
.trapezoid:after{
content: '';
margin-left: -100px;
top: 0;
height: 120px;
background: red;
transform: skew(-31deg,0deg);
-o-transform: skew(-31deg,0deg);
-ms-transform: skew(-31deg,0deg);
-moz-transform: skew(-31deg,0deg);
-webkit-transform: skew(-59deg,0deg);
position: absolute;
width: 1000px;
border-top-left-radius: 40px;
}
Demo:
http://jsfiddle.net/n3TLP/24/
Here's my attempt lol
.trapezoid{
position:relative;
border-bottom: 100px solid blue;
border-right: 12px solid transparent;
border-left: 180px solid transparent;
width: 122px;
}
.trapezoid:before{
content:' ';
left:-184px;
top:98px;
position:absolute;
background:blue;
border-radius:80px 20px 80px 80px;
width:318px;
height:20px;
}
.trapezoid:after {
content:' ';
left:-11px;
top:-7px;
position:absolute;
background:blue;
border-radius:150px 50px 90px 0px;
width:133px;
height:30px;
}
<div style="margin:30px">
<div class="trapezoid">
</div>
</div>
http://jsfiddle.net/Bzj3h/
Use Adobe Illustrator or any other software to draw a shape and than save it as SVG code, you can use SVG directly on the page but IE8 and lower will ignore it. If you need to support older versions of IE you can use Raphael.js to draw your SVG element.
Rendering SVG polygons in Raphael Javascript library

White border on overlap CSS

Can this be created using CSS :
I have tried using PNG images :
.x {
position:relative;
top: 100px;
left: 0px;
height: 120px;
width: 300px;
display: block;
}
.y {
position:relative;
top: -20px;
left: 0px;
height: 120px;
width: 300px;
display: block;
transform: rotate(60deg);
}
.z {
position:relative;
top: -140px;
left: 0px;
height: 120px;
width: 300px;
display: block;
transform:rotate(-60deg)
}
<img class="x" src="http://i.stack.imgur.com/Qf8Ot.png">
<img class="y" src="http://i.stack.imgur.com/Qf8Ot.png">
<img class="z" src="http://i.stack.imgur.com/Qf8Ot.png">
But I wanted overlap to be white as the first image. Any clues? Thank you very much.
Create ellipses using border-radius.
Add box-shadow to them :
#a, #a:before, #a:after {
height:80px;
width: 300px;
background: transparent;
border-radius: 50%;
border: 5px solid black;
transform: rotate(30deg);
}
#a {
position: relative;
top:100px;
}
#a:before, #a:after {
position: absolute;
content: "";
box-shadow:inset 0 0 0 4px white, 0 0 0 4px white;
}
#a:before {
transform: rotate(60deg);
}
#a:after {
transform: rotate(120deg);
}
<div id="a"></div>
You should use box-shadow. Make the color of shadow white.
Use inset shadow also, it will make shadow inside the image.
box-shadow :inset 0 0 5px #FFF, 0 0 5px #FFF ;
use -webkit-, -moz- , -o- according to your browser requirments.

Shape resembling a compass pointer or inner part of a Safari logo

I am trying to make the below shape using only CSS. I know that achieving this shape using an image or SVG would be a lot easier but I am trying to achieve it with CSS for a proof of concept.
The below is the code that I have tried so far. It creates a diamond shape by using transform: rotate(45deg) but the diagonals are of the same length whereas the shape that I need has one very long diagonal and another very short.
.separator{
background: #555;
top: 40px;
padding-top: 0px;
margin: 0px 40px;
height: 100px;
width: 100px;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
transform: rotate(45deg);
}
Fiddle Demo
Is it possible to create the shape that I need using CSS?
Note: A similar question was asked earlier and was closed/deleted as "too broad" as it did not show any coding attempt. Posting a new question and self answering it based on this meta discussion. Please feel free to chip in with alternate solutions (or) edit the question to make it more useful for future readers.
For a needle resting on its tip
Yes, it is possible to create that shape using only CSS. You have to rotate the shape along both the Y-axis and the Z-axis to achieve it.
Rotating it along the Z-axis by 45 degrees will produce a diamond shape (as indicated in the question) and rotating it along the Y-axis by close to (but less than) 90 degrees will make only a part of the shape visible from the front and thereby would give it the appearance of having shorter diagonal lines (resembling a compass pointer).
Additionally adding a linear-gradient for the background and a inset box-shadow will help to achieve a shape that is a lot closer to the shape shown in question.
body {
background: #333;
font-family: Calibri;
font-size: 18px;
}
div {
height: 200px;
width: 150px;
display: inline-block;
vertical-align: middle;
color: white;
padding-top: 40px;
}
.separator {
background: #555;
top: 40px;
padding-top: 0px;
height: 160px;
width: 160px;
background-image: linear-gradient(-45deg, #555 0%, #555 40%, #444 50%, #333 97%);
box-shadow: inset 6px 6px 22px 8px #272727;
transform: rotateY(87deg) rotate(45deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div>
Some lengthy paragraph content which wraps around when it exceeds the width
</div>
<div class='separator'></div>
<div>
Some lengthy paragraph content which wraps around when it exceeds the width
</div>
For a needle resting on its base
For a needle that is resting on its base, the rotation should be along the X-axis and Z-axis instead of along Y-axis and Z-axis for the needle resting on its tip. Below is a sample snippet.
body {
background: #AAA;
font-family: Calibri;
font-size: 18px;
}
div {
height: 200px;
width: 150px;
display: inline-block;
vertical-align: middle;
color: white;
padding-top: 40px;
margin: 40px;
}
.separator {
background: #555;
top: 40px;
padding-top: 0px;
height: 160px;
width: 160px;
background-image: linear-gradient(-45deg, #555 0%, #555 40%, #444 50%, #333 97%);
box-shadow: inset 6px 6px 22px 8px #272727;
transform: rotateX(87deg) rotate(45deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='separator'></div>
Compass Pointer created using above method:
Here is a sample compass pointer (inspired in part by the Safari logo) created purely using CSS. The pointer or the needle inside is created using the method explained above.
.container {
position: relative;
height: 152px;
width: 152px;
padding: 10px;
border-radius: 50%;
background: radial-gradient(circle at 50% 50%, white 58%, #999 70%, #EEE 80%);
border: 1px solid #AAA;
}
.dial {
height: 150px;
width: 150px;
border-radius: 50%;
background: linear-gradient(#1ad4fd, #1d65f0 100%);
border: 1px solid #999;
position: relative;
animation: rotatedial 2s 6 alternate forwards;
}
.dial:after {
content: '';
position: absolute;
top: 25px;
left: 25px;
height: 100px;
width: 100px;
background-image: linear-gradient(-45deg, white 0%, white 47%, red 50%);
box-shadow: inset 0px 6px 22px 0px #CCC, inset -6px -6px 22px 0px #AAA;
transform: rotateY(85deg) rotate(45deg);
}
.dial:before {
content: '';
position: absolute;
top: 72px;
left: 70px;
height: 8px;
width: 8px;
background: radial-gradient(circle at 50% 50%, white 30%, grey 100%);
border: 1px solid #999;
border-radius: 50%;
z-index: 2;
}
.hands,
.hands-small {
position: absolute;
height: 150px;
width: 150px;
top: 11.25px;
left: 11px;
z-index: 0;
}
.hands:before,
.hands:after,
.hands .hand:before,
.hands .hand:after {
content: '';
position: absolute;
top: 0;
left: 74.5px;
width: 1px;
height: 12px;
background: #EEE;
border-radius: 4px;
box-shadow: 0px 138px #EEE;
transform-origin: 50% 75px;
}
.hands-small:before,
.hands-small:after,
.hands-small .hand-small:before,
.hands-small .hand-small:after {
content: '';
position: absolute;
top: 0;
left: 74.5px;
width: 1px;
height: 7px;
background: #EEE;
border-radius: 4px;
box-shadow: 0px 143px #EEE;
transform-origin: 50% 75px;
}
.hands:before {
transform: rotate(-45deg);
}
.hands:after {
transform: rotate(0deg);
}
.hand:before {
transform: rotate(45deg);
}
.hand:after {
transform: rotate(90deg);
}
.hands-small:before {
transform: rotate(-22.5deg);
}
.hands-small:after {
transform: rotate(22.5deg);
}
.hand-small:before {
transform: rotate(67.5deg);
}
.hand-small:after {
transform: rotate(112.5deg);
}
#keyframes rotatedial {
0% {
transform: rotate(35deg);
}
100% {
transform: rotate(15deg);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="container">
<div class="dial"></div>
<div class="hands">
<div class="hand"></div>
</div>
<div class="hands-small">
<div class="hand-small"></div>
</div>
</div>
If you stumbled on this page looking for a SVG implementation, have a look at the below snippet:
.separator {
position: relative;
width: 12px;
}
svg {
position: absolute;
top: 0px;
left: 0px;
height: 100%;
width: 100%;
}
path {
fill: url(#MyGradient);
}
path#shade {
stroke: #2E2E2E;
stroke-width: 3;
}
/* Just for the demo to style the divs and position */
body {
background: #333;
font-family: Calibri;
font-size: 18px;
}
.container {
display: flex;
}
.container > .content {
flex: 1;
flex-grow: 1;
color: white;
margin: 20px;
}
<div class='container'>
<div class='content'>Some lengthy paragraph content which wraps around when it exceeds the width.Some lengthy paragraph content which wraps around when it exceeds the width.Some lengthy paragraph content which wraps around when it exceeds the width.</div>
<div class='separator'>
<svg viewBox='0 0 10 200' preserveAspectRatio='none'>
<defs>
<linearGradient id="MyGradient" x1=' 50% ' y1='0% ' x2='50% ' y2='100% '>
<stop offset="0%" stop-color="#333" />
<stop offset="100%" stop-color="#555" />
</linearGradient>
</defs>
<path d='M0,100 5,0 10,100 z' id='shade' />
<path d='M0,100 5,0 10,100 5,200 z ' />
</svg>
</div>
<div class='content '>Some lengthy paragraph content which wraps around when it exceeds the width.Some lengthy paragraph content which wraps around when it exceeds the width.Some lengthy paragraph content which wraps around when it exceeds the width.</div>
</div>