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.
<p class="instructions">Pick the cities in the order you would wish to visit.</p>
<div id="app">
<div class="container">
<!-- create our empty "backing" that our choices will slide into, technically on top -->
<ul class="list-back">
<li v-for="city in cities" :key="city"></li>
</ul>
<!-- transition-group works the much the same as transition, but for "lists" -->
<!-- note the tag attribute states "ul", otherwise it defaults to span -->
<transition-group :name="currentListTransition" tag="ul" class="list">
<!-- whenever a choice is made, for loop updates the markup -->
<li v-for="(item, index) in selectedItems" :key="item">{{ item }}</li>
</transition-group>
</div>
<div class="container">
<ul class="list">
<!-- creates our list to choose from -->
<li class="city" v-for="(city, index) in cities" :key="city" :data-index="index" @click="chooseCity">{{ city }}</li>
</ul>
</div>
<div class="panel">
<!-- this appears to be the button to clear our selected list -->
<button class="clear" @click="clearSelection">clear</button>
</div>
</div>
body {
background-color: #707070;
font-family: sans-serif;
height: 100vh;
overflow: hidden;
width: 100vw;
}
#app {
align-items: center;
display: flex;
flex-wrap: wrap;
height: 100%;
justify-content: center;
width: 100%;
}
.instructions {
color: white;
font-size: 20px;
text-align: center;
}
.container {
height: 150px;
margin: 20px;
position: relative;
width: 200px;
}
.list,
.list-back {
align-items: center;
bottom: 0;
display: flex;
flex-direction: column;
justify-content: flex-start;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 0;
li {
align-items: center;
display: flex;
flex-shrink: 0;
font-size: 24px;
height: 34px;
justify-content: center;
margin-bottom: 4px;
width: 100%;
}
}
.list-back li {
background-color: #E0E0E0;
border-radius: 24px;
}
.city {
background-color: #E0E0E0;
border-radius: 24px;
cursor: pointer;
transition: 0.5s;
&:hover {
background-color: #307B21;
color: #F5F5F5;
}
&.selected {
opacity: 0.5;
pointer-events: none;
}
}
.panel {
display: flex;
justify-content: center;
width: 100%;
.clear {
background-color: #C11826;
border: {
radius: 8px;
style: none;
}
color: #F5F5F5;
cursor: pointer;
padding: 8px 16px;
}
}
// each transition is prefixed with the direction they are to appear from
.top-enter-active, .top-leave-active { transition: 0.5s; }
.top-enter, .top-leave-to { opacity: 0; transform: translate3d(0, -40px, 0); }
.top-move { opacity: 0.5; transition: 0.5s; }
.left-enter-active, .left-leave-active { transition: 0.5s; }
.left-enter, .left-leave-to { opacity: 0; transform: translate3d(-40px, 0, 0); }
.left-move { opacity: 0.5; transition: 0.5s; }
// this transition is also used from the "clear" button
.right-enter-active, .right-leave-active { transition: 0.5s; }
.right-enter, .right-leave-to { opacity: 0; transform: translate3d(40px, 0, 0); }
.right-move { opacity: 0.5; transition: 0.5s; }
.bottom-enter-active, .bottom-leave-active { transition: 0.5s; }
.bottom-enter, .bottom-leave-to { opacity: 0; transform: translate3d(0, 30px, 0); }
.bottom-move { opacity: 0.5; transition: 0.5s; }
// if animations are reduced at the OS level, turn off transitions
@media screen and (prefers-reduced-motion: reduce) {
.top-enter-active, .top-leave-active { transition: none; }
.top-move { transition: none; }
.left-enter-active, .left-leave-active { transition: none; }
.left-move { transition: none; }
.right-enter-active, .right-leave-active { transition: none; }
.right-move { transition: none; }
.bottom-enter-active, .bottom-leave-active { transition: none; }
.bottom-move { transition: none; }
}
new Vue({
el: "#app",
data: {
// array to keep track of selected cities
selectedItems: [],
// array of the current choices
cities: ['Chicago', 'New York', 'Seattle', 'Miami', 'Boston'],
// this string gets changed as choices are made
// but we know the first one will be from the top anyway...
currentListTransition: 'top'
},
methods: {
// method for the city list you choose from
// the index is passed because that easily relates to the arrays
chooseCity: function (event) {
let selectedLength = this.selectedItems.length;
let citiesLength = this.cities.length;
let clt = this.currentListTransition;
// somewhat complicated if/else sequence
// mainly because I wanted left and right entries for the middle items
if (selectedLength === 0) {
// we most likely don't require this since it is default
// but, you never know
clt = 'top';
} else if (selectedLength > 0 && selectedLength < citiesLength - 1) {
// check the previous transition direction
// if it was top or left then go right, if it was right then go left
// could reverse this, or even make it random for a large list
clt = clt === 'top' || clt === 'left' ? 'right' : 'left';
} else if (selectedLength === citiesLength - 1) {
// always make last one appear from bottom
clt = 'bottom';
}
// update the transition
this.currentListTransition = clt;
// push the choice into the array to trigger the transition
this.selectedItems.push(this.cities[event.target.dataset.index]);
// mark the choice as selected
event.target.classList.add('selected');
},
clearSelection: function () {
// make everything disappear to the right
// this can be changed to any of the other transitions
this.currentListTransition = 'right';
// clearing the array causes chosen cities to go transition out
this.selectedItems = [];
// clear selections so we can start again
document.querySelectorAll('.city.selected').forEach(element => {
element.classList.remove('selected');
});
}
}
})
Also see: Tab Triggers