HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
@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;
}
}
}
Also see: Tab Triggers