CSS Syntax: How is CSS Code Written?

The syntax to write CSS code consists of:

For example:

p {color: brown;}

In the above code:

Note: The declaration block consists of property and value in the form property: value;.

Note: We can have multiple declarations for a selector (an HTML element). For example:

p {color: brown; text-align: center;}

Note - All the declarations must be enclosed within a {} (an opening and a closing curly brace).

Now let's take a complete example that shows how the CSS code can be implemented to style HTML elements.

HTML with CSS Code
<!DOCTYPE html>
<html>
<head>
   <style>
      p {color: brown;}
      span {background-color: tomato; color: white;}
   </style>
</head>
<body>
   
   <p>This is <span>Para</span> one.</p>
   <p>This is Para two.</p>
   
</body>
</html>
Output

This is Para one.

This is Para two.

In the above example, the CSS code inside the "style" tags has two selectors.

The first selector p matches all p elements and gives their text a "brown" color. As a result, the text color for both p elements in the HTML will be "brown".

The second selector span matches all span elements and gives their content a "tomato" background color and a "white" text color. Only the span element within the first p element will be affected in this case. The span element's content, the word "Para," will have a background color of "tomato" and a text color of "white." The second p element is unaffected because it lacks a span element.

As a result, the first p element will have brown text, with the word "Para" inside it having a tomato background color and white text. Only brown text will appear on the second p element.

Before concluding the discussion on the general format of CSS code, I'd like to provide one more example. So here's an example to show the syntax of CSS codes. To explain the main portion of the CSS codes, I used comments.

<!DOCTYPE html>
<html>
<head>
  <style>
    /* This is a CSS comment */
    
    /* CSS selectors */
    h1 {
      /* CSS properties */
      color: #4CAF50;              /* green */
      font-size: 36px;
      font-weight: bold;
      text-align: center;
    }
    
    p {
      color: #333;                 /* dark gray */
      font-size: 18px;
      line-height: 1.5;
    }
    
    /* CSS class selector */
    .highlight {
      background-color: #FFC107;   /* yellow */
      padding: 10px;
    }
    
    /* CSS ID selector */
    #intro {
      text-align: center;
      font-size: 24px;
      margin-bottom: 20px;
    }
  </style>
</head>
<body>

  <h1>CSS General Form Example</h1>
  
  <p id="intro" class="highlight">This is an example of how to write CSS code in your HTML file.</p>
  
  <p>You can use CSS selectors to target specific HTML elements and apply styles to them. For example, 
  the CSS code above targets all <code><p></code> elements and sets their text color to dark gray, 
  font size to 18 pixels, and line height to 1.5.</p>
  
  <p class="highlight">You can also use CSS classes and IDs to target specific elements and apply unique 
  styles to them. The CSS code above targets all elements with the class name <code>highlight</code> and 
  sets their background color to yellow and padding to 10 pixels. It also targets an element with the ID 
  <code>intro</code> and sets its text alignment to center, font size to 24 pixels, and bottom margin to 
  20 pixels.</p>
  
</body>
</html>

The sample output produced by this HTML and CSS example code should look exactly like the screenshot below:

css syntax example code

CSS Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!