How to create gradient color - css

I need to create the background color as like an below image.. how to acheive this background color using gradient?
Note: just background color for that pic no need for needle, tick and label.

You can get it by using radial-gradient.
Below i posted a working example to get it, You can play with radial-gradient properties to understand how it's work.
Working fiddle
Radial gradient
.round {
width:300px;
height:300px;
border-radius:50%;
background: rgba(0,0,0,0.8);
background-image:radial-gradient(circle at 50% 30%,#cacaca,#333);
-webkit-background-image:radial-gradient(circle at 50% 30%,#cacaca,#333);
position:relative;
overflow:hidden;
}
.round:after, .round:before {
content:'';
width:100%;
height:100%;
position:absolute;
border-radius:50%;
}
.round:before {
left:0;
top:30px;
transform: rotate(22deg);
transform-origin: -13% 52%;
-webkit-transform-origin: -13% 52%;
background-image: radial-gradient(circle at -11% 30%,#333,#999);
background-image: -webkit-radial-gradient(circle at -11% 30%,#333,#999);
opacity:0.2;
}
.round:after {
right:0;
top:30px;
transform: rotate(22deg);
transform-origin: 22% 125%;
-webkit-transform-origin: 22% 125%;
background-image: radial-gradient(circle at -45% 30%,#999,#333);
-webkit-background-image: radial-gradient(circle at -45% 30%,#999,#333);
opacity:0.2;
}
<div class="round"></div>

Close eh?
Let me know if you need fuller CSS breakdown but personally, I've never used radial gradients in CSS before but it seems to work well.
Here's what I used as a reference.
.container {
position: relative;
width: 300px;
height: 300px;
}
.radial-gradient {
position: absolute;
width: 100%;
height: 100%;
border-style: soild;
border-width: 5px;
border-radius: 50%;
background-image: radial-gradient(circle at 50% 30% , #C3C3C3 0%, #000000 100%);
}
<div class="container">
<div class="radial-gradient">
</div>
</div>

I am able to produce a similar effect using either a radial gradient or the box-shadow property.
Note 1: gradients are rendered differently across different browsers.
Note 2: too much blur in the box-shadow property is bad for performance
Examples (might need some fine-tuning on your end):
.circle {
height: 250px;
width: 250px;
border-radius: 100vw;
background: white;
margin: 1em auto;
}
.gradient {
background-position: -55px -86px;
background-image: radial-gradient(circle, rgb(249, 249, 249) -4%, #000000 81%);
background-repeat: no-repeat;
background-size: 136%;
}
.box-shadow {
box-shadow: inset -7px -28px 140px 48px rgba(0, 0, 0, 0.75);
}
.sample {
text-align: center;
border: 1px solid #444;
width: 300px;
margin: 1em;
}
<div class="sample">Gradient
<div class="circle gradient"></div>
</div>
<div class="sample">Box-shadow
<div class="circle box-shadow"></div>
</div>
Recommendation? Use a an image instead of CSS in this case.

Related

Line with more glow in the middle than start and end

I was looking at some 80's retro design, and came across some glowy stuff, including this one:
Can this be achieved with CSS ? I mean, create a line and do some box shadow. But I have to have more glow in the middle, and less in the sides, and I am not sure how this can be accomplished in just CSS ?
Thanks in advance.
Something like this?
:root {
--clr-inner: #fed9ff;
--clr-outer: #c727c9;
}
body {
background-color: black;
text-align: center;
}
.box {
display: inline-block;
position: relative;
width: 100%;
}
.box .line-outer {
background: var(--clr-outer);
border-radius: 50%;
box-shadow: 0px 0px 25px 10px var(--clr-outer);
filter: blur(1px);
height: 4px;
overflow: hidden;
position: absolute;
width: 100%;
}
.box .line-outer .line-inner {
background: var(--clr-inner);
border-radius: 50%;
box-shadow: 0px 0px 25px 15px var(--clr-inner);
filter: blur(1px);
height: 4px;
left: 30%;
position: absolute;
top: 0;
width: 40%;
}
<div class="box">
<div class="line-outer">
<div class="line-inner"></div>
</div>
</div>
What about using a radial gradient ?
(you can adjust parameters using : https://html-css-js.com/css/generator/gradient/)
#demo {
background: #FFFFFF;
background: -moz-radial-gradient(center, #FFFFFF 0%, #A42799 64%, #000000 100%);
background: -webkit-radial-gradient(center, #FFFFFF 0%, #A42799 64%, #000000 100%);
background: radial-gradient(ellipse at center, #FFFFFF 0%, #A42799 64%, #000000 100%);
height:50px;
width:100%
}
<div id="demo"></div>

CSS custom shape with irregular rectangle and border

I'm trying to create a button like this:
After researching online, I only came up with making a parallelogram. But this is the result:
Code:
.parallelogram {
width: 100px;
height: 50px;
transform: skew(25deg);
background: black;
border: 1px solid #EC00F4;
color: white;
box-shadow: 0px 3px 8px #EC00F4;
}
<button class="parallelogram"> Hello button </button>
Is there a way to make the edges go where I want (like in the picture) but without moving the text ?
Use clip-path on pseudo elements. The trick is to consider the same clip-path and apply a scale transformation to one pseudo element to simulate the border. Simply adjust the value of the polygon to get the needed result.
Hover to see a different clip-path:
.parallelogram {
padding:20px 45px;
font-size:30px;
color: white;
border:none;
background:none;
outline:none;
position:relative;
z-index:0;
margin:15px;
filter:drop-shadow(0px 30px 25px rgba(236, 0, 244, 0.45));
}
.parallelogram:before,
.parallelogram:after {
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
clip-path: polygon(0 11%, 100% 0, 90% 88%, 3% 96%);
transition:1s all;
background:#000;
}
.parallelogram:before {
background:#EC00F4;
transform:scale(1.05,1.12);
}
.parallelogram:hover:before,
.parallelogram:hover:after {
clip-path: polygon(5% 2%, 100% 5%, 100% 100%, 0% 94%);
}
<button class="parallelogram"> Hello button </button>
<button class="parallelogram"> button </button>
<button class="parallelogram"> A </button>
You can also consider pixel value to keep the same shape whataver the content inside:
.parallelogram {
padding:20px 45px;
font-size:30px;
color: white;
border:none;
background:none;
outline:none;
position:relative;
z-index:0;
margin:15px;
filter:drop-shadow(0px 30px 25px rgba(236, 0, 244, 0.45));
}
.parallelogram:before,
.parallelogram:after {
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
clip-path: polygon(0 10px, 100% 0, calc(100% - 8px) calc(100% - 15px), 5px calc(100% - 8px));
transition:1s all;
background:#000;
}
.parallelogram:before {
background:#EC00F4;
transform:scale(1.05,1.12);
}
.parallelogram:hover:before,
.parallelogram:hover:after {
clip-path: polygon(0 5px, 100% 0, 100% 100%, 10px calc(100% - 20px));
}
<button class="parallelogram"> Hello button </button>
<button class="parallelogram"> button </button>
<button class="parallelogram"> A </button>
This works kinda like you want it:
button{
width: 150px;
height: 50px;
-webkit-clip-path: polygon(0% 20%, 92% 14%, 88% 88%, 0% 100%);
clip-path: polygon(0% 20%, 92% 14%, 88% 88%, 0% 100%);
background: black;
color: white;
}
<button class="parallelogram"> Hello button </button>
EDIT:
You can create an SVG that looks exactly like your button here: https://vectr.com/new
You can add border + shadow and simply copy the html.
You could use a pseudo element to set your perspective effect:
example
.parallelogram {
width: 100px;
height: 50px;
position: relative;
color: white;
/* appearance:none; could be used too */
background: none;
border: none;
cursor: pointer; /* show where and that i'm clickable */
}
.parallelogram:before {
content: "";
position: absolute;
z-index: -1;
top: 0;
left: 0;
width: 110%; /* might need to be resized */
height: 100%;
transform: /* tune the rotation and perspective values to your needs */
perspective(200px)
rotatey(35deg)
rotatex(-25deg)
;
background: black;
border: 2px solid #ec00f4;
box-shadow: 0px 3px 8px #ec00f4;
}
<button class="parallelogram"> Hello button </button>
screenshot from firefox :
Add a span, with the class .unskew and do the opposite of your skew effect on the background and change the display rule.
Example:
CSS
.parallelogram {
transition: background 0.3s;
transform: skew(-25deg);
/* SKEW */
}
.unskew{
display: block;
/* block or inline-block is needed */
text-decoration: none;
padding: 5px 10px;
font: 30px/1 sans-serif;
transform: skew(25deg);
/* UNSKEW */
color: inherit;
}
HTML
<button class="parallelogram"><span class="unskew" >Hello button</span></button>

How to achieve "depth" with a 3d css transform

I am trying to create a "perspective mockup" using CSS. There are a fair amount of tutorials on how to achieve this with 3D layers in Photoshop, but I would like to do it with CSS. Here is an example of what I am trying to achieve:
And here is the code (using the raw image, https://i.imgur.com/foDEYpB.png):
#perspective {
width: 400px;
height: 500px;
position: absolute;
background-image: url("https://i.imgur.com/foDEYpB.png");
background-repeat: no-repeat;
background-size: cover;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -250px;
transform: rotate3d(360, 120, -90, 60deg) rotateZ(-30deg);
box-shadow: -15px 15px 20px rgba(0, 0, 0, 0.5);
}
<div id='perspective'></div>
I am pretty close, but am unsure how to achieve the "depth" or "height" where the image looks raised. Zoomed in version of said "depth" where the image is repeated onto the sides:
P.S. if anyone knows the correct name for what I'm referring to as "depth", I'd love to know!
Try adding three type of images to make 3D effects. Use transform property with rotation for images to get the desired result.
Answer reference here.
.perspective {
position: relative;
width: 400px;
height: 500px;
transform-style: preserve-3d;
transition: all 500ms ease-in;
transform: rotateY(20deg) rotateX(60deg) rotateZ(-10deg);
transform: rotateY(15deg) rotateX(50deg) rotateZ(-15deg);
box-shadow: -40px 80px 80px -10px rgba(0, 0, 0, 0.7);
cursor: pointer;
margin-right: 30px;
display: inline-block;
margin-left: 30%;
}
.perspective img {
position: absolute;
top: 0px;
left: 0px;
width: 400px;
height: 500px;
transform: translateZ(16px);
}
.bottom,
.left {
position: absolute;
width: 400px;
height: 500px;
display: block;
transition: all 1s linear;
overflow: hidden;
border-radius: 3px;
transform: translateZ(16px);
filter: brightness(80%)
}
.left {
transform: rotateY(270deg) translateX(-1px);
transform-origin: center left;
width: 18px;
}
.bottom {
transform: rotateX(90deg) translateY(15px) translateZ(-480px);
transform-origin: bottom center;
height: 18px;
}
.bottom img {
transform: rotateX(180deg);
width: 100%;
height: 500px;
left: 0px;
}
<div class="perspective">
<img src="https://i.imgur.com/foDEYpB.png">
<div class="bottom"><img src="https://i.imgur.com/foDEYpB.png"></div>
<div class="left"><img src="https://i.imgur.com/foDEYpB.png"></div>
</div>
Here is a hacky idea using multiple background to simulate such effect. The trick is to add 2 semi-transparent gradients to create the shadow effect then 2 other gradient to cut a small part of the corner to obtain the 3D shape.
The result may not be perfect for all the images:
.wrapper {
display:inline-block;
perspective:1000px;
}
.box {
margin: 50px;
width:200px;
height:200px;
transform: rotate3d(360, 120, -90, 60deg) rotateZ(-30deg);
background:
linear-gradient(to bottom right,transparent 49%,#fff 52%) bottom right/14px 10px,
linear-gradient(to top left,transparent 49%,#fff 52%) top left /10px 14px,
linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)) 0 0px/10px 100%,
linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)) 100% 100%/calc(100% - 10px) 10px,
url(https://picsum.photos/id/1061/1000/800) center/cover;
background-repeat:no-repeat;
}
<div class="wrapper" >
<div class="box" >
</div>
</div>
With your image you can have a specific gradient like below:
body {
background:#ccc;
}
.wrapper {
display:inline-block;
perspective:1000px;
}
.box {
margin: 50px;
width:200px;
height:250px;
transform: rotate3d(360, 120, -90, 60deg) rotateZ(-30deg);
background:
linear-gradient(to bottom right,transparent 49%,#ccc 52%) bottom right/16px 10px,
linear-gradient(to top left,transparent 49%,#ccc 52%) top left /10px 12px,
linear-gradient(#efefef,#efefef) 100% 100%/calc(100% - 10px) 10px,
linear-gradient(-226deg,#222428 13px,#ff4946 13px,#ff4946 77px,#592D30 77px,#592D30 100px,#222428 100px,#222428 108px,#efefef 108px,#efefef 161px) 0 0px/10px 100%,
url(https://i.imgur.com/foDEYpB.png) center/cover;
background-repeat:no-repeat;
}
<div class="wrapper">
<div class="box">
</div>
</div>

Achieve object projection effect with box-shadow

I am trying to achieve this effect in CSS:
This is my code:
#test {position: relative;margin: 100px;}
#test::after {
background-color: maroon;
box-shadow: 0 -50px 10px 7px gray;
height: 45px;
left: -15px;
position: absolute;
top: 40px;
transform: perspective(150px) rotateX(-45deg);
transform-origin: center bottom 0;
width: 60px;
content: "";
}
<div id="test"></div>
but I am not achieving the expected result with the cast shadow. I wonder if its even possible to do this with CSS only?
Fiddle Demo
Maybe something like this? I added another element representing the shadow:
#shadow {
height: 90px;
left: -15px;
position: absolute;
top: 30px;
background: rgba(0, 0, 0, 0);
width: 60px;
transform: perspective(50px) rotateX(25deg);
box-shadow: 0 -106px 20px 17px #808080;
}
https://jsfiddle.net/zcyy09mp/4/
As mentioned in my comment, I would generally recommend the approach used in my fiddle (which is, use another pseudo-element) or the one in Martin's answer (which is, to use an extra element) but as you've mentioned that the other pseudo-element is already used and you are trying to avoid any extra elements, the other approach is to use gradients as background for the parent element. By using the appropriate side-to-side gradients with background-position, background-size, we can not only get the shape but also an effect very similar to the blurred nature of the shadow.
Below is a sample snippet: (the output is also reasonably responsive as you can see by hovering it)
#test {
position: relative;
height: 100px;
width: 100px;
margin: 100px;
background: linear-gradient(to bottom right, transparent 45%, gray 55%), linear-gradient(to bottom left, transparent 45%, gray 55%), linear-gradient(to bottom, transparent, gray), linear-gradient(gray, gray);
background-repeat: no-repeat;
background-size: 30px 95%, 30px 95%, calc(100% - 60px) 8px, calc(100% - 60px) calc(100% - 8px);
background-position: 0% 100%, 100% 100%, 50% 4px, 50% 100%;
}
#test::after {
position: absolute;
content: "";
background-color: maroon;
width: 100%;
height: 45%;
left: 0px;
top: 100%;
transform: perspective(150px) rotateX(-45deg);
transform-origin: center top 0;
}
/* just for demo */
#test {
transition: all 1s;
}
#test:hover {
height: 200px;
width: 200px;
}
<div id="test"></div>
In the below snippet, I have given a different color for each of the gradient just to visually show how it is achieved.
#test {
position: relative;
height: 100px;
width: 100px;
margin: 100px;
background: linear-gradient(to bottom right, transparent 45%, red 55%), linear-gradient(to bottom left, transparent 45%, blue 55%), linear-gradient(to bottom, transparent, green), linear-gradient(rebeccapurple, rebeccapurple);
background-repeat: no-repeat;
background-size: 30px 95%, 30px 95%, calc(100% - 60px) 8px, calc(100% - 60px) calc(100% - 8px);
background-position: 0% 100%, 100% 100%, 50% 4px, 50% 100%;
}
#test::after {
position: absolute;
content: "";
background-color: maroon;
width: 100%;
height: 45%;
left: 0px;
top: 100%;
transform: perspective(150px) rotateX(-45deg);
transform-origin: center top 0;
}
/* just for demo */
#test {
transition: all 1s;
}
#test:hover {
height: 200px;
width: 200px;
}
<div id="test"></div>
According to the W3 spec, "the 'box-shadow' property attaches one or more drop-shadows to the box". The shadow you want to create is not a drop shadow so there is no CSS that would make the shadow in the picture.
The closest you could achieve is pushing the shadow off one edge by using a negative spread radius:
body {
padding-top: 50px;
}
#test {
margin: 0 auto;
background-color: maroon;
box-shadow: 0 -20px 7px -6px black;
height: 45px;
width: 60px;
transform: perspective(150px) rotateX(-45deg);
transform-origin: center bottom 0;
}
<div id="test"></div>

Nested div masked off by circular container div

I'm trying to figure out if there's a way to do this with CSS: I have a container div with a border-radius of 50% (circular). Inside of that is a rectangular div with a height of 30% positioned at the bottom of the container, and I want to be able to mask that off so that anything outside of the container's rounded border radius doesn't show. How do I accomplish this? Attached is a screenshot of what's currently happening, and this is my code:
<div id="coupon_container">
<div id="meter_container">50c off</div>
</div>
#coupon_container {
position: fixed; right:0; top:0; z-index: 100; color: #fff; width:170px; height: 120px;
#meter_container {
position: absolute; width: 110px; height:110px; .round; background: #greenDk; border:5px solid #fff; left: 60px; overflow: hidden;
.meter_level { width: 100%; height:30%; position: absolute; bottom:0; text-align: center; font-size: 1.6em; background: #limeLt; }
}
}
I really like the gradient solution that bookcasey has posted. However, compatibility may be a drawback as IE9 doesn't support CSS gradients. So another solution would be this one:
demo
The idea is to use a top padding of 70% instead of absolute positioning.
HTML:
<div id="coupon_container">
<div id="meter_container">50c off</div>
</div>
CSS:
#coupon_container {
overflow: hidden;
width: 8em; height: 8em;
border-radius: 50%;
background: green;
}
#meter_container {
margin: 70% 0;
height: 30%;
text-align: center;
background: lime;
}
You can achieve the effect you want using CSS3 gradients:
#coupon_container {
width: 100px;
height: 100px;
border-radius: 100px;
background: -webkit-gradient(linear, 50% 0%, 50% 70, color-stop(100%, #fa8072), color-stop(100%, #ff0000));
background: -webkit-linear-gradient(top, #fa8072 70px, #ff0000 70px);
background: -moz-linear-gradient(top, #fa8072 70px, #ff0000 70px);
background: -o-linear-gradient(top, #fa8072 70px, #ff0000 70px);
background: linear-gradient(top, #fa8072 70px, #ff0000 70px);
position: relative;
}
#meter_container {
width: 100px;
text-align: center;
position: absolute;
bottom: 10px;
}
Demo
I could be totally missing something, but couldn't you just add "overflow: hidden;" to the round element, #coupon_container?

Resources