user1278673
user1278673

Reputation:

Sunburst effect with css3 gradient

I have been looking around and trying for a few days, but i just cant seem to get it 100% right... i am trying to achieve the following effect with css3 gradient:

sunburst done in illustrator

the closest i have gotten is DEMO:

html {
  background:
    linear-gradient(80deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(90deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(82deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(67deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(52deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(37deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(22deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(7deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(-8deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(-23deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(-38deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(-53deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(-68deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(-83deg, transparent 50%, #ddd 50%, #ddd);
  background-position: center -100%;
  background-color: #eee;
  background-size: 100% 200%;
  min-height: 100%;
}

I will continue attempting it.. any help is greatly Appreciated though.

Update:

There has to be a better/reusable way of doing this... looking into a scss solution, here is what i have thus far:

.sunburst {
  @for $ray from 1 through 26 {
    $color: #eee;
    $degree: 7;
    @if $ray%2 == 0 {
      $color: #ddd;
    }
    background:linear-gradient($degree+deg, transparent 50%, $color 50%, $color),
  }
}

Now its just the actual maths behind it i am trying to figure out... attempting to steal logic from pow.js, but kind of difficult if your as terrible at maths as i am...

Upvotes: 7

Views: 6306

Answers (5)

Jax-p
Jax-p

Reputation: 8551

Currently, there is a repeating-conic-gradient, which creates an image consisting of a repeating gradient.

div {
  height: 500px;
  background: repeating-conic-gradient(
    hsl(186deg 100% 50% / 31%) 0deg 15deg,
    hsla(0,0%,100%,0) 0deg 30deg
  ) #1c2c3c
}
<div></div>

You read more about it at W3 CSS Image Values.
This property is not compatible with all browsers. Check caniuse for more information.

Upvotes: 0

frumbert
frumbert

Reputation: 2427

In modern chrome-based browsers there are conic gradients which do this.

       div {
       height:250px;
          background-image:
             repeating-conic-gradient(#fff 0 9deg, #000 9deg 18deg);
        }
<div></div>

Upvotes: 5

Leo
Leo

Reputation: 13848

@chipChocolate.py gave a brilliant solution! This is an improvement based on his.

  1. In Firefox transparent behaves like rgba(0,0,0,0) which leaves a thin gray line at the edge. Change to rgba(255,255,255,0) looks better.

  2. Make the visual effect closer to OP's screenshot: 36 strips, each occupies a 10 degree angle.

  3. Effective on <html> tag, like OP's try.

BTW: Chrome's render engine sucks, best viewed in Firefox.

html {
  height: 100%;
  position: relative;
}

html:before, html:after {
  content: '';
  height: 100%;
  width: 50%;
  position: absolute;
  top: 0;
  background-size: 200% 100%;
  background-image: linear-gradient(85deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(75deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(65deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(55deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(45deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(35deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(25deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(15deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(5deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(-5deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(-15deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(-25deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(-35deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(-45deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(-55deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(-65deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(-75deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                    linear-gradient(-85deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                    linear-gradient(-95deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db);
}

html:before {
  left: 50%;
  transform: rotate(180deg);
}

Upvotes: 0

Weafs.py
Weafs.py

Reputation: 22992

You could use :before and :after :pseudo-elements to get this effect.

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
}
#grad {
  position: relative;
  width: 100%;
  height: 100%;
}
#grad:after, #grad:before {
  content: '';
  position: absolute;
  background: linear-gradient(90deg, transparent 50%, black 50%, black), linear-gradient(82deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(67deg, transparent 50%, #000000 50%, #000000), linear-gradient(52deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(37deg, transparent 50%, #000000 50%, #000000), linear-gradient(22deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(7deg, transparent 50%, #000000 50%, #000000), linear-gradient(-8deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(-23deg, transparent 50%, #000000 50%, #000000), linear-gradient(-38deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(-53deg, transparent 50%, #000000 50%, #000000), linear-gradient(-68deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(-83deg, transparent 50%, #000000 50%, #000000), linear-gradient(-90deg, transparent 50%, #12E0DB 50%, #12E0DB);
  background-position: 0% 0%;
  background-size: 200% 100%;
  height: 100%;
  width: 50%;
}
#grad:before {
  left: 50%;
  transform: rotate(180deg);
}
<div id="grad"></div>

Upvotes: 5

MMachinegun
MMachinegun

Reputation: 3074

your background-postition is set to center -100%;. Just use

background-position: center center;

Now with this change and your provided code half of your image is already done ;). Just add the second half with more linear-gradients

Upvotes: 0

Related Questions