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 id="app"></div>
<!-- The only anti-pattern I have cosciously left in the code is the passage of data from the slide component up to the top level of the view, but I reeeeeeally wanted to be able to change all those borders throughout the carousel at the same time so at least I did it in the cleanest way I know of. Please let me know if there are any other major flaws with this, as I can really really really use the feedback, cheers!-->
@import url("https://fonts.googleapis.com/css2?family=Playfair+Display:ital@0;1&display=swap");
@keyframes fade-in {
from {
opacity: 0;
transform: scale(1.3);
}
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes next-slide-in {
from {
transform: translateX(100%);
}
to {
transform: translateX(0);
}
}
@keyframes next-slide-out {
from {
transform: translateX(0);
}
to {
transform: translateX(-100%);
}
}
@keyframes back-slide-in {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
@keyframes back-slide-out {
from {
transform: translateX(0);
}
to {
transform: translateX(100%);
}
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
&::before,
&::after {
content: "";
}
}
html {
font-size: 10px;
}
#app {
background: linear-gradient(to top left, #e66465, purple);
//background: linear-gradient(to top left,#ff8a00,#e52e71);
font-family: "Playfair Display", serif;
}
.wrapper {
position: relative;
display: flex;
justify-content: center;
flex-wrap: wrap;
padding: 50px 50px 70px 50px;
flex-direction: column;
max-width: 650px;
margin: 0 auto;
@media only screen and (max-width: 650px) {
padding: 30px 30px 50px 30px;
}
}
.intro {
text-align: center;
font-size: 1.6rem;
margin-bottom: 50px;
//text-shadow: 0px 0px 10px purple;
h1 {
font-size: 5rem;
font-weight: lighter;
}
h1,
h2 {
font-style: italic;
}
h2 {
margin-bottom: 20px;
}
span {
text-decoration: underline;
color: #ff7a2a;
}
@media only screen and (max-width: 550px) {
h1 {
font-size: 4rem;
}
h2 {
font-size: 1.6rem;
}
}
@media only screen and (max-width: 450px) {
p {
font-size: 1.2rem;
}
}
@media only screen and (max-width: 415px) {
h1 {
font-size: 3rem;
}
}
}
.carousel {
display: flex;
position: relative;
overflow: hidden;
justify-content: center;
align-items: center;
height: 350px;
margin-bottom: 50px;
box-shadow: 0px 15px 30px 0px #151515;
border-style: solid;
border-width: 3px;
transition-duration: 0.5s;
transition-property: border-color;
.controls {
position: absolute;
z-index: 12121996; // <---- fun fact: this is my birthday
height: 30px;
width: 30px;
background-color: purple;
border-radius: 50%;
cursor: pointer;
color: white;
font-size: 1.4rem;
display: flex;
justify-content: center;
align-items: center;
user-select: none;
transition-duration: 0.1s;
transition-timing-function: ease-in;
&--back {
left: 25px;
span {
position: relative;
border-left: 5px solid pink;
border-top: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid pink;
border-bottom-left-radius: 1px;
transform: translate3d(-3px, -1px, 0) rotateZ(30deg);
&::before {
position: absolute;
border-top: 4px solid pink;
width: 12px;
border-top-right-radius: 10px;
height: 15px;
transform: rotateZ(-45deg) translate3d(8px, -3px, 0);
}
}
}
&--next {
right: 25px;
span {
position: relative;
border-left: 5px solid transparent;
border-top: 5px solid transparent;
border-right: 5px solid pink;
border-bottom: 5px solid pink;
border-bottom-right-radius: 1px;
transform: translate3d(4px, -1px, 0) rotateZ(-34deg);
&::before {
position: absolute;
border-top: 4px solid pink;
width: 12px;
border-top-left-radius: 10px;
height: 15px;
transform: rotateZ(45deg) translate3d(-17px, 5px, 0);
}
}
}
&:hover {
height: 33px;
width: 33px;
}
}
& .slide {
position: absolute;
background-color: pink;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
padding: 50px;
z-index: 0;
&__copy-message {
position: absolute;
top: 10px;
font-size: 1.2rem;
transition-duration: 0.5s;
&.nope {
top: 0;
transform: translate3d(0, -100%, 0);
}
}
&__shape {
animation-name: fade-in;
animation-duration: 0.5s;
animation-fill-mode: forwards;
cursor: crosshair;
position: relative;
&--heart {
position: relative;
width: 100px;
height: 90px;
&::before,
&::after {
position: absolute;
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
&::after {
left: 0;
transform: rotate(45deg);
transform-origin: 100% 100%;
}
}
&--pacman {
width: 0px;
height: 0px;
border-right: 60px solid transparent;
border-top: 60px solid orange;
border-left: 60px solid orange;
border-bottom: 60px solid orange;
border-top-left-radius: 60px;
border-top-right-radius: 60px;
border-bottom-left-radius: 60px;
border-bottom-right-radius: 60px;
}
&--yin-yang {
width: 96px;
box-sizing: content-box;
height: 48px;
background: #eee;
border-color: black;
border-style: solid;
border-width: 2px 2px 50px 2px;
border-radius: 100%;
position: relative;
&::before {
position: absolute;
top: 50%;
left: 0;
background: #eee;
border: 18px solid black;
border-radius: 100%;
width: 12px;
height: 12px;
box-sizing: content-box;
}
&::after {
position: absolute;
top: 50%;
left: 50%;
background: black;
border: 18px solid #eee;
border-radius: 100%;
width: 12px;
height: 12px;
box-sizing: content-box;
}
}
&--cut-diamond {
border-style: solid;
border-color: transparent transparent DodgerBlue transparent;
border-width: 0 25px 25px 25px;
height: 0;
width: 50px;
box-sizing: content-box;
position: relative;
margin-bottom: 50px;
&:after {
position: absolute;
top: 25px;
left: -25px;
width: 0;
height: 0;
border-style: solid;
border-color: DodgerBlue transparent transparent transparent;
border-width: 70px 50px 0 50px;
}
}
&--infinity {
position: relative;
width: 150px;
height: 80px;
box-sizing: content-box;
&::before,
&::after {
content: "";
box-sizing: content-box;
position: absolute;
top: 0;
left: 0;
width: 40px;
height: 40px;
border: 15px solid green;
border-radius: 40px 40px 0 40px;
transform: rotate(-45deg);
}
&::after {
left: auto;
right: 0;
border-radius: 40px 40px 40px 0;
transform: rotate(45deg);
}
}
}
}
&--heart {
border-color: red;
}
&--pacman {
border-color: orange;
}
&--yin-yang {
border-color: black;
}
&--cut-diamond {
border-color: DodgerBlue;
}
&--infinity {
border-color: green;
}
@media only screen and (max-width: 450px) {
height: 300px;
}
}
.code-wrapper {
border-width: 3px;
border-style: solid;
background-color: pink;
box-shadow: 0px 15px 30px 0px #151515;
position: relative;
overflow: hidden;
width: 100%;
display: flex;
transition-property: border-color;
transition-duration: 0.5s;
.code {
font-family: monospace;
font-size: 1.2rem;
white-space: pre-wrap;
animation-duration: 1s;
animation-fill-mode: forwards;
padding: 60px;
width: 100%;
&--last {
position: absolute;
&.slide-left {
animation-name: back-slide-out;
}
&.slide-right {
animation-name: next-slide-out;
}
}
&--active {
position: relative;
&.slide-left {
animation-name: back-slide-in;
}
&.slide-right {
animation-name: next-slide-in;
}
}
@media only screen and (max-width: 550px) {
padding: 45px;
}
@media only screen and (max-width: 450px) {
padding: 30px;
}
}
&--heart {
border-color: red;
}
&--pacman {
border-color: orange;
}
&--yin-yang {
border-color: black;
}
&--cut-diamond {
border-color: DodgerBlue;
}
&--infinity {
border-color: green;
}
}
!(function () {
const useState = React.useState;
const SHAPES = [
[
"heart",
`#heart {
position: relative;
width: 100px;
height: 90px;
}
#heart::before, #heart::after {
content: '';
position: absolute;
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
#heart::after {
left: 0;
transform: rotate(45deg);
transform-origin: 100% 100%;
}`
],
[
"pacman",
`#pacman {
width: 0px;
height: 0px;
border-right: 60px solid transparent;
border-top: 60px solid orange;
border-left: 60px solid orange;
border-bottom: 60px solid orange;
border-top-left-radius: 60px;
border-top-right-radius: 60px;
border-bottom-left-radius: 60px;
border-bottom-right-radius: 60px;
}`
],
[
"yin-yang",
`#yin-yang {
width: 96px;
box-sizing: content-box;
height: 48px;
background: white;
border-color: black;
border-style: solid;
border-width: 2px 2px 50px 2px;
border-radius: 100%;
position: relative;
}
#yin-yang::before {
content: '';
position: absolute;
top: 50%;
left: 0;
background: white;
border: 18px solid black;
border-radius: 100%;
width: 12px;
height: 12px;
box-sizing: content-box;
}
#yin-yang::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
background: black;
border: 18px solid white;
border-radius: 100%;
width: 12px;
height: 12px;
box-sizing: content-box;
}`
],
[
"cut-diamond",
`#cut-diamond {
border-style: solid;
border-color: transparent transparent DodgerBlue transparent;
border-width: 0 25px 25px 25px;
height: 0;
width: 50px;
box-sizing: content-box;
position: relative;
margin-bottom: 50px;
}
#cut-diamond::after {
content: "";
position: absolute;
top: 25px;
left: -25px;
width: 0;
height: 0;
border-style: solid;
border-color: DodgerBlue transparent transparent transparent;
border-width: 70px 50px 0 50px;
}`
],
[
"infinity",
`#infinity {
position: relative;
width: 150px;
height: 80px;
box-sizing: content-box;
}
#infinity::before, #infinity::after {
content: "";
box-sizing: content-box;
position: absolute;
top: 0;
left: 0;
width: 40px;
height: 40px;
border: 15px solid green;
border-radius: 40px 40px 0 40px;
transform: rotate(-45deg);
}
#infinity::after {
left: auto;
right: 0;
border-radius: 40px 40px 40px 0;
transform: rotate(45deg);
}`
]
];
function App(props) {
const [app, updateApp] = useState({
currentIndex: 0,
lastIndex: -1,
direction: 0
});
return (
<div className="wrapper">
<div className="intro">
<h1>“The Shapes of CSS”</h1>
<h2>from CSS-Tricks</h2>
<p>
an incredibly clever article written by <span>Chris Coyier</span>,
</p>
<p>shamelessly copied (only a little part) by me</p>
</div>
<Carousel
direction={app.direction}
currentIndex={app.currentIndex}
lastIndex={app.currentIndex}
updateApp={updateApp}
shapes={props.shapes}
/>
<Code
direction={app.direction}
lastShape={props.shapes[app.lastIndex]}
currentShape={props.shapes[app.currentIndex]}
shapes={props.shapes}
/>
</div>
);
}
function Carousel(props) {
function showNext(index) {
let lastIndex =
index < 0
? 0
: index === props.shapes.length
? props.shapes.length - 1
: index;
let currentIndex = index + 1 === props.shapes.length ? 0 : index + 1;
props.updateApp({ currentIndex, lastIndex, direction: 1 });
}
function showPrevious(index) {
let lastIndex = index < 0 ? 0 : index;
let currentIndex = index - 1 < 0 ? props.shapes.length - 1 : index - 1;
props.updateApp({ currentIndex, lastIndex, direction: -1 });
}
return (
<div
className={"carousel carousel--" + props.shapes[props.currentIndex][0]}
>
<div
onClick={() => showPrevious(props.currentIndex)}
className="controls controls--back"
>
<span></span>
</div>
<Slide shape={props.shapes[props.currentIndex]} />
<div
onClick={() => showNext(props.currentIndex)}
className="controls controls--next"
>
<span></span>
</div>
</div>
);
}
function Slide(props) {
const [copyMessage, setCopyMessage] = useState(false);
function copyCSS(shapeCSS) {
navigator.clipboard.writeText(shapeCSS).then(
() => {
console.log("Successfully copied CSS to clipboard");
setCopyMessage(true);
setTimeout(setCopyMessage, 2000, false);
},
() => {
console.log("Error: couldn't copy to clipboard :(");
}
);
}
return (
<div key={props.shape[0]} className={"slide slide--" + props.shape[0]}>
<h6
className={"slide__copy-message " + (copyMessage ? "show" : "nope")}
>
I am in your Clipboard!
</h6>
<div
onClick={() => copyCSS(props.shape[1])}
className={"slide__shape slide__shape--" + props.shape[0]}
></div>
</div>
);
}
function Code(props) {
return (
<div className={"code-wrapper code-wrapper--" + props.currentShape[0]}>
<pre
key={props.currentShape[0]}
className={
"code code--active" +
(props.direction < 0 ? " slide-left" : " slide-right")
}
>
{props.currentShape[1]}
</pre>
{props.lastShape && (
<pre
key={props.lastShape[0]}
className={
"code code--last" +
(props.direction < 0 ? " slide-left" : " slide-right")
}
>
{props.lastShape[1]}
</pre>
)}
</div>
);
}
ReactDOM.render(<App shapes={SHAPES} />, document.getElementById("app"));
})();
Also see: Tab Triggers