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="carousel">
<div class="carousel__control">
</div>
<div class="carousel__stage">
<div class="spinner spinner--left">
<div class="spinner__face js-active" data-bg="#27323c">
<div class="content" data-type="iceland">
<div class="content__left">
<h1>ICELAND<br><span>EUROPE</span></h1>
</div>
<div class="content__right">
<div class="content__main">
<p>“As I flew north to begin my third circuit of Iceland in four years, I was slightly anxious. The number of visitors to Iceland has doubled in that period, and I feared this might mean a little less magic to go around. At the end of this trip, 6000km later, I'm thrilled to report that the magic levels remain high. It's found in glorious football victories and Viking chants, kayaking among icebergs, sitting with puffins under the midnight sun and crunching across brand-new lava fields.” </p>
<p>– Carolyn Bain</p>
</div>
<h3 class="content__index">01</h3>
</div>
</div>
</div>
<div class="spinner__face" data-bg="#19304a">
<div class="content" data-type="china">
<div class="content__left">
<h1>CHINA<br><span>ASIA</span></h1>
</div>
<div class="content__right">
<div class="content__main">
<p>“Its modern face is dazzling, but China is no one-trick pony. The world's oldest continuous civilisation isn't all smoked glass and brushed aluminium and while you won't be tripping over artefacts – three decades of round-the-clock development and rash town-planning have taken their toll – rich seams of antiquity await.”</p>
<p>– Damian Harper</p>
</div>
<h3 class="content__index">02</h3>
</div>
</div>
</div>
<div class="spinner__face" data-bg="#2b2533">
<div class="content" data-type="usa">
<div class="content__left">
<h1>USA<br><span>NORTH AMERICA</span></h1>
</div>
<div class="content__right">
<div class="content__main">
<p>“When it comes to travel, America has always floored me with its staggering range of possibilities. Not many other countries have so much natural beauty – mountains, beaches, rainforest, deserts, canyons, glaciers – coupled with fascinating cities to explore, an unrivaled music scene and all the things that make travel so rewarding (friendly locals, great restaurants and farmers markets, and plenty of quirky surprises).” </p>
<p>– Regis St Louis</p>
</div>
<h3 class="content__index">03</h3>
</div>
</div>
</div>
<div class="spinner__face" data-bg="#312f2d">
<div class="content" data-type="peru">
<div class="content__left">
<h1>PERU<br><span>SOUTH AMERICA</span></h1>
</div>
<div class="content__right">
<div class="content__main">
<p>“For me, Peru is the molten core of South America, a distillation of the oldest traditions and the finest building, weaving and art made by the most sophisticated cultures on the continent. In Peru the wildest landscapes – from frozen Andean peaks to the deep Amazon – help us re-forge our connection to the natural world. It is also a cultural stew, where diverse peoples live side by side, negotiating modern life with humor and aplomb. Beyond that, the cuisine alone makes it worth the trip. Every return is rich and surprising.”</p>
<p>– Carolyn McCarthy</p>
</div>
<h3 class="content__index">04</h3>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Poor man's preloader -->
<div style="height: 0; width: 0; overflow: hidden">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/peru.jpg">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/canada.jpg"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/china.jpg"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/usa.jpg"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/iceland.jpg"></div>
$page-margin-y = 20px
$page-margin-x = 20px
$width = "calc(50vw - (%s))" % $page-margin-x
$half-width = "calc(50vw - (%s))" % ($page-margin-x / 2)
$neg-half-width = "calc(-25vw + %s)" % ($page-margin-x / 2)
$height = "calc(100vh - %s)" % ($page-margin-y * 2)
$half-height = "calc(50vh - %s)" % $page-margin-y
$neg-half-height = "calc(-50vh + %s)" % $page-margin-y
$transition-dur = 1s
html
body
height: 100%
padding: 0
margin: 0
body
background: #fff
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif
// -- Carousel
.js-transitions-disabled *
transition: none !important
.carousel
position: relative
height: 100%
overflow: hidden
perspective: 50vw
perspective-origin: 50% 50%
.carousel__control
position: absolute
height: 160px
width: 40px
background: #fff
right: 0
top: 0
bottom: 0
margin: auto
z-index: 1
a
position: relative
display: block
width: 100%
padding-top: 75%
box-sizing: border-box
&:hover:before
background-color: rgba(0, 0, 0,0.4)
&.active:before
&.active:hover:before
background-color: rgba(0, 0, 0,0.6)
&:first-child
margin-top: 15px
&:before
position: absolute
top: 50%
left: 0
right: 0
margin: auto
border-radius: 50%
padding-top: 25%
width: 25%
background: rgba(0, 0, 0, 0.2)
content: ''
display: block
margin-top: -12.5%
.carousel__stage
position: absolute
top: $page-margin-y
bottom: $page-margin-y
left: $page-margin-x
right: $page-margin-x
margin: auto
transform-style: preserve-3d
transform: translateZ($neg-half-height)
.spinner
position: absolute
width: $width
height: $height
top: 0
left: 0
right: auto
bottom: 0
margin: auto
transform-style: preserve-3d
transition: transform $transition-dur
backface-visibility: hidden
transform-origin: 50% 50%
transform: rotateX(0)
.js-spin-fwd &
transform: rotateX(-90deg)
.js-spin-bwd &
transform: rotateX(90deg)
.spinner--right
.js-spin-fwd &
transform: rotateX(90deg)
.js-spin-bwd &
transform: rotateX(-90deg)
.spinner--right
right: 0
left: auto
.spinner__face
display: none
position: absolute
width: 100%
height: 100%
overflow: hidden
&.js-next
display: block
transform: rotateX(90deg) translateZ($half-height)
.spinner--right &
transform: rotateX(270deg) translateZ($half-height)
.js-spin-bwd &
&.js-next
transform: rotateX(-90deg) translateZ($half-height)
.js-spin-bwd
.spinner--right
.spinner__face.js-next
transform: rotateX(-270deg) translateZ($half-height)
.js-active
display: block
transform: translateZ($half-height)
// -- Content
.content
position: absolute
width: 200%
height: 100%
left: 0
.spinner--right &
left: -100%
.content__left
.content__right
position: absolute
left: 0
top: 0
width: 50%
height: 100%
.content__right
right: 0
left: auto
.content__left
background-repeat: no-repeat
background-size: cover
&:after
position: absolute
display: block
content: ""
width: 100%
height: 100%
background-color: rgba(0, 0, 0, 0.1)
h1
position: absolute
top: 50%
margin-top: -3vw
text-align: center
font-family: oswald
font-size: 5vw
height: 10vw
opacity: 1
color: #fff
width: 100%
letter-spacing: 0.15em
line-height: 0.6
span
font-size: 1vw
font-weight: 300
letter-spacing: 0.2em
opacity: 0.9
font-family: Merriweather
.content__right
display: flex
align-items: center
justify-content: center
.content__main
position: absolute
font-family: Merriweather, serif
text-align: left
color: #fff
font-size: 1.3vw
padding: 0 8vw
line-height: 1.65
font-weight: 300
margin: 0
opacity: 0.8
p:last-child
// font-style:italic
text-transform: uppercase
letter-spacing: 0.15em
font-size: 0.85em
.content__index
font-size: 30vh
position: absolute
right: -1vh
top: 35vh
opacity: 0.04
font-family: oswald
color: #fff
[data-type="iceland"]
.content__left
background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/iceland.jpg')
.spinner--right &
background-image: none
[data-type="china"]
.content__left
background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/china.jpg')
.spinner--right &
background-image: none
[data-type="usa"]
.content__left
background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/usa.jpg')
.spinner--right &
background-image: none
[data-type="peru"]
.content__left
background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/peru.jpg')
.spinner--right &
background-image: none
let activeIndex = 0
let limit = 0
let disabled = false
let $stage
let $controls
let canvas = false
const SPIN_FORWARD_CLASS = 'js-spin-fwd'
const SPIN_BACKWARD_CLASS = 'js-spin-bwd'
const DISABLE_TRANSITIONS_CLASS = 'js-transitions-disabled'
const SPIN_DUR = 1000
const appendControls = () => {
for (let i = 0; i < limit; i++) {
$('.carousel__control').append(`<a href="#" data-index="${i}"></a>`)
}
let height = $('.carousel__control').children().last().outerHeight()
$('.carousel__control').css('height', (30 + (limit * height)))
$controls = $('.carousel__control').children()
$controls.eq(activeIndex).addClass('active')
}
const setIndexes = () => {
$('.spinner').children().each((i, el) => {
$(el).attr('data-index', i)
limit++
})
}
const duplicateSpinner = () => {
const $el = $('.spinner').parent()
const html = $('.spinner').parent().html()
$el.append(html)
$('.spinner').last().addClass('spinner--right')
$('.spinner--right').removeClass('spinner--left')
}
const paintFaces = () => {
$('.spinner__face').each((i, el) => {
const $el = $(el)
let color = $(el).attr('data-bg')
$el.children().css('backgroundImage', `url(${getBase64PixelByColor(color)})`)
})
}
const getBase64PixelByColor = (hex) => {
if (!canvas) {
canvas = document.createElement('canvas')
canvas.height = 1
canvas.width = 1
}
if (canvas.getContext) {
const ctx = canvas.getContext('2d')
ctx.fillStyle = hex
ctx.fillRect (0, 0, 1, 1)
return canvas.toDataURL()
}
return false
}
const prepareDom = () => {
setIndexes()
paintFaces()
duplicateSpinner()
appendControls()
}
const spin = (inc = 1) => {
if (disabled) return
if (!inc) return
activeIndex += inc
disabled = true
if (activeIndex >= limit) {
activeIndex = 0
}
if (activeIndex < 0) {
activeIndex = (limit - 1)
}
const $activeEls = $('.spinner__face.js-active')
const $nextEls = $(`.spinner__face[data-index=${activeIndex}]`)
$nextEls.addClass('js-next')
if (inc > 0) {
$stage.addClass(SPIN_FORWARD_CLASS)
} else {
$stage.addClass(SPIN_BACKWARD_CLASS)
}
$controls.removeClass('active')
$controls.eq(activeIndex).addClass('active')
setTimeout(() => {
spinCallback(inc)
}, SPIN_DUR, inc)
}
const spinCallback = (inc) => {
$('.js-active').removeClass('js-active')
$('.js-next').removeClass('js-next').addClass('js-active')
$stage
.addClass(DISABLE_TRANSITIONS_CLASS)
.removeClass(SPIN_FORWARD_CLASS)
.removeClass(SPIN_BACKWARD_CLASS)
$('.js-active').each((i, el) => {
const $el = $(el)
$el.prependTo($el.parent())
})
setTimeout(() => {
$stage.removeClass(DISABLE_TRANSITIONS_CLASS)
disabled = false
}, 100)
}
const attachListeners = () => {
document.onkeyup = (e) => {
switch (e.keyCode) {
case 38:
spin(-1)
break
case 40:
spin(1)
break
}
}
$controls.on('click', (e) => {
e.preventDefault()
if (disabled) return
const $el = $(e.target)
const toIndex = parseInt($el.attr('data-index'), 10)
spin(toIndex - activeIndex)
})
}
const assignEls = () => {
$stage = $('.carousel__stage')
}
const init = () => {
assignEls()
prepareDom()
attachListeners()
}
$(() => {
init();
});
Also see: Tab Triggers