What syntax is used to define a CSS variable?

Prepare for the CodeHS Advanced HTML and CSS Test. Leverage comprehensive questions with detailed explanations and hints. Ace your exam with tailored practice!

The syntax used to define a CSS variable is indeed to use the format that begins with two hyphens followed by the variable name, and then the value. This is reflected in the answer that specifies two hyphens, as it is the standard convention in CSS to declare custom properties, or CSS variables.

When CSS variables are created, they are typically defined within a selector's block, like this:


:root {

--my-variable: 10px;

}

The :root pseudo-class allows you to define the variable globally, making it accessible throughout the entire document. After defining a variable in this way, it can be reused across various CSS rules by referencing it with the var() function. For example:


margin: var(--my-variable);

This method of variable declaration not only allows for easy maintenance of styles but also promotes better usage of values throughout the stylesheet, as changes to the variable will reflect wherever it is used. The other provided options do not conform to the correct syntax for defining CSS variables, thus affirming the chosen answer is the correct one.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy