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.
<body id="app">
<div class="show-picture">
<div class="flex">
<div class="image popup"></div>
</div>
</div>
<div class="wrapper">
<h1>Gallery</h1>
<form v-on:submit='search'>
<input type="text" id="search-phrase" placeholder="Input text" v-model='searchPhrase'>
<button type="submit" id="search">Search</button>
<div class="clearfix"></div>
</form>
<div class="wrapper__titles" v-if="searchResults">
<h2 class="res-title">Results</h2>
<h2 class="fav-title">Favorites</h2>
<div class="clearfix"></div>
</div>
<div class="results">
<!-- Pictures got from vk -->
<div class="results__all">
<!-- Template for pictures got from vk -->
<result-item v-if="searchResults" v-for='searchResult in searchResults' v-bind:search-result='searchResult'></result-item>
</div>
<!-- Favorite pictures -->
<div class="results__favorite">
<!-- Template for pictures maved to favorites -->
<favorite-item v-for="favorite in favorites" v-bind:favorite='favorite'></favorite-item>
</div>
<div class="clearfix"></div>
</div>
</div>
<!-- template for result-item -->
<template id="result-template">
<div class="results__all__item">
<a href v-on:click.stop.prevent='show(searchResult)'><img v-bind:src='searchResult.src_big'></a>
<a href class='add' v-on:click.stop.prevent='add(searchResult)' v-if='!searchResult.added'>Add to favorites</a>
<a href class='remove' v-on:click.stop.prevent='remove(searchResult)' v-if='searchResult.added'>Added</a>
</div>
</template>
<!-- template for favorite-item -->
<template id="favorite-template">
<div class="results__fav__item">
<a href v-on:click.stop.prevent='show(favorite)'><img v-bind:src='favorite.src_big'></a>
<a href class='remove' v-on:click.stop.prevent='remove(favorite)'>Remove from favorites</a>
</div>
</template>
</body>
* {
margin: 0;
padding: 0;
font-family: Verdana, sans-serif;
text-decoration: none;
color: #212121;
}
.wrapper {
width: 100%;
max-width: 1280px;
margin: 0 auto;
padding: 1%;
box-sizing: border-box;
}
/* Head with input field */
.wrapper h1 {
margin: 1% 0;
font-size: 3rem;
}
.wrapper input {
display: inline-block;
float: left;
width: 74%;
height: 50px;
margin: 0 0 2% 0;
padding: 10px;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #b0b0b0;
outline: none;
font-size: 1.2rem;
transition: all 300ms ease-out;
}
.wrapper input:hover,
.wrapper input:focus {
border: 1px solid #FF5722;
}
form > button {
display: inline-block;
float: right;
width: 24%;
height: 50px;
margin: 0 0 2% 0;
border: 1px solid #FF5722;
border-radius: 5px;
box-sizing: border-box;
font-size: 1.2rem;
text-align: center;
line-height: 50px;
color: #FF5722;
background-color: #FFFFFF;
font-weight: bold;
transition: all 300ms ease-out;
}
form > button:hover {
background-color: #FF5722;
color: #FFFFFF;
}
.clearfix {
content: '';
clear: both;
line-height: 0px;
}
/* Results block */
.results {
width: 100%;
display: flex;
align-items: flex-start;
justify-content: space-between;
}
.results h2 {
display: block;
width: 100%;
}
.res-title {
float: left;
width: 74%;
margin-bottom: 0.5rem;
}
.results__all {
display: flex;
align-items: flex-start;
justify-content: space-between;
flex-wrap: wrap;
width: 74%;
box-sizing: border-box;
}
.results__all__item {
display: inline-block;
width: 30%;
margin: 0 0 0.8rem 0;
}
.results__all__item:nth-of-type(3n) {}
.results__all__item img {
width: 100%;
height: auto;
}
.results__all__item a {
display: block;
width: 100%;
box-sizing: border-box;
font-size: 0.7rem;
text-align: center;
line-height: 20px;
transition: all 300ms ease-out;
}
.results__all__item a.add {
border: 1px solid #FF5722;
color: #FF5722;
}
.results__all__item a.remove {
border: 1px solid #4CAF50;
color: #4CAF50;
}
.fav-title {
float: right;
width: 24%;
margin-bottom: 0.5rem;
}
.results__favorite {
display: flex;
align-items: flex-start;
justify-content: space-around;
flex-wrap: wrap;
width: 24%;
}
.results__fav__item {
display: inline-block;
width: 100%;
margin: 0 0 0.8rem 0;
}
.results__fav__item img {
width: 100%;
height: auto;
}
.results__fav__item a.remove {
display: block;
width: 100%;
border: 1px solid #FF5722;
box-sizing: border-box;
font-size: 0.7rem;
text-align: center;
line-height: 20px;
color: #FF5722;
transition: all 300ms ease-out;
}
.results__all__item a.add:hover,
.results__fav__item a.remove:hover {
background-color: #FF5722;
color: #FFFFFF;
}
.results__all__item a.remove:hover {
background-color: #4CAF50;
color: #FFFFFF;
}
/* Showing picture */
.show-picture {
position: fixed;
display: none;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.5);
}
.flex {
display: flex;
width: 100%;
height: 100%;
flex-direction: row;
justify-content: center;
align-items: center;
}
.image {
width: 90%;
height: 90%;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
@media screen and (max-width: 600px) {
.wrapper h1 {
font-size: 2rem;
}
.res-title,
.fav-title {
font-size: 1rem;
}
}
@media screen and (max-width: 400px) {
.wrapper h1 {
font-size: 1.5rem;
}
.res-title,
.fav-title {
font-size: 0.8rem;
}
}
@media screen and (max-width: 370px) {
form > button {
font-size: 1rem;
}
}
'use strict';
$(document).ready(function () {
Vue.component('result-item', {
props: [
'searchResult',
],
template: '#result-template',
methods: {
show: function (obj) {
var src;
if (obj.src_xxxbig !== undefined) src = obj.src_xxxbig;
else if (obj.src_xxbig !== undefined) src = obj.src_xxbig;
else if (obj.src_xbig !== undefined) src = obj.src_xbig;
else src = obj.src_big;
/* Setting background image and showing our popup */
$('.image').css('background-image', 'url(' + src + ')');
$('.show-picture').css('display', 'block');
},
add: function (searchResult) {
var index = vueView.searchResults.indexOf(searchResult);
/* Checking if stuff is already in favorites or not */
if (vueView.favorites.indexOf(searchResult) === -1) {
/* Pushing stuff to favorites if not */
vueView.favorites.push(searchResult);
/* Changing field value to hide button "Add to favorites" and show "Added" */
vueView.searchResults[index].added = true;
};
},
remove: function (searchResult) {
var index = vueView.searchResults.indexOf(searchResult);
if (vueView.favorites.indexOf(searchResult) !== -1) {
/* Removing stuff from favorites */
vueView.favorites.splice((vueView.favorites.indexOf(searchResult)), 1);
/* Changing field value to hide button "Added" and show "Add to favorites" */
vueView.searchResults[index].added = false;
};
},
},
});
Vue.component('favorite-item', {
props: [
'favorite',
],
template: '#favorite-template',
methods: {
show: function (obj) {
var src;
if (obj.src_xxxbig !== undefined) src = obj.src_xxxbig;
else if (obj.src_xxbig !== undefined) src = obj.src_xxbig;
else if (obj.src_xbig !== undefined) src = obj.src_xbig;
else src = obj.src_big;
/* Setting background image and showing our popup */
$('.image').css('background-image', 'url(' + src + ')');
$('.show-picture').css('display', 'block');
},
remove: function (favorite) {
var index = vueView.searchResults.indexOf(favorite);
if (vueView.favorites.indexOf(favorite) !== -1) {
/* Removing stuff from favorites */
vueView.favorites.splice((vueView.favorites.indexOf(favorite)), 1);
/* Changing field value to hide button "Added" and show "Add to favorites" */
vueView.searchResults[index].added = false;
};
},
},
});
var vueView = new Vue({
el: '#app',
data: {
searchPhrase: '',
searchResults: null,
favorites: [],
},
methods: {
search: function (e) {
e.preventDefault();
var _this = this;
var searchUrl = 'https://api.vk.com/method/photos.search?q=' + this.searchPhrase + '&count=100&callback=JSON_CALLBACK';
$.ajax({
url: searchUrl,
dataType: 'jsonp',
success: function (content) {
var searchResults = content.response;
var arr = [];
/* Iterate through results and pushing objects to result array */
for (var i = 1; i < searchResults.length; i++) {
arr.push(searchResults[i]);
/* Adding flag for hiding button "Added" */
arr[i - 1].added = false;
};
console.log('Done, bleat!');
_this.$set('searchResults', arr);
_this.$set('favorites', []);
},
error: function () {
console.log('Error, sukaaa!');
},
});
},
},
});
/* Closing our popup by clicking somewhere */
$('.flex').click(function () {
$('.show-picture').css('display', 'none');
});
/* Closing our popup by pressing ESC button */
$(window).keydown(function (event) {
if (event.keyCode == 27 && $('.show-picture').css('display') === 'block') {
$('.show-picture').css('display', 'none');
};
});
});
Also see: Tab Triggers