CSS is a hyphen-delimited syntax language which means while writing HTML attributes we can use hyphen(-).
Example:
font-size, line-height, background-color, etc...
There are various reasons to preferring hyphen(-):
- In CSS, one can use underscore(_) instead of hyphen(-) for CSS selectors(class, id, span, …), but preference is given based on its ease to use.
- Underscores require hitting the Shift key and are therefore harder to type. On the other hand, CSS already uses hyphen or dash as the part of their official code so it makes more convenient to use a hyphen rather than using underscore.
- Instead of using dashes, we can also use camel case to write but it has issues to use it like, it is harder to read and whitespace between the word makes them more clear to read.
- There is also one more benefit of using dashes that it makes the code more readable.
Example:
<!DOCTYPE html>
< html lang = "en" dir = "ltr" >
< head >
< meta charset = "utf-8" >
< title >
Why are dashes preferred
for CSS selectors / HTML
attributes ?
</ title >
< style media = "screen" >
.Geeks-for-Geeks-Example-1{
background-color: forestGreen;
color: white;
height: 10vh;
text-align: center;
font-size: 4.5vh;
}
.GeeksforGeeksExample2{
background-color: Orange;
color: white;
height: 10vh;
text-align: center;
font-size: 4.5vh;
}
.Geeks_for_Geeks_Example_3{
background-color: tomato;
color: white;
height: 10vh;
text-align: center;
font-size: 4.5vh;
}
</ style >
</ head >
< body >
< div class = "Geeks-for-Geeks-Example-1" >
This is sample text for the class
name containing a hyphen.
</ div >
< br >
< div class = "GeeksforGeeksExample2" >
This is sample text for the class
name not containing a hyphen.
</ div >
< br >
< div class = "Geeks_for_Geeks_Example_3" >
This is sample text for the class
name containing an underscore.
</ div >
</ body >
</ html >
|
Output:

From the above code, it is clear that using dash is a more appropriate way of writing program instead of using a camel case or underscore and thus, due to this fact dashes are preferred for CSS selectors/HTML attributes.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Apr, 2020
Like Article
Save Article