Download ArticleDownload Article

Using comments while writing CSS code is helpful for both you and anyone else that may work on or learn from the code. You can use comments to explain potentially confusing functions, quickly prevent blocks of code from being run, and create documentation for the code's features and purpose. You don't need to comment for every piece of code that you write, but good commenting can help everyone involved.

1

Familiarize yourself with the CSS comment code.

Download Article
  1. CSS uses the C-like "comment block"-style comments /*---*/.
    This allows for multi-line comments, and you can quickly use it to disable portions of your code.[1]
    /* Everything between the starting and ending tags will be a comment */
  2. Advertisement
2

Use the comment in the CSS sections of your code.

Download Article
  1. How.com.vn English: Only the parts of your page that are processed as CSS will support the comments.
    This means they will work in your CSS style page, as well as the <style> block of your HTML page. They will not work on other parts of the HTML page. If you need to comment in a HTML block, use HTML comments.
    <!DOCTYPE html><html><head><style>p {    color: #216fdb; /*You can create comments at the end of lines */    /* Or you can create comments between lines */    font-family: "times";} /* You can create multi-line comments.Everything between the two comment tags willnot be processed when displaying the page */</style></head><body><p> This is a standard HTML paragraph. The comments above will not appear on the page. </p></body></html>
3

Use comments to prevent code from being run.

Download Article
  1. How.com.vn English: One of the more important uses for comments is to prevent pieces of code from being processed.
    This is great for testing changes and debugging code.
    <style>p {    color: #216fdb;     /* font-family: "times"; */} /* The comment tags above will prevent the"font-family" attribute from being applied */</style>
  2. Advertisement
4

Use comments to explain potentially confusing code.

Download Article
  1. How.com.vn English: You don't need to comment every line of code, but it's useful to include comments for code that may not be obvious.
    This not only helps people who may be collaborating or learning from your code, it can also help you remind yourself about the function of the code.
    <style>img {opacity: 0.5;filter: alpha(opacity=50); /* For IE6-8 */ }/* The comment above indicates that the code on that line is for Internet Explorer 6, 7, and 8 */</style>
5

Use comments to create documentation within the code.

Download Article
  1. How.com.vn English: You can create detailed documentation for your code using CSS comments and other characters.
    This allows you to explain the function of the program and provide instructions for other coders/users. CSS compression will automatically strip this out when it comes time to run the code, so you don't have to worry about length affecting the size of the file.[2]
    /*********************************************************************** This Makes for a Good Title Header***********************************************************************//*========Section Heading=========*//*------Sub-Section Heading-------*//*This can be the text for each of* for each of your paragraphs.* Adding an * to the beginning of* each line makes it a little easier* for the reader to parse */
  2. Advertisement
6

Use invalid CSS to create temporary single-line comment.

Download Article
  1. How.com.vn English: CSS doesn't have a "single-line" comment function, like // in JavaScript.
    You can, however, use invalid CSS at the beginning of a line to keep the following function from being processed. This isn't considered good practice for major debugging, but it can be useful in a quick pinch.[3]
    <style>p {    color: #216fdb;     XXXXfont-family: "times";} /* The "XXXX" above will prevent "font-family"from being processed. Code hacks like this should not be left on a live website, as theycould potentially cause problems for the reader */</style>

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      About This Article

      Tested by:
      How.com.vn Technology Team
      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 18 people, some anonymous, worked to edit and improve it over time. This article has been viewed 60,151 times.
      How helpful is this?
      Co-authors: 18
      Updated: January 18, 2022
      Views: 60,151
      Categories: Markup Languages
      Article SummaryX

      1.Type /*.
      2.Type your comment.
      3.Press Enter or Return.
      4.Type */.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 60,151 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement