Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
<h1>SVG vs CSS Linear Gradients</h1>

    <p class="intro">SVG and its <code>linearGradient</code> element allow to achieve similar results to those obtained with CSS's <code>linear-gradient()</code> function. A great benefit of using SVG is the possibility to create it using external graphic tools and then simply embedding in HTML. Once embedded, JS or CSS can be applied to it!</p>

    <article class="challenges">
        <section>
            <span>Default</span>
            <div class="css" style="background: linear-gradient(black, darkcyan)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientDefault">
                            <stop offset="0%" stop-color="black" />
                            <stop offset="100%" stop-color="darkcyan" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientDefault')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Gradient Direction</span>
            <div class="css" style="background: linear-gradient(0.25turn, black, darkcyan)"></div>
            <div class="css" style="background: linear-gradient(to bottom right, black, darkcyan)"></div>
            <div class="css" style="background: linear-gradient(45deg, black, darkcyan)"></div>

            <div class="svg">
                <!-- x1 y1 x2 y2 -->
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientXY" x1="0" y1="0" x2="100%" y2="100%">
                            <stop offset="0%" stop-color="black" />
                            <stop offset="100%" stop-color="darkcyan" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientXY')" />
                </svg>
            </div>
            <div class="svg">
                <!-- rotate -->
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientRotate" style="transform: rotate(45deg);">
                            <stop offset="0%" stop-color="black" />
                            <stop offset="100%" stop-color="darkcyan" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientRotate')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Gradient Hint / Midpoint Positioning</span>
            <div class="css" style="background: linear-gradient(to right, darkcyan, 25%, brown)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientMidpoint">
                            <stop offset="0%" stop-color="darkcyan" />
                            <!-- two stops need to be added - graphic tool needed -->
                            <stop offset="0.2" stop-color="#416565"/>
                            <stop offset="0.42" stop-color="#853D3D"/>

                            <stop offset="100%" stop-color="brown" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientMidpoint')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Three Colors</span>
            <div class="css" style="background: linear-gradient(to right, black, cyan, darkcyan)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientThreeColors">
                            <stop offset="0%" stop-color="black" />
                            <stop offset="50%" stop-color="cyan" />
                            <stop offset="100%" stop-color="darkcyan" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientThreeColors')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Positioning Middle Color</span>
            <div class="css" style="background: linear-gradient(to right, black, cyan 25%, darkcyan)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientPositionMiddle">
                            <stop offset="0%" stop-color="black" />
                            <stop offset="25%" stop-color="cyan" />
                            <stop offset="100%" stop-color="darkcyan" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientPositionMiddle')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Rainbow</span>
            <div class="css" style="background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientRainbow">
                            <stop offset="0%" stop-color="red" />
                            <stop offset="16%" stop-color="orange" />
                            <stop offset="33%" stop-color="yellow" />
                            <stop offset="50%" stop-color="green" />
                            <stop offset="67%" stop-color="blue" />
                            <stop offset="83%" stop-color="indigo" />
                            <stop offset="100%" stop-color="violet" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientRainbow')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Sharp Stripes</span>
            <div class="css" style="background: linear-gradient(to right, red 14%, orange 14%, orange 28%, yellow 28%, yellow 42%, green 42%, green 58%, blue 58%, blue 72%, indigo 72%, indigo 86%, violet 86%)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientRainbowStripes">
                            <stop offset="14%" stop-color="red" />
                            <stop offset="14%" stop-color="orange" />
                            <stop offset="28%" stop-color="orange" />
                            <stop offset="28%" stop-color="yellow" />
                            <stop offset="42%" stop-color="yellow" />
                            <stop offset="42%" stop-color="green" />
                            <stop offset="58%" stop-color="green" />
                            <stop offset="58%" stop-color="blue" />
                            <stop offset="72%" stop-color="blue" />
                            <stop offset="72%" stop-color="indigo" />
                            <stop offset="86%" stop-color="indigo" />
                            <stop offset="86%" stop-color="violet" />
                            <stop offset="100%" stop-color="violet" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientRainbowStripes')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Transparency</span>
            <div class="css" style="background: linear-gradient(to right, red, transparent)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientTransparent">
                            <stop offset="0%" stop-color="red" />
                            <stop offset="100%" stop-color="transparent" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientTransparent')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Layers</span>
            <div class="css" style="background:
                        linear-gradient(to top, #FF0000AA, #FF000000),
                        linear-gradient(to bottom right, #00FF00AA, #00FF0000),
                        linear-gradient(to bottom left, #0000FFAA, #0000FF00)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientLayersRed" x1="0" y1="100%" x2="0" y2="0">
                            <stop offset="0%" stop-color="red" stop-opacity="67%" />
                            <stop offset="100%" stop-color="red" stop-opacity="0%"/>
                        </linearGradient>
                        <linearGradient id="gradientLayersGreen" x1="0" y1="0" x2="100%" y2="100%">
                            <stop offset="0%" stop-color="green" stop-opacity="67%" />
                            <stop offset="100%" stop-color="green" stop-opacity="0%"/>
                        </linearGradient>
                        <linearGradient id="gradientLayersBlue" x1="100%" y1="0" x2="0" y2="100%">
                            <stop offset="0%" stop-color="blue" stop-opacity="67%" />
                            <stop offset="100%" stop-color="blue" stop-opacity="0%"/>
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientLayersRed')" />
                    <rect width="100" height="100" fill="url('#gradientLayersGreen')" />
                    <rect width="100" height="100" fill="url('#gradientLayersBlue')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Mist Effect</span>
            <div class="css" style="background:
                        linear-gradient(to bottom, mistyrose, transparent),
                        url('https://images.pexels.com/photos/2416864/pexels-photo-2416864.jpeg?auto=compress&cs=tinysrgb&w=600');
                        background-size: cover;
            "></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientMist" x1="0" y1="0" x2="0" y2="100%">
                            <stop offset="0%" stop-color="mistyrose" stop-opacity="100%" />
                            <stop offset="100%" stop-color="mistyrose" stop-opacity="0%"/>
                        </linearGradient>
                    </defs>
                    <image href="https://images.pexels.com/photos/2416864/pexels-photo-2416864.jpeg?auto=compress&cs=tinysrgb&w=600" height="100" width="100" />
                    <rect width="100" height="100" fill="url('#gradientMist')" />
                </svg>
            </div>
        </section>

        <section>
            <span>Repeated Gradient</span>
            <div class="css" style="background: repeating-linear-gradient(to right, black, darkcyan 10%, black 20%)"></div>

            <div class="svg">
                <svg viewBox="0 0 100 100">
                    <defs>
                        <linearGradient id="gradientRepeated" x1="0" y1="0" x2="20%" y2="0" spreadMethod="repeat">
                            <stop offset="0%" stop-color="black" />
                            <stop offset="50%" stop-color="darkcyan" />
                            <stop offset="100%" stop-color="black" />
                        </linearGradient>
                    </defs>
                    <rect width="100" height="100" fill="url('#gradientRepeated')" />
                </svg>
            </div>
        </section>
    </article>
</main>
              
            
!

CSS

              
                @import "https://necolas.github.io/normalize.css/8.0.1/normalize.css";
@import url('https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,300;0,400;0,600;0,700;0,900;1,400;1,600;1,700;1,900&display=swap');

* {
  box-sizing: border-box;
}

body {
  background-color: #092927;
  color: white;
  font-family: 'Montserrat', sans-serif;
}

main {
  width: 100%;
  margin: 2em auto;
  @media (min-width: 400px) {
    width: 80%;
  }
}

p.intro {
  color: white;
  font-size: 0.9em;
  text-align: justify;
  margin: 1em auto;
}

h1 {
  text-align: center;
  font-weight: 900;
  font-size: 2.5em;
}

.challenges {
  display: flex;
  flex-direction: column;
  gap: 1em;

  section {
    position: relative;
    padding-top: 2em;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 1em;
    align-content: space-between;
    justify-content: center;

    div {
      width: 200px;
      height: 200px;
      border: 1px solid white;
      position: relative;

      &.css {
        &:before {
          content: "CSS";
          display: inline-block;
          position: absolute;
          left: 0;
          bottom: 0;
          padding: 0.5em;
          background-color: #00000055;
        }
      }

      &.svg {
        &:before {
          content: "SVG";
          display: inline-block;
          position: absolute;
          left: 0;
          bottom: 0;
          padding: 0.5em;
          background-color: #00000055;
        }
      }
    }

    span {
      position: absolute;
      top: 0;
      font-weight: 600;
    }
  }
}

              
            
!

JS

              
                
              
            
!
999px

Console