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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<link href="https://fonts.googleapis.com/css2?family=Quicksand:[email protected];400;500;600;700&display=swap" rel="stylesheet">
<div class="container">
<h1>Simple image popup effect with vanilla js and css animation</h1>
<h2>Click on the image</h2>
<!-- Container for images that will have popup effec -->
<div class="img-container">
<img alt=" " src="https://codeorum.com/images/article_images/900/20200726T133520_900.jpg">
<img alt=" " src="https://codeorum.com/images/article_images/900/20200726T133520_900.jpg">
</div>
</div>
body {
margin: 0;
padding: 0;
background: #3c72d6;
background: linear-gradient(to right, #f8d9b1, #ffd857);
font-family: "Quicksand", sans-serif;
color: #262626;
}
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
max-width: 1000px;
margin: 0 auto;
padding: 10px 10px 100px 10px;
h1 {
text-align: center;
margin-bottom: 30px;
font-weight: 500;
}
h2 {
font-weight: 500;
}
}
.img-container {
display: flex;
flex-direction: row;
justify-content: space-between;
img {
cursor: pointer;
border-radius: 6px;
width: 48%;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.15),
0 10px 10px -5px rgba(0, 0, 0, 0.1) !important;
}
}
// container for popup image
.popupContainer {
position: fixed;
left: 0;
bottom: 0;
right: 0;
top: 0;
overflow: auto;
// background of the popup umage
.popUpBackground {
transition: all 0.3s ease-in-out;
position: fixed;
left: 0;
bottom: 0;
right: 0;
top: 0;
&.active {
background-color: rgba(0, 0, 0, 0.4);
}
}
// popup image, you can adjust here animation tranistion time and effect
.popImage {
position: relative;
display: block;
transition: all 0.3s ease-in-out;
border-radius: 6px;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.15),
0 10px 10px -5px rgba(0, 0, 0, 0.1);
}
}
function initImagePopup(elem){
// check for mouse click, add event listener on document
document.addEventListener('click', function (e) {
// check if click target is img of the elem - elem is image container
if (!e.target.matches(elem +' img')) return;
else{
var image = e.target; // get current clicked image
// create new popup image with all attributes for clicked images and offsets of the clicked image
var popupImage = document.createElement("img");
popupImage.setAttribute('src', image.src);
popupImage.style.width = image.width+"px";
popupImage.style.height = image.height+"px";
popupImage.style.left = image.offsetLeft+"px";
popupImage.style.top = image.offsetTop+"px";
popupImage.classList.add('popImage');
// creating popup image container
var popupContainer = document.createElement("div");
popupContainer.classList.add('popupContainer');
// creating popup image background
var popUpBackground = document.createElement("div");
popUpBackground.classList.add('popUpBackground');
// append all created elements to the popupContainer then on the document.body
popupContainer.appendChild(popUpBackground);
popupContainer.appendChild(popupImage);
document.body.appendChild(popupContainer);
// call function popup image to create new dimensions for popup image and make the effect
popupImageFunction();
// resize function, so that popup image have responsive ability
var wait;
window.onresize = function(){
clearTimeout(wait);
wait = setTimeout(popupImageFunction, 100);
};
// close popup image clicking on it
popupImage.addEventListener('click', function (e) {
closePopUpImage();
});
// close popup image on clicking on the background
popUpBackground.addEventListener('click', function (e) {
closePopUpImage();
});
function popupImageFunction(){
// wait few miliseconds (10) and change style of the popup image and make it popup
// waiting is for animation to work, yulu can disable it and check what is happening when it's not there
setTimeout(function(){
// I created this part very simple, but you can do it much better by calculating height and width of the screen,
// image dimensions.. so that popup image can be placed much better
popUpBackground.classList.add('active');
popupImage.style.left = "15%";
popupImage.style.top = "50px";
popupImage.style.width = window.innerWidth * 0.7+"px";
popupImage.style.height = ((image.height / image.width) * (window.innerWidth * 0.7))+"px";
}, 10);
}
// function for closing popup image, first it will be return to the place where
// it started then it will be removed totaly (deleted) after animation is over, in our case 300ms
function closePopUpImage(){
popupImage.style.width = image.width+"px";
popupImage.style.height = image.height+"px";
popupImage.style.left = image.offsetLeft+"px";
popupImage.style.top = image.offsetTop+"px";
popUpBackground.classList.remove('active');
setTimeout(function(){
popupContainer.remove();
}, 300);
}
}
});
}
// Start popup image function
initImagePopup(".img-container") // elem = image container
Also see: Tab Triggers