LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code

HTML, CSS, Forms - A Test

When using ::placeholder in connection with the attribute of the same name (placeholder="your text") being put in an input field, we can tell our users what or how they should fill an input field in our precious webforms.

On top of that we love to tell people which fields they have to fill out in order to be able to submit said webform to us. Therefore we got the :required pseudo-class to style form elements which we deem important – you sure want to write required into the input then for the styling to take effect. <input required /> will do by the way, as it is a boolean and therefore it defaults to true when it's there.

I think I found a nice way to combine both the pesudo-element with the pseudo-class in order to give people a hint on required fields.

Notice: Browser Support is an issue when it comes to IE. "&#$§¥ you IE!." Anything lower than IE10 won't understand those selectors. *shakes fist towards the sky*

Enough with the chatter, here we go with the example form:


Please tell me a few things about you! Required fields are marked with a blue border.

Interestingly enough the placeholder text for <input type="number" /> does not change its color although the field is set to required in the HTML. The border color though did change.

Update:

Now, that I added the :valid and :invalid selectors to the CSS, there's another funky thing going on. The <number> input is now marked as invalid although it's empty (apart from the placeholder, which now received the color I set for invalid input values). STRRRRAAAAANNNGE!

Apart from that you would have to know Regular Expressions in order to prevent the type="text" inputs to take any character. You'd want to prevent people from using numbers for their name, right? There might be a use-case for this (numbers in names that is) but I honestly don't know, so I'd go the way of not preventing the user from typing whatever he thinks his name really is. - Yours Batman1982-0H-Y34H

Interesting find: When setting the submit button styles related to the form being valid or invalid you can give users visible feedback on whether they can submit the form or not.

CSS
/*
  Let's say Firefox has issues, okay?
*/
button::-moz-focus-inner,
input::-moz-focus-inner {
  border: 0;
  padding: 0;
}

/*
  As little styling as we need
*/
body {
  font-family: sans-serif;
  font-size: 1.25em;
  line-height: 1.55;
  padding: 1.618em 5%;
  max-width: 900px;
  margin: 0 auto;
}

code {
  font-family: Consolas, Monaco, "Courier New", Courier, monospace;
  background: #f0f0f0;
  font-size: .875em;
  color: #333;
  padding: .25em;
}

.spoken {
  font-family: "Comic Sans MS";
  text-align: center;
  float: right;
  padding: 2em;
  margin: 1em 0 1em 1em;
  background: #ffa;
  border: .3125em solid #333;
  border-radius: 50% 75%;
  width: 300px;
  -webkit-transform: rotate(-15deg);
          transform: rotate(-15deg);
}

hr {
  border: 0;
  height: .08em;
  background: #ebc533;
  margin: 2em
}

small {
  font-size: .75em;
  display: block;
  margin: .5em 0;
}

small em {
  color: #595959;
}

/*
  The actual form - finally
*/

form {
  margin: 2em 0;
}

fieldset {
  margin: 0 0 2em;
}

legend {
  font-weight: bold;
  padding: 0 .5em;
}

label {
  display: block;
  margin: 1em 0 0;
  cursor: pointer;
  color: #595959;
}

input,
textarea {
  border: .125em solid #e5e5e5;
  padding: .5em;
}

textarea {
  box-sizing: border-box;
  resize: vertical;
  width: 50%;
  height: 8em;
}

input:not(:focus):valid,
textarea:not(:focus):valid{
  border-color: green;
  color: green;
}

input:not(:focus):invalid,
textarea:not(:focus):invalid {
  border-color: red;
  color: red;
}

input:required,
textarea:required {
  border-color: darkblue;
}

::-webkit-input-placeholder {
  color: #777;
}

::-moz-placeholder {
  color: #777;
}

:-ms-input-placeholder {
  color: #777;
}

::placeholder {
  color: #777;
}

input:required::-webkit-input-placeholder,
textarea:required::-webkit-input-placeholder{
  color: orange;
  opacity: 1;
}

input:required::-moz-placeholder,
textarea:required::-moz-placeholder{
  color: orange;
  opacity: 1;
}

input:required:-ms-input-placeholder,
textarea:required:-ms-input-placeholder{
  color: orange;
  opacity: 1;
}

input:required::placeholder,
textarea:required::placeholder{
  color: orange;
  opacity: 1;
}

button {
  color: darkblue;
  border: 2px solid darkblue;
  padding: .309em .618em;
}

form:valid button {
  color: green;
  border-color: green;
}

form:invalid button {
  color: red;
  border-color: red;
}
Host Instantly Drag and Drop Website Builder

 

Description

I want to test form validation and how to mark required fields in an accessible but also semantic and straight-forward way.
Term
Mon, 11/27/2017 - 22:04

Related Codes

Pen ID
Pen ID
Pen ID