DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Using The :invalid CSS Pseduo Class

Using The :invalid CSS Pseduo Class

Paul Underwood user avatar by
Paul Underwood
·
Sep. 12, 12 · Interview
Like (0)
Save
Tweet
Share
5.30K Views

Join the DZone community and get the full member experience.

Join For Free

HTML comes with a set of standards which front-end developers must compile to in order for your HTML elements to be rendered correctly.

Luckily HTML is a very forgiving language and the browsers will do its best to display the HTML correctly even if the element is defined incorrectly. But HTML will have to guess what you intended to do so the output might look different.

The things which are commonly missed in front-end development can be missing quotes, missing closed tags or incorrect tags which the browser doesn't understand. The best way to check if your HTML is correctly is to run it through a validator.

W3C HTML Validator

The most popular HTML validator people use is the W3C Markup validator.

W3C Validator

The W3C Markup Validation Service

You can choose to validate your website by typing in the URL of the page, providing a file to upload or directly copying in the HTML to be validated.

Validate HTML With CSS

There is a new CSS pseudo-class, which can be used to style an input or a form tag differently if it has failed validation. This means that you can make invalid HTML elements stand out when you display the page making it easy to see which element does not validate.

The pseudo-class you need to use is :invalid.

:invalid {
    background-color: #ffdddd;
}

Browser Support

This class is currently supported on the following browsers.

  • Chrome 10 or higher
  • Firefox 4.0 or higher
  • IE 10
  • Opera 10
  • Safari 5.0

Example Using The Invalid Class

In this example we show how the invalid class works on a basic form.

<style type="text/css">
  :invalid {
    background-color: #ffdddd;
  }
  :valid {
    background-color: #ddffdd;
  }
</style>
<form>
<label>Username</label><input type="text" />
<label>Email</label><input type="email" required />
</form>

Username

Email

As you can see from this example the Email text box is highlighted red because of the required attribute is not valid HTML.

CSS HTML

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Web Application Architecture: The Latest Guide
  • Top 5 PHP REST API Frameworks
  • Artificial Intelligence in Drug Discovery
  • Three SQL Keywords in QuestDB for Finding Missing Data

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: