Syntax of CSS

  • The syntax of CSS (Cascading Style Sheets) defines how CSS rules are structured and written. It consists of various components that work together to specify the styling and layout of HTML elements. Let's explore the detailed syntax of CSS:
Selectors:
  • Selectors target specific HTML elements to which the CSS rules should be applied. Selectors can be based on element names, class names, IDs, attributes, pseudo-classes, and more.
Here are some examples:

  • Element selector: `p` selects all `<p>` elements.
  • Class selector: `.my-class` selects elements with the class name "my-class".
  • ID selector: `#my-id` selects the element with the ID "my-id".
  • Attribute selector: `[type="text"]` selects elements with the attribute "type" set to "text".
  • Pseudo-class selector: `:hover` selects elements when they are being hovered over.
Declarations:
  • Declarations define the specific styling properties and values to be applied to the selected elements. Declarations consist of a property and a value, separated by a colon (:), and terminated with a semicolon (;). For example:

   
    color: blue;
    font-size: 16px;

Rule Sets:

  • A rule set is a combination of a selector and one or more declarations enclosed in curly braces ({ }). It defines the styling to be applied to the elements that match the selector. Here's an example:


    p {
        color: blue;
        font-size: 16px;
    }

Comments:

  • CSS allows you to include comments within your style sheet for documentation or clarification purposes. Comments start with `/*` and end with `*/`. For example:


    /* This is a CSS comment */

CSS At-rules:

  • CSS At-rules are special instructions that define various behaviors and functionalities in CSS. They start with the '@' symbol followed by the rule name and can have additional parameters or blocks of declarations. Some commonly used at-rules include `@media`, `@import`, `@font-face`, and `@keyframes`.
CSS Properties and Values:
  • CSS properties define the specific aspects of an element's presentation, such as color, font, size, margins, padding, etc. Each property has a corresponding value that determines how the property should be applied. For example:

    color: blue;
    font-size: 16px;

  • CSS properties and their values can vary depending on the property itself. Some properties accept predefined keywords (e.g., `display: flex`), while others require specific values (e.g., `font-size: 16px`).
  • It's important to follow the correct syntax and structure while writing CSS rules to ensure that the styles are applied correctly to the desired HTML elements.

No comments:

Post a Comment