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.
<html>
<head>
<title>Simple JavaScript Template</title>
</head>
<body>
<header>
<h1>@learnMern</h1>
<h3>Simple JavaScript Template</h3>
<p>Everything below this line is created with JavaScript</p>
</header>
<div class="container">
<div class="container container__main">
<div class="container container__header">
<i class="fas fa-phone fa-3x"></i>
<h1>Validator</h1>
</div>
<input id="userPhoneNum" type="text" placeholder="Enter phone number">
<button class="btn btn__primary" onclick="submit()">Validate</button>
<button class="btn btn__secondary" onclick="clearSubmit()">Clear</button>
<div id="content" class="content">
<h3>Phone Number Validation Return</h3>
<p id="content__phoneNumber"></p>
</div>
</div>
</div>
</body>
</html>
:root {
--primary-light: #8abdff;
--primary: #6d5dfc;
--primary-dark: #5b0eeb;
--white: #ffffff;
--greyLight-1: #e4ebf5;
--greyLight-2: #c8d0e7;
--greyLight-3: #bec8e4;
--greyDark: #9baacf;
}
$shadow: 0.3rem 0.3rem 0.6rem var(--greyLight-2),
-0.2rem -0.2rem 0.5rem var(--white);
$inner-shadow: inset 0.2rem 0.2rem 0.5rem var(--greyLight-2),
inset -0.2rem -0.2rem 0.5rem var(--white);
html {
box-sizing: border-box;
overflow-y: scroll;
background: var(--greyLight-1);
color: var(--primary);
@media screen and (min-width: 900px) {
font-size: 75%;
}
}
h3 {
color: rgba(109, 93, 252, 0.89);
}
p {
color: rgba(109, 93, 252, 0.6);
}
header {
border-bottom: 1px solid rgba(255, 255, 255, 0.6);
}
.container {
width: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
font-family: "Poppins", sans-serif;
background: var(--greyLight-1);
&__main {
width: 50%;
min-height: 200px;
border-radius: 1rem;
margin: 4rem;
padding: 2rem;
box-shadow: 0.8rem 0.8rem 1.4rem var(--greyLight-2),
-0.2rem -0.2rem 1.8rem var(--white);
}
&__header {
width: 100%;
justify-content: space-between;
padding: 2rem;
box-shadow: $shadow;
border-radius: 1rem;
color: rgba(109, 93, 252, 0.89);
}
}
.content {
display: none;
flex-direction: column;
border-radius: 1rem;
box-shadow: 0.8rem 0.8rem 1.4rem var(--greyLight-2),
-0.2rem -0.2rem 1.8rem var(--white);
padding: 2rem;
width: 100%;
margin: 2rem 0;
}
span {
color: #ff073a;
font-weight: bold;
}
input {
width: 100%;
margin: 2rem 0 1rem 0;
height: 3rem;
border: none;
border-radius: 1rem;
font-size: 1rem;
padding-left: 1rem;
box-shadow: $inner-shadow;
background: none;
font-family: inherit;
color: var(--greyDark);
&::placeholder {
color: var(--greyLight-3);
}
&:focus {
outline: none;
box-shadow: $shadow;
+ .search__icon {
color: var(--primary);
}
}
}
.btn {
width: 100%;
height: 3rem;
margin: 2rem 0 1rem 0;
font-family: inherit;
font-size: 1rem;
border: 0;
padding: 0;
border-radius: 1rem;
box-shadow: $shadow;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: 0.3s ease;
&__primary {
background: var(--primary);
box-shadow: inset 0.2rem 0.2rem 1rem var(--primary-light),
inset -0.2rem -0.2rem 1rem var(--primary-dark), $shadow;
color: var(--greyLight-1);
&:hover {
color: var(--white);
}
&:active {
box-shadow: inset 0.2rem 0.2rem 1rem var(--primary-dark),
inset -0.2rem -0.2rem 1rem var(--primary-light);
}
}
&__secondary {
color: var(--greyDark);
&:hover {
color: var(--primary);
}
&:active {
box-shadow: $inner-shadow;
}
}
}
@media only screen and (min-width: 768px) {
input {
width: 30%;
}
.btn {
width: 20%;
}
.container__main {
justify-content: space-between;
}
.btn__primary {
position: relative;
left: 10%;
}
}
// DOM variables
const userPhoneNum = document.getElementById("userPhoneNum");
const content = document.getElementById("content");
const content__phoneNumber = document.getElementById("content__phoneNumber");
// regex variable to match valid 10 digit phone numbers 555-555-5555, (555)555-5555, (555) 555-5555, 555 555 5555, 5555555555, 1 555 555 5555
const regex = /^1?\s?(\(\d{3}\)|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm;
// logs an explanation of the regex
const regexSummary = (regex) => {
console.log(`regex: ${regex} \n
**** ^1? **** matches strings that start with 1 and 1 is optional.
^ anchor matches the beginning of the string.
? quantifier is for optional\n
**** \\s? **** followed by a space and space being optional
\\s character class matches whitespace; spaces, tabs, and line breaks.\n
**** (\\(\\d{3}\\) **** followed by open parentheses and three digits followed by closed parentheses. \n
\\d matches any digit 0-9
{3} matches three digits \n
**** | **** or \n
| acts as a boolean Or and matches the expression befor or after the | \n
**** \\d{3})(\\s|-)? **** followed by three digits followed by an optional space or hyphen. \n
**** | **** or \n
**** \\d{3}(\\s|-)? **** followed by three digits and an optional space or hyphen. \n
**** \\d{4}$ **** followed by four digits found at the end of the string. \n
$ matches the end of the string.
`);
};
// validates a phone number
const validatePhone = (string) => {
regexSummary(regex);
if (string.match(regex)) {
console.log(true);
return true;
} else {
console.log(false);
return false;
}
};
// handler functions
const submit = () => {
let phoneNumber = userPhoneNum.value;
if (validatePhone(phoneNumber)) {
content.style.display = "flex";
content__phoneNumber.innerHTML = `The phone number <span id="phoneNumber__span">${phoneNumber}</span> is valid!!!`;
document.getElementById("phoneNumber__span").style.color = "#49fb35";
userPhoneNum.value = "";
} else {
content.style.display = "flex";
content__phoneNumber.innerHTML = `The phone number <span id="phoneNumber__span">${phoneNumber}</span> is NOT valid!!!, please enter a valid 10 digit number`;
document.getElementById("phoneNumber__span").style.color = "#ff073a";
userPhoneNum.value = "";
}
};
const clearSubmit = () => {
content.style.display = "none";
content__phoneNumber.innerHTML = "";
console.clear();
};
Also see: Tab Triggers