CSS comments: usage rules

Table of contents:

CSS comments: usage rules
CSS comments: usage rules
Anonim

Using comments in code has always been considered good form among programmers. This makes the code easier to understand, helps to divide it into logical blocks, and especially helps in collaborating on a project. In this article, we will look at how to properly use comments in CSS.

What are comments in code and why are they needed?

There are common ways to make code more readable and easier to understand - tabs, spaces, and, of course, commenting the code. Comments in the CSS code are distinguished by special characters and are not executable tags, that is, the browser ignores them, they carry only an informational load.

We'll show you how to properly comment your code.

css code
css code

Comment syntax

In order for the browser not to accept information from the block with a comment, but only to display the data, the following syntax is used:


.block { color: 000000; text-decoration: underline; /this text will be underlined/ font-style: italic; }

That is, the text is enclosed in the construction /…/. The browser will not treat CSS comments as elements to be executed. They can be placed anywhere in the document.

Very often CSS-code during the work on the project becomes long, with a lot of various blocks - one, for example, describes the header of the site, the next - the text input fields, and so on. In order to neatly highlight each of these blocks, you can also use a comment. Many developers do it this way.


/ ------------------This is the left side----------------- -/ left { background: red; width: 20% height: 100%; } / ------------------This is the right side------------------/ right { background: green; width: 80% height: 100%; }

That is, the comment itself is highlighted with hyphens to visually highlight the part of the work that needs to be noted.

css logo
css logo

Rules for creating and using comments

A comment in CSS can be either multi-line or single-line, it is not regulated. What matters is what is between the opening and closing tags.

Sometimes you need to disable a piece of code to see how the formatting will display in the browser without it. It's quite inconvenient to copy it to a new document every time, there is a risk of losing it from the clipboard - in this case, it's easiest to enclose it in the comment syntax.

Comments in CSS can consist of any set of textual, numeric or symbolic notation, no matter what language they are incompleted.

Nested comments are not supported, it is forbidden to use one comment inside another.

HTML and CSS book
HTML and CSS book

Commenting is used in the following cases:

  1. For the convenience of understanding the code by its author. When you need to make changes to the work, it is much easier to find the block when it is provided with an explanatory caption.
  2. In team work on a project, when you need to point colleagues to this or that information in the document body.
  3. For the logical division of the code into certain blocks, which also greatly simplifies the work.
  4. You can add auxiliary code snippets to the comment that you want to stop displaying at a certain point.
  5. Typically, at the beginning of the C SS code in the comment form, information about the developer and other information and technical data, such as the name of the project, the name of the author, the date of creation, and others.

In conclusion, it should be said that comments are certainly a great and necessary tool for a developer, however, the more of them in the body of the document, the greater its weight, and this can negatively affect the performance of the site.

Popular topic

Editor's choice

  • Restore Skype account: step by step instructions, return access to your account
    Restore Skype account: step by step instructions, return access to your account

    Don't know how to recover your Skype account? Or do you want to know your friend's Skype password? The article discusses several ways to restore the user's page, as well as recommendations for storing and selecting pin codes

  • Why can't I follow on Instagram? All possible reasons
    Why can't I follow on Instagram? All possible reasons

    We subscribe to people with a specific goal - to find friends, like-minded people of interest, to learn something new for ourselves. And sometimes it is very strange to see that the Instagram system does not allow you to "follow" the profile you are interested in. This error occurs quite often, no one is immune from this trouble. Let's analyze the main reasons why Instagram does not allow you to subscribe to people

  • How to find out who "liked" a person on Instagram. Proven Methods
    How to find out who "liked" a person on Instagram. Proven Methods

    Everyone who is trying to start the path of a blogger or has already achieved some success in this field, hopes that the content that he shares with readers will not be left without attention. The main indicator of page popularity is “likes”. “Thumbs up” is also put when they want to attract the attention of potential subscribers

  • How to restore a channel on YouTube: detailed instructions, simple and effective tips
    How to restore a channel on YouTube: detailed instructions, simple and effective tips

    This article describes how to restore a channel on YouTube after deleting it. To understand this task, it will be useful to familiarize yourself with some terminology. There is a difference between deleting and blocking a YouTube account

  • How to remove a contact from "Vatsap" and how to block an unwanted interlocutor
    How to remove a contact from "Vatsap" and how to block an unwanted interlocutor

    WhatsApp is a popular messenger. It has many features that users are not even aware of. One of these is the ability to block contacts. It helps to get rid of annoying interlocutors. And how to remove a contact from WhatsApp and how to block it? You can perform these actions in different ways