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.
<div id="root"></div>
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Montserrat, sans-serif;
height: 100vh;
display: grid;
justify-content: center;
align-items: center;
color: #4f546c;
background-color: #1f1d2b;
}
.container {
background-color: white;
padding: 2rem;
border-radius: 0.2rem;
width: 20rem;
}
.container__header {
margin-bottom: 2rem;
}
.form {
display: flex;
flex-direction: column;
gap: 2rem;
}
.form__group {
display: flex;
flex-direction: column;
position: relative;
}
.form__label {
position: absolute;
top: -0.5rem;
left: 0.5rem;
background-color: white;
padding: 0 0.5rem;
font-size: 0.8rem;
}
.form__input {
padding: 1rem;
font-family: inherit;
color: inherit;
outline: none;
border: 1px solid #c7ccdb;
border-radius: 0.2rem;
}
.form__successicon, .form__erroricon {
position: absolute;
right: 1rem;
top: 1rem;
width: 1rem;
display: none;
}
.form__successicon {
fill: #00c853;
}
.form__erroricon {
fill: #ff1744;
}
.form__message {
font-size: 0.8rem;
margin-top: 0.2rem;
color: #ff1744;
display: none;
}
.form__success input {
border: 1px solid #00c853;
}
.form__success svg.form__successicon {
display: block;
}
.form__error input {
border: 1px solid #ff1744;
}
.form__error svg.form__erroricon {
display: block;
}
.form__error p.form__message {
display: block;
}
.form__button {
outline: none;
border: none;
padding: 1rem;
border-radius: 0.2rem;
font-family: inherit;
cursor: pointer;
background-color: #1f1d2b;
color: white;
}
const {useState, useEffect, useRef, StrictMode, ChangeEvent } = React
const {createRoot, } = ReactDOM
const rootElement = document.getElementById('root')
const root = createRoot(rootElement)
const App =()=>{
const initialState ={
email:'',
password:''
}
const [formValue, setFormValue] = useState(initialState)
const email = useRef(null)
const smallsEmail = useRef(null)
const password = useRef(null)
const smallsPass = useRef(null)
const button = useRef(null)
const handleChange = ({target:{name,value}})=>{
setFormValue({...formValue, [name]:value})
}
const handleBlur = ({target:{name,value}}, input ) =>{
if (input.current.name === 'email') {
if (!isEmail(input.current.value.trim() )) {
setErrorFor(input, 'Did not enter a valid email', smallsEmail.current);
}else {
setSuccessFor(input);
}
}
if (input.current.name === 'password'){
isPass(input)
}
}
const handleSumit = (e)=>{
e.preventDefault();
checkInputs()
if(isEmail(email.current.value.trim()) && isPass2(password) ){
const spiner = document.createElement("i")
spiner.classList.add("fa", "fa-spinner", "fa-spin")
console.log(spiner)
button.current.appendChild(spiner)
setFormValue({
email:'',
password:'',
})
email.current.value ='',
password.current.value =''
}
}
const handleKeyUp = ({target:{name, value}}, input) =>{
if (input.current.name === 'email') {
if (!isEmail(input.current.value.trim() )) {
setErrorFor(input, 'Did not enter a valid email', smallsEmail.current);
}else {
setSuccessFor(input);
}
}
if (input.current.name === 'password'){
isPass(input)
}
}
function checkInputs() {
// trim to remove the whitespaces
const emailValue = email.current.value.trim();
const passwordValue = password.current.value.trim();
if(emailValue === '') {
setErrorFor(email, 'You cannot leave the email blank', smallsEmail.current);
} else if (!isEmail(emailValue)) {
setErrorFor(email, 'Did not enter a valid email', smallsEmail.current);
return
} else {
setSuccessFor(email);
}
if(passwordValue === '') {
setErrorFor(password, 'Password must not be entered blank.', smallsPass.current);
return
} else {
isPass(password)
}
}
function setErrorFor(input, message, small) {
const formControl = input.current.parentElement;
formControl.classList.remove("form__success")
formControl.classList.add("form__error");
small.innerHTML = message
}
function isPass(password){
const isLowerCase = /(?=[a-z])/.test(password.current.value);
const isUpperCase = /(?=[A-Z])/.test(password.current.value);
const isNumber = /(?=\d)/.test(password.current.value);
const isSpecialChar = /(?=\W)/.test(password.current.value);
const isLongEnough = /.{8,}/.test(password.current.value);
if (!isNumber) {
setErrorForPass(password, 'The field must contain numbers', smallsPass.current);
return
} else {
setSuccessForPass(password);
}
if (!isLowerCase) {
setErrorForPass(password, 'The field must contain lowercase', smallsPass.current);
return
} else {
setSuccessForPass(password);
}
if (!isUpperCase) {
setErrorForPass(password, 'The Field must Contain Capital Letters', smallsPass.current);
return
} else {
setSuccessForPass(password);
}
if (!isSpecialChar) {
setErrorForPass(password, 'The field must contain Special characters', smallsPass.current);
return
} else {
setSuccessForPass(password);
}
if (!isLongEnough) {
setErrorForPass(password, 'The field must contain more than 8 characters', smallsPass.current);
return
} else {
setSuccessForPass(password);
}
}
function setSuccessFor(input) {
const formControl = input.current.parentElement;
formControl.classList.remove("form__error");
formControl.classList.add("form__success");
}
function setErrorForPass(input, message, small) {
const formControl = input.current.parentElement;
formControl.classList.remove("form__success")
formControl.classList.add("form__error");
small.innerHTML = message
}
function setSuccessForPass(input) {
const formControl = input.current.parentElement;
formControl.classList.remove("form__error");
formControl.classList.add("form__success");
}
function isEmail(email) {
return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}
function isPass2(password) {
const isLowerCase = /(?=[a-z])/.test(password.current.value);
const isUpperCase = /(?=[A-Z])/.test(password.current.value);
const isNumber = /(?=\d)/.test(password.current.value);
const isSpecialChar = /(?=\W)/.test(password.current.value);
const isLongEnough = /.{8,}/.test(password.current.value);
if(!isLowerCase){
return false
}
if(!isUpperCase){
return
}
if(!isNumber){
return false
}
if(!isSpecialChar){
return
}
if(!isLongEnough){
return false
}
return true
}
return(
<div className="container">
<h2 className="container__header">Login</h2>
<form action="" className="form" onSubmit={handleSumit}>
<div className="form__group">
<label htmlFor="email" className="form__label">Email</label>
<input
type="text"
className="form__input form__username"
name="email"
id="username"
placeholder="Create a username"
autoComplete="off"
onChange={handleChange}
onKeyUp={(e)=> handleKeyUp(e, email)}
onBlur={(e)=> handleBlur(e,email)}
ref={email}
/>
<svg className="form__successicon" viewBox="0 0 512 512" width="100" title="check-circle">
<path d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z" />
</svg>
<svg className="form__erroricon" viewBox="0 0 512 512" width="100" title="exclamation-circle">
<path d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zm-248 50c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z" />
</svg>
<p ref = {smallsEmail} className="form__message"></p>
</div>
<div className="form__group">
<label htmlFor="password" className="form__label">Password</label>
<input
type="password"
className="form__input form__password"
name="password"
id="form__password"
placeholder="Create a password"
ref={password}
onChange={handleChange}
onBlur={(e)=>handleBlur(e,password)}
onKeyUp= {(e)=> handleKeyUp(e, password)}
autoComplete="off"
/>
<svg className="form__successicon" viewBox="0 0 512 512" width="100" title="check-circle">
<path d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z" />
</svg>
<svg className="form__erroricon" viewBox="0 0 512 512" width="100" title="exclamation-circle">
<path d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zm-248 50c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z" />
</svg>
<p ref ={smallsPass} className="form__message"></p>
</div>
<button ref={button} className="form__button">Submit </button>
</form>
</div>
)
}
root.render(
<StrictMode>
<App/>
</StrictMode>
)
Also see: Tab Triggers