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.
<main uk-height-viewport="expand: true">
<div class="uk-container uk-margin-top uk-margin-bottom">
<p class="uk-text-lead">Multiple file upload with image preview</p>
<div class="uk-upload-box">
<div id="error-alert" class="uk-alert-danger uk-margin-top uk-hidden" uk-alert>
<p id="error-messages"></p>
</div>
<div class="drop__zone uk-placeholder uk-text-center">
<span uk-icon="icon: cloud-upload"></span>
<span class="uk-text-middle uk-margin-small-left">Attach files by dropping them here or</span>
<div uk-form-custom>
<input name="documents[]" type="file" accept="image/png, image/jpeg, application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document" multiple>
<span class="uk-link">selecting one</span>
</div>
<ul id="preview" class="uk-list uk-grid-match uk-child-width-1-2 uk-child-width-1-4@l uk-child-width-1-5@xl uk-text-center" uk-grid uk-scrollspy="cls: uk-animation-scale-up; target: .list-item; delay: 80"></ul>
</div>
</div>
<hr>
<p class="uk-text-lead">Single file upload image preview</p>
<div class="uk-upload-box">
<div id="error-alert" class="uk-alert-danger uk-margin-top uk-hidden" uk-alert>
<p id="error-messages"></p>
</div>
<div class="drop__zone uk-placeholder uk-text-center">
<span uk-icon="icon: cloud-upload"></span>
<span class="uk-text-middle uk-margin-small-left">Attach files by dropping them here or</span>
<div uk-form-custom>
<input name="document" accept="image/png, image/jpeg, application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document" type="file">
<span class="uk-link">selecting one</span>
</div>
<ul id="preview" class="uk-list uk-grid-match uk-child-width-1-2 uk-child-width-1-4@l uk-child-width-1-5@xl uk-text-center" uk-grid uk-scrollspy="cls: uk-animation-scale-up; target: .list-item; delay: 80"></ul>
</div>
</div>
<!-- Random Card -->
<div class="uk-card uk-card-hover uk-box-shadow-medium" hidden>
<div class="uk-card-header uk-background-primary uk-light">
<h3 class="uk-heading uk-heading-bullet uk-card-title">I am a card title</h3>
</div>
<div class="uk-card-body">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores asperiores totam recusandae aliquam nulla velit suscipit necessitatibus facere. Alias exercitationem sit magni. Inventore accusantium minima illum cupiditate saepe non laborum, voluptatibus accusamus ducimus fugiat tempore voluptatem a tempora, omnis voluptatum.</p>
</div>
<div class="uk-card-footer uk-background-muted">
<span class="uk-text-meta uk-text-muted">I am a card footer</span>
</div>
</div>
</div>
</main>
<!-- FOOTER || ALWAYS STICKS TO BOTTOM ;-) -->
<footer>
<div class="uk-section uk-section-xsmall uk-margin-small-top uk-section-muted">
<div class="uk-container uk-container-expand">
<div class="uk-grid uk-child-width-1-2@s uk-text-center uk-text-left@s" data-uk-grid>
<div class="uk-text-small uk-text-muted">
© 2022 Your Company | All rights reserved.
</div>
<div class="uk-text-small uk-text-muted uk-text-center uk-text-right@s">
<span>Designed & Developed with <i class="mdi mdi-heart" style="color: palevioletred"></i> By </span> | <a href="https://codepen.io/kent_iyo/pen/GRJJjKZ" class="uk-text-muted" title="A pen by Kent" target="_blank" data-uk-tooltip>Kent</a>
</div>
</div>
</div>
</div>
</footer>
.file-upload-name {
background: #585858;
padding: 0.5rem;
color: white;
margin-top: 0.2rem;
}
.alert {
border-left: 1px solid;
}
#img-preview-responsive {
min-height: 200px;
}
const UPLOAD = {
BASE_PATH: window.location.origin,
ACCEPTED_DOC_MIMES: [
"image/png",
"image/jpeg",
"application/pdf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/msword"
],
DOC_MIMES: [
"application/pdf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/msword"
],
FileInputs: [...document.querySelectorAll("input[type='file']")],
Init: () => {
UPLOAD.AddEventListeners();
},
AddEventListeners: () => {
UPLOAD.FileInputs.map((fileInput) => {
fileInput.addEventListener("change", UPLOAD.HandleFileChange);
});
UPLOAD.FileInputs.map((fileInput) => {
fileInput.addEventListener("blur", (e) => {
// console.log(e.target);
});
});
UPLOAD.FileInputs.map((fileInput) => {
const box = fileInput.closest(".uk-placeholder");
box.addEventListener(
"dragover",
(e) => {
e.preventDefault();
e.stopPropagation();
box.classList.add("uk-box-shadow-medium");
},
false
);
});
UPLOAD.FileInputs.map((fileInput) => {
const box = fileInput.closest(".uk-placeholder");
box.addEventListener(
"dragleave",
(e) => {
e.preventDefault();
e.stopPropagation();
box.classList.remove("uk-box-shadow-medium");
},
false
);
});
UPLOAD.FileInputs.map((fileInput) => {
const box = fileInput.closest(".uk-placeholder");
box.addEventListener(
"drop",
(e) => {
e.preventDefault();
e.stopPropagation();
box.classList.remove("uk-box-shadow-medium");
const dropZone = e.target.closest(".drop__zone");
const componentContainer = dropZone.closest(".uk-upload-box");
let preview, alertBox, alertMsg;
preview = dropZone.querySelector(`#preview`);
alertBox = componentContainer.querySelector(`#error-alert`);
alertMsg = componentContainer.querySelector(`#error-messages`);
let files;
let isMultiple = false;
if (fileInput.hasAttribute("multiple")) {
isMultiple = true;
}
if (!e.dataTransfer.files.length) return;
/** If items dropped are more than one file and the input doesn't accept multiple, do not accept the files. Clear preview field, show notification and add shake animatin to dropzone */
if (isMultiple === false && e.dataTransfer.files.length > 1) {
UPLOAD.RemoveChild(preview);
UPLOAD.AddShakeAnimation(dropZone);
UIkit.notification({
message: "Cannot accept multiple files when expecting a single file!",
status: "danger",
pos: "center",
timeout: 4000
});
return;
}
files = e.dataTransfer.files;
fileInput.files = files;
const options = {
isMultiple,
files,
preview,
alertBox,
alertMsg,
fileInput,
dropZone
};
if (isMultiple) {
UPLOAD.PreviewMultipleDocs(options);
} else {
UPLOAD.PreviewSingleDoc(options);
}
},
false
);
});
},
Reset: (e) => {
if (e.target.files.length == 0) {
const alertWrapper = e.target.closest("#error-alert");
let alertMessage = alertWrapper.querySelector("#error-messages");
alertWrapper.classList.add("uk-hidden");
alertMessage.textContent = "";
input.value = "";
return;
}
},
AddShakeAnimation: (parent) => {
let timeout;
clearTimeout(timeout);
parent.classList.add("uk-animation", "uk-animation-shake");
timeout = setTimeout(() => {
parent.classList.remove("uk-animation", "uk-animation-shake");
}, 1000);
},
HandleFileChange: (e) => {
let files;
let isMultiple = false;
if (e.target.hasAttribute("multiple")) {
isMultiple = true;
}
const dropZone = e.target.closest(".drop__zone");
const componentContainer = dropZone.closest(".uk-upload-box");
const documentCategory = dropZone.dataset.documentCategory;
let preview, alertBox, alertMsg;
preview = dropZone.querySelector(`#preview`);
alertBox = componentContainer.querySelector(`#error-alert`);
alertMsg = componentContainer.querySelector(`#error-messages`);
/** Remove existing files and preview files if files lenght is 0 */
if (!e.target.files.length) {
UPLOAD.RemoveChild(preview);
return;
}
files = e.target.files;
const options = {
files,
fileInput: e.target,
isMultiple,
preview,
alertBox,
alertMsg,
dropZone
};
if (isMultiple) {
UPLOAD.PreviewMultipleDocs(options);
} else {
UPLOAD.PreviewSingleDoc(options);
}
},
RemoveChild: (preview) => {
while (preview.firstChild) {
preview.removeChild(preview.firstChild);
}
},
ImgPreviewLi: (readerResult, filename) => {
const li = document.createElement("li");
const div = document.createElement("div");
const img = document.createElement("img");
const span = document.createElement("span");
li.className = "list-item uk-margin-medium-top";
div.className = "uk-cover-container";
img.setAttribute("id", "img-preview-responsive");
img.setAttribute("src", readerResult);
img.setAttribute("data-name", filename);
img.setAttribute("alt", "file-image-preview");
span.className = "uk-text-meta uk-text-break file-upload-name";
span.textContent = filename;
div.append(img);
li.append(div, span);
return li;
},
ValidateFileType: ({
fileType,
fileInput,
alertBox,
alertMsg,
preview,
dropZone
}) => {
if (!UPLOAD.ACCEPTED_DOC_MIMES.includes(fileType)) {
alertMsg.textContent =
"Sorry, one or more of your file type is not allowed.";
alertMsg.classList.add("uk-text-danger");
alertBox.classList.remove("uk-hidden");
fileInput.value = "";
UPLOAD.AddShakeAnimation(dropZone);
UPLOAD.RemoveChild(preview);
return false;
}
return true;
},
ValidateFileSize: ({
fileInput,
size,
alertBox,
alertMsg,
preview,
dropZone
}) => {
if (size > 2000000) {
alertMsg.textContent =
"Sorry, one or more of your files has exceeded the file size limit of 2MB.";
alertMsg.classList.add("uk-text-danger");
alertBox.classList.remove("uk-hidden");
fileInput.value = "";
UPLOAD.AddShakeAnimation(dropZone);
UPLOAD.RemoveChild(preview);
return false;
}
return true;
},
PreviewMultipleDocs: ({
files,
fileInput,
preview,
alertBox,
alertMsg,
dropZone
}) => {
const docFiles = [...files];
docFiles.forEach((file) => {
const size = file["size"];
const fileType = file["type"];
if (docFiles.length !== 0) {
UPLOAD.RemoveChild(preview);
}
const fileOptions = {
fileInput,
preview,
alertBox,
alertMsg,
fileType,
size,
dropZone
};
if (!UPLOAD.ValidateFileSize(fileOptions)) return;
if (!UPLOAD.ValidateFileType(fileOptions)) return;
alertMsg.textContent = "";
alertBox.classList.add("uk-hidden");
fileInput.files = files;
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
let filename = file["name"];
let imgPreview = "";
if (UPLOAD.DOC_MIMES.includes(fileType)) {
if (fileType === "application/pdf") {
imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/pdf.svg`;
imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
} else {
imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/docx.svg`;
imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
}
} else {
imgPreview = UPLOAD.ImgPreviewLi(reader.result, filename);
}
preview.append(imgPreview);
};
});
},
PreviewSingleDoc: ({
files,
fileInput,
preview,
alertBox,
alertMsg,
dropZone
}) => {
const size = files[0]["size"];
const fileType = files[0]["type"];
let filename = files[0]["name"];
if (files[0].length !== 0) {
UPLOAD.RemoveChild(preview);
}
const fileOptions = {
fileInput,
preview,
alertBox,
alertMsg,
fileType,
size,
dropZone
};
if (!UPLOAD.ValidateFileSize(fileOptions)) return;
if (!UPLOAD.ValidateFileType(fileOptions)) return;
alertMsg.textContent = "";
alertBox.classList.add("uk-hidden");
const reader = new FileReader();
reader.readAsDataURL(files[0]);
reader.onload = () => {
let imgPreview = "";
let imagePath = "";
if (UPLOAD.DOC_MIMES.includes(fileType)) {
if (fileType === "application/pdf") {
imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/pdf.svg`;
imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
} else {
imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/docx.svg`;
imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
}
} else {
imgPreview = UPLOAD.ImgPreviewLi(reader.result, filename);
}
preview.append(imgPreview);
};
}
};
document.addEventListener("DOMContentLoaded", UPLOAD.Init());
Also see: Tab Triggers