Which characters are valid in CSS class names/selectors?
It is very easy to choose a valid class name or selectors in CSS just follow the rule.
- A valid name should start with an underscore (_), a hyphen (-) or a letter (a-z)/(A-Z) which is followed by any numbers, hyphens, underscores, letters.
- It cannot start with a digit, starting with the digit is acceptable by HTML5 but not acceptable by CSS.
- Two hyphens followed by a number is valid.
Example 1: This example describes the list of valid id selectors using CSS.
<!DOCTYPE html> <html> <head> <title> Valid id Selectors </title> <style> #main { border:2px solid green; text-align:center; } #1st { color:green; font-size:30px; } #_st { color:green; font-size:30px; } #-st { color:green; font-size:30px; } #st { color:green; font-size:30px; } #St { color:green; font-size:30px; } #--1 { color:green; font-size:30px; } #s { color:green; font-size:30px; } #_1 { color:green; font-size:30px; } #- { color:green; font-size:30px; } #-- { color:green; font-size:30px; } #_ { color:green; font-size:30px; } #__ { color:green; font-size:30px; } </style> </head> <body> <div id="main"> <div id="1st">Starting with digit GeeksforGeeks</div> <div id="_st">Starting with underscore</div> <div id="-st">Starting with hyphen</div> <div id="st">Starting with lower case alphabet</div> <div id="St">Starting with uper case alphabet</div> <div id="--1">Starting with double hyphen</div> <div id="s">only one alphabet</div> <div id="_1">underscore before digit</div> <div id="-">only hyphen</div> <div id="--">double hyphen</div> <div id="_">only underscore</div> <div id="__">double underscore</div> </div> </body> </html> |
chevron_right
filter_none
Output:

Example 2: This example describes the list of valid class selectors using CSS.
<!DOCTYPE html> <html> <head> <style> .main { border:2px solid green; } .1st { color:green; font-size:30px; background-color:#9400D3; } ._st { color:green; font-size:30px; background-color:#4B0082; } .-st { color:green; font-size:30px; background-color:#0000FF; } .st { color:green; font-size:30px; background-color:##00FF00; } .St { color:green; font-size:30px; background-color:#FFFF00; } .--1st { color:green; font-size:30px; background-color:#FF7F00; } .s { color:green; font-size:30px; background-color:#FF0000; } ._1 { color:green; font-size:30px; background-color:#9400D3; } .- { color:green; font-size:30px; background-color:#4B0082; } .-- { color:green; font-size:30px; background-color:#0000FF; } ._ { color:green; font-size:30px; background-color:##00FF00; } .__{ color:green; font-size:30px; background-color:#FFFF00; } </style> </head> <body> <div class="main"> <h1 style="color:green; text-align:center;"> GeeksforGeeks </h1> <hr> <div class="1st">Starting with digit </div> <div class="_st">Starting with underscore</div> <div class="-st">Starting with hyphen</div> <div class="st">Starting with lower case alphabet</div> <div class="St">Starting with uper case alphabet</div> <div class="--1st">Starting with double hyphen</div> <div class="s">only one alphabet</div> <div class="_1">underscore before digit</div> <div class="-">only hyphen</div> <div class="--">double hyphen</div> <div class="_">only underscore</div> <div class="__">double underscore</div> </div> </body> </html> |
chevron_right
filter_none
Output:

Recommended Posts:
- How to find the parent class name with known class in jQuery ?
- CSS | : valid Selector
- PHP | SimpleXMLIterator valid() Function
- PHP | SplObjectStorage valid() Function
- PHP | SplFixedArray valid() Function
- What are valid values for the id attribute in HTML?
- How to check a date is valid or not using JavaScript ?
- Code valid in both C and C++ but produce different output
- How to override the CSS properties of a class using another CSS class ?
- JavaScript | Check if a string is a valid hex color representation
- JavaScript | Check if a string is a valid JSON string
- How to get the last n characters of a PHP string?
- PHP | Different characters in the given string
- JavaScript to keep only first N characters in a string
- How to remove non-alphanumeric characters in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2



