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.
<div class="controls">
<h2 class="controls__headline">
Playground:
</h2>
<div class="angle-control">
<input type="range" id="angle-control" min="-60" max="60" step="1" value="-11">
<span id="angle-result" class="result">-11 deg</span>
</div>
</div>
<div class="diagonal-box">
<div class="content">
<h1>Tips for Pure CSS Diagonal Layouts</h1>
<p class="intro">
Below you will find a few tips for creating diagonal layouts. If this is all too fast for you, check out this <a href="https://9elements.com/blog/pure-css-diagonal-layouts/" target="_blank">step-by-step tutorial</a>.
</p>
</div>
</div>
<div class="diagonal-box bg-one">
<div class="content">
<h2>1. Skew to the rescue.</h2>
<p>
When you <a href="#">rotate</a> a 100%-width box, you get some ugly corners and need to
make the whole box wider than 100%. The problem here is that you maybe
don't know the height of the element, and then you also don't know how
much wider than 100% it has to be.
</p>
<p>
So instead of <code>transform: rotate(-11deg)</code> use
<code>transform: skewY(-11deg)</code> and the transformed section stays
within it's horizontal boundaries.
</p>
<div class="columns">
<figure class="figure">
<svg viewBox="0 0 280 280">
<rect
fill="none"
x="1"
y="1"
width="278"
height="278"
stroke="#FFF"
stroke-width="2"
/>
<rect
class="object object--rotate"
width="270"
height="130"
x="5"
y="75"
fill="#FFF"
opacity="0.4"
/>
</svg>
<figcaption>transform: rotate(-11deg);</figcaption>
</figure>
<figure class="figure">
<svg viewBox="0 0 280 280">
<rect
fill="none"
x="1"
y="1"
width="278"
height="278"
stroke="#FFF"
stroke-width="2"
/>
<rect
class="object object--skew"
width="270"
height="130"
x="5"
y="75"
fill="#FFF"
opacity="0.4"
/>
</svg>
<figcaption>transform: skew(-11deg);</figcaption>
</figure>
</div>
</div>
</div>
<div class="diagonal-box bg-two">
<div class="content">
<h2>2. Use a pseudo-element.</h2>
<p>
If you want diagonal sections, but still write horizontally, you need to
re-transform the content inside the section. What you can do instead is
insert a <code>:before</code> pseudo-element, position it
<code>absolute</code> and then transform this element instead of the
section itself.
</p>
<div class="columns">
<figure class="figure">
<svg viewBox="0 0 280 280">
<rect
fill="none"
x="1"
y="1"
width="278"
height="278"
stroke="#FFF"
stroke-width="2"
/>
<rect
class="object object--skew-pause"
width="270"
height="130"
x="5"
y="75"
fill="#FFF"
opacity="0.4"
/>
<rect
class="object object--skew-pause-alt"
width="220"
height="80"
x="30"
y="100"
fill="#FFF"
opacity="0.4"
/>
</svg>
<figcaption>
The content needs to be re-transformed, when you transform the whole
section.
</figcaption>
</figure>
<figure class="figure">
<svg viewBox="0 0 280 280">
<rect
fill="none"
x="1"
y="1"
width="278"
height="278"
stroke="#FFF"
stroke-width="2"
/>
<rect
class="object object--skew-pause"
width="270"
height="130"
x="5"
y="75"
fill="#FFF"
opacity="0.4"
/>
<rect
class="object"
width="220"
height="80"
x="30"
y="100"
fill="#FFF"
opacity="0.4"
/>
</svg>
<figcaption>
Content is not affected when transforming a pseudo-element in the
background.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="diagonal-box bg-three">
<div class="content">
<h2>3. Find the right padding.</h2>
<p>
Because of the transformation, some elements <em>bleed</em> into the previous and the next element. To find a safe area where you can place content, you need to add some padding. The amount of padding can be calculated with this formula:<br>
<span class="formula">x = tan(α) * a / 2</span>
</p>
<svg viewBox="0 0 560 305">
<g fill="none" fill-rule="evenodd">
<rect
width="558"
height="198"
x="1"
y="53"
stroke="#FFF"
stroke-width="2"
/>
<polygon
fill="#FFF"
points="6 106.52 554 0 554 198 6 304.52"
opacity=".3"
/>
<line
x1="554.5"
x2="554.5"
y2="51"
stroke="#FFF"
stroke-dasharray="2 2"
stroke-width="2"
/>
<line
x1="6.5"
x2="6.5"
y1="53"
y2="104"
stroke="#FFF"
stroke-dasharray="2 2"
stroke-width="2"
/>
<path
fill="#FFF"
fill-rule="nonzero"
d="M325.1 33.3c1.3 0 2-1.7 2-3.7h-.3c-.2 1.5-.5 2-.9 2-.7 0-1.5-2.3-1.7-3.5l2.8-6.8h-2.5l-1.2 3.5c-.5-1.8-1.7-3.8-4-3.8-3.2 0-5.1 2.9-5.1 6.2 0 3.5 2 6 4.9 6 2 0 3-1.2 4-2.8.3 1.7 1 2.9 2 2.9zm-5.8-.8c-2.3 0-2.8-4.7-2.8-6.6 0-3 1.4-4.1 2.7-4.1 1.9 0 2.6 3.9 2.8 5.8-.2 1.4-1 5-2.7 5z"
/>
<text fill="#FFF" font-family="Raleway" font-size="26">
<tspan x="273" y="153">a</tspan>
</text>
<text fill="#FFF" font-family="Raleway" font-size="26">
<tspan x="17.1" y="88">x</tspan>
</text>
<text fill="#FFF" font-family="Raleway" font-size="26">
<tspan x="530.1" y="35">x</tspan>
</text>
<line
x1="6"
x2="554"
y1="165"
y2="165"
stroke="#FFF"
stroke-dasharray="2 2"
stroke-width="2"
/>
<path
stroke="#FFF"
stroke-dasharray="1 1"
d="M346 53c0-4.1-.4-8.1-1-12"
/>
</g>
</svg>
<p>Sadly you can not make this completely dynamic without javascript, as CSS calculations don't support <code>sin</code>, <code>cos</code> and <code>tan</code>. </p>
<p><strong>Pro Tip:</strong> I think most of you will use deg as unit, when you do the transformation: <code>skewY(-11deg)</code>. <br>
If you do so, you also have to use <strong>Deg</strong> and not <Strong>Rad</Strong> when you calculate tangens. The standard <a href="https://www.google.com/search?q=calculator" target="_blank">google calculator</a> uses <strong>Rad</strong> as default.</p>
</div>
</div>
<div class="diagonal-box bg-one">
<div class="content">
<h2>4. Use CSS-Variables to store the padding-value.</h2>
<p>You can use CSS Custom Properties to store the calculated value for the needed padding and reuse it. For example you can translate elements so that they are in line with the diagonal background-line.</p>
<p><code>transform: translateY(var(--skew-padding))</code></p>
<div class="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
</div>
<div class="diagonal-box bg-two">
<div class="content">
<h2>And that's it</h2>
<p>
If this all went too fast for you, you find a more <a href="https://9elements.com/blog/pure-css-diagonal-layouts/" target="_blank">detailed article here</a>. And for all further questions, you can find me on <a href="https://twitter.com/supremebeing09" target="_blank">Twitter</a>.
Thanks for reading.
</p>
</div>
</div>
<div class="clip-path">
<div class="content">
<h2>Update 28. Feb. 2020:<br>Combine with Clip-Path</h2>
<p>Quite a few people mentioned to me that you could also do this by using clip-path. So I added this section here, where there is no skew-transform, but parts of the section are hidden with clip-path.</p>
<p>This technique works fine as well, only the calculation of the padding is a little harder, as you need both the width of the container and the width of the viewport.</p>
<p>The significant advantage, though: you can place background-images to the section without them being transformed.</p>
</div>
</div>
:root {
--width: 100vw;
--full-width: 100vw;
@media (min-width: 42em) {
--width: 42rem;
}
--angle: -11deg;
/*-----------------
Magic Number Alert:
the 0.09719 is calculated by this formula:
tan(11°) / 2
11° is the current skewY value.
------------------*/
--magic-number: 0.09719;
--skew-padding: calc(var(--width) * var(--magic-number));
--clip-padding: calc(var(--full-width) * var(--magic-number));
}
.diagonal-box {
position: relative;
padding: var(--skew-padding) 0;
margin-top: -1px;
&:before {
content: "";
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
transform: skewy(var(--angle));
transform-origin: 50% 0;
outline: 1px solid transparent;
backface-visibility: hidden;
}
}
.bg-one:before {
background-image: linear-gradient(45deg, #654ea3, #eaafc8);
}
.bg-two:before {
background-image: linear-gradient(-135deg, #ff0084, #33001b);
}
.bg-three:before {
background-image: linear-gradient(-135deg, #007, #003);
}
.content {
max-width: var(--width);
margin: 0 auto;
padding: 1.5em;
position: relative;
/* -----------
enable the border to see, that the content
perfectly fits into the section withou
bleeding into the adjecting areas:
------------ */
// border: 2px dashed #fff8;
}
/* --------------------
Clip Path Update
-------------------- */
.clip-path {
position: relative;
margin-top: calc( ( var(--clip-padding) * -1 ) - 2px );
background-image:
linear-gradient(rgba(0,0,0,0.05) 50%, 0, transparent 100%),
linear-gradient(-135deg, #0cc, #066);
background-size: .5em .5em, 100% 100%;
padding: calc( ( var(--clip-padding) * 2 ) - ( var(--clip-padding) - var(--skew-padding) ) ) 0 4em;
clip-path: polygon(
0% calc(var(--clip-padding) * 2),
100% 0%,
100% 100%,
0% 100% );
-webkit-clip-path: polygon(
0% calc(var(--clip-padding) * 2),
100% 0%,
100% 100%,
0% 100% );
}
/* --------------------
Presentational Styles
-------------------- */
*, *:before, *:after {
box-sizing: border-box;
}
html {
font-size: 100%;
transition: font-size 0.2s linear;
@media (min-width: 70em) {
font-size: 125%;
}
}
body {
background: #003;
padding-top: 8em;
color: #fff;
font-family: 'Raleway', sans-serif;
}
h1 {
text-align: center;
margin:0 auto 1em;
padding: 0 1em;
max-width: 42rem;
line-height: 1.2;
transform: skewY(var(--angle));
font-size: 3em;
text-transform: uppercase;
font-weight: 900;
}
h2 {
font-size: 2.5em;
margin: 0 0 0.5em;
font-weight: 900;
}
.intro {
font-size: 1.25em;
transform: skewY(var(--angle));
margin: 0em auto 0em;
text-align: center;
background: #fff;
color: #003;
font-weight: 900;
padding: 0.5em;
text-transform: uppercase;
a {
background-image: linear-gradient(transparent 90%, 0, #003 100%);
background-image: none;
border-bottom: 4px solid;
transition: none;
&:hover {
border-color: #a06;
opacity: 1;
}
}
}
p {
font-size: 1.25em;
margin: 0;
line-height: 1.5;
& + &,
svg + & {
margin-top: 1em;
}
code {
background: #0033;
padding: 0.125em 0.375em;
border-radius: 0.125em;
@media (min-width: 35em) {
white-space: nowrap;
}
}
a {
color: inherit;
text-decoration: none;
background-image: linear-gradient(transparent 90%, 0, #fffa 100%);
padding: 0.125em 0;
//display: inline-block;
transition: opacity 0.3s ease-out;
&:hover {
text-decoration: none;
opacity: 0.8;
}
}
}
.columns {
display: flex;
margin: 2em -1em;
}
.figure {
display: block;
width: 100%;
margin: 0 1em;
svg {
display: block;
width: 100%;
}
.object {
transform-origin: 140px 140px;
&--rotate {
animation: rotate 3s ease-in-out alternate infinite;
}
&--skew {
animation: skew 3s ease-in-out alternate infinite;
}
&--skew-pause {
animation: skew-pause 6s ease-in-out infinite;
}
&--skew-pause-alt {
animation: skew-pause-alt 6s ease-in-out infinite;
}
}
figcaption {
margin-top: 0.5em;
line-height: 1.5;
font-weight: 700;
opacity: 0.9;
}
}
@keyframes rotate {
0% {transform: rotate(0deg);}
50% {transform: rotate(-11deg);}
100% {transform: rotate(-11deg) scaleX(1.2);}
}
@keyframes skew {
to {transform: skewY(-11deg);}
}
@keyframes skew-pause {
0%, 70%, 100% {transform: skewY(0deg);}
20%, 50% {transform: skewY(-11deg);}
}
@keyframes skew-pause-alt {
0%, 40%, 100% {transform: skewY(0deg);}
20%, 30% {transform: skewY(-11deg);}
}
.formula {
font-family: monospace;
font-size: 1.5em;
display: block;
margin: 1em auto;
text-align: center;
@media (min-width: 26em) {
font-size: 2em;
}
}
.boxes {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 3%;
margin: 2em 0;
--translation: 0;
.box {
width: 100%;
height: 0;
padding-bottom: 100%;
border: 1px solid #fff;
background: #fff3;
transform: translateY( var(--translation) );
animation: translate 3s ease-in-out infinite;
&:nth-child(1) { --translation: calc(var(--skew-padding) * 1.5)}
&:nth-child(2) { --translation: calc(var(--skew-padding) * 1)}
&:nth-child(3) { --translation: calc(var(--skew-padding) * 0.5)}
&:nth-child(4) { --translation: calc(var(--skew-padding) * 0)}
}
}
@keyframes translate {
0%, 20%, 100% { transform: translateY(0); }
50%, 70% { transform: translateY(var(--translation)); }
}
/* ---------------------------------
Interactive Controls
--------------------------------- */
.controls {
background: #FFF3;
z-index: 5;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
border-radius: 0 0 0.5em 0.5em;
max-width: 90%;
&__headline {
color: #fff;
margin: 0.75em 1.125em 0.625em;
font-size: 1em;
text-align: center;
font-weight: 400;
}
}
.angle-control {
padding: 0.75em 0.625em 0.625em;
margin: 0 0.5em;
font-size: 1em;
border-top: 1px solid #fff3;
display: flex;
> * {
vertical-align: middle;
margin: 0 0.5em;
}
span {
display: inline-block;
min-width: 6ch;
}
input {
width: 8em;
flex-shrink: 1;
}
}
.result {
text-align: right;
}
[hidden] {
display: none;
}
const angleRange = document.getElementById('angle-control');
const angleResult = document.getElementById('angle-result');
const root = document.querySelector('HTML');
const boxes = document.querySelectorAll('.box');
angleRange.addEventListener("input", function() {
const angle = angleRange.value;
const angleRad = angle * Math.PI / 180;
const magicNumber = Math.abs( Math.tan(angleRad) / 2 );
angleResult.innerHTML = angleRange.value + " deg";
document.documentElement.style.setProperty('--angle', angleRange.value + 'deg');
document.documentElement.style.setProperty('--magic-number', magicNumber);
// Sadly the animation only updates correctly in Firefox :-(
boxes.forEach((box) => {
box.style.animation = "none";
});
}, false);
Also see: Tab Triggers