CKTIS / CSS
CSS comments /* */ and declaration structure
Use `/* */` to leave notes in CSS, and learn to read a declaration as a property name, a colon, and a value. Semicolons separate declarations in a block.
Version: Current CSS specifications; browser support varies by feature
Syntax
/* comment */
selector {
property-name: value;
another-property: another-value;
}A CSS comment starts with /* and ends at the first */ that follows. It can span one line or several. The browser does not apply declarations inside a comment, so comments are useful for notes or for temporarily disabling code. Comments cannot nest, and // does not start a CSS comment. If */ is missing, the comment consumes the rest of the CSS input.
In a simple style rule, a selector such as .demo identifies the elements the rule targets. The braces { and } enclose the declaration block. In color: blue;, color is the property you are setting, : separates it from the value blue, and ; separates the declaration from the next one. The final semicolon before } is optional, but keeping it makes it easier to add another declaration.
Correct structure alone is not enough: the value must be allowed for that property. 20px is valid for font-size, but not for color. The browser discards color: 20px;, not the whole block.
Examples
<!doctype html>
<meta charset="utf-8">
<title>CSS declaration example</title>
<style>
.demo {
color: blue;
/* color: red; */
color: 20px;
font-size: 20px
}
</style>
<pre class="demo">Example</pre>
<script>
const demo = document.querySelector('.demo');
const style = getComputedStyle(demo);
const output = `color: ${style.color}\nfont-size: ${style.fontSize}`;
demo.textContent = output;
console.log(output);
</script>Result
color: rgb(0, 0, 255) font-size: 20px
The text color is `rgb(0, 0, 255)` and the font size is `20px`. The `color: red;` declaration is commented out, `color: 20px;` is invalid, and the last declaration works without a semicolon.
Related lessons
- CSS · Lesson 1 · Basic · Version: 3