Open In App

Why are dashes preferred for CSS selectors / HTML attributes ?

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(-):

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>
  
    <!-- Easy to read and write class name -->
    <div class="Geeks-for-Geeks-Example-1">
        This is sample text for the class
        name containing a hyphen.
    </div>
    <br>
      
    <!-- Difficult to read class name -->
    <div class="GeeksforGeeksExample2">
        This is sample text for the class
        name not containing a hyphen.
    </div>
    <br>
  
    <!-- Difficult to write class name -->
    <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.


Article Tags :