CSS Comments: Best Practices to Comment Like a Pro - Position Is Everything (2024)

  • Author
  • Recent Posts

Melvin Nolan

Istarted writing code around 20 years ago, and throughout the years, I have gained a lot of expertise from hands-on experience as well as learning from others. This website has also grown with me and is now something that I am proud of.

Latest posts by Melvin Nolan (see all)

  • MySQL No Database Selected: Actionable Tips to Fix the Error - July 12, 2024
  • How to Un-Snooze Email in Outlook: A Step-by-Step Guide - July 12, 2024
  • How to Search Outlook for Multiple Words: Boost Your Efficiency - July 12, 2024

CSS Comments: Best Practices to Comment Like a Pro - Position Is Everything (3)CSS comment lets you add comments that organize or explain various segments of the stylesheet.

Although adding comments on CSS may seem extraneous when coding, it is extremely useful when redesigning or debugging a website.

Comments in CSS tell the reader the purpose of specific lines of CSS, and such insights are useful especially when multiple developers work on a website. Read on to learn how you can comment like a pro.

JUMP TO TOPIC

  • How to Comment in CSS
    • – Example – Single-Line CSS Comments
  • How to Comment Out CSS
    • – Example – Commenting out CSS Using Single-line Comment
    • – Example 2 – Commenting out CSS Using Multi-line Comment
  • Adding notes in CSS
    • – Example
  • Organizing CSS Using Comments – For Large Projects
    • – Organizing CSS Comments: Example
  • Techniques To Improve CSS Code and Comment Structure
    • – Divide and Conquer
    • – Defining a Table of Contents
    • – Defining Custom Typography and Colors
    • – Using Keywords in Comments – For Better Overview of New Changes
  • Conclusion

How to Comment in CSS

Commenting in CSS helps explain a block of code or even make temporary changes while developing websites. Browsers do not execute commented CSS code.

In CSS, comments can either be single or multi-line and they all start with /* and terminate with */. With these style sheets, developers can add as many comments as they want. Moreover, it is possible to style the comments to make them more readable.

By simply adding a text inside the /**/ you tell the browser these are notes in CSS and should not be executed. Just like programming languages that rely on the /* */ syntax for comments, you cannot nest CSS comments. It means that the first occurrence of */ after a /* terminates the comment.

The single-line comment is the most common CSS comment format.

– Example – Single-Line CSS Comments

/* set text color to blue and background color to white */

p {

color: blue;

background-color: white;

}

How to Comment Out CSS

Besides explaining what the CSS code does, comments in CSS provide a way to comment out CSS or invalidate CSS ruleset. Commenting out CSS tells the browsers not to execute that styling.

– Example – Commenting out CSS Using Single-line Comment

Using the previous example, we can comment out the font color as shown below.

p {

/* color: blue; */

background-color: white;

}

– Example 2 – Commenting out CSS Using Multi-line Comment

Also, it is possible to comment out the entire ruleset as follows.

Adding notes in CSS

Comments make it easy to add explanatory notes. Using as little as one CSS comment line, can make it possible for you to add notes next to the CSS.

– Example

p {

color: blue; /* setting the font color for paragraph to blue*/

background-color: white;

}

Note: CSS Comments do not interfere with the interpretation of other parts of the stylesheet. As well, they do not affect the front-end layout of a website. What’s more, they are easy to add even for beginners. You can place comments anywhere in the stylesheet where white space is allowed.

Organizing CSS Using Comments – For Large Projects

In huge projects, CSS files tend to be large in size which makes maintaining them difficult. So, organizing CSS code into sections is helpful because it makes it easy to find specific rules even after finishing the project.

Whenever possible, you should group various stylesheet sections using comments and separate one section from another using new lines. Putting comments for each group of CSS code provides an easy way of locating the CSS group when editing.

– Organizing CSS Comments: Example

/* Header */

/* Fonts */

/* Body */

/* Footer */

Techniques To Improve CSS Code and Comment Structure

Using sensible code structure is advisable since it makes it easy to understand the CSS code years later. Comments drastically decrease the complexity of understanding CSS code. Below are the techniques developers use to comment and simplify maintenance of CSS code, and now you can use them in your code too!

– Divide and Conquer

Establish the most important aspects and the layout of the CSS code and group common elements inside sections and give each group a title. For instance, in large projects, developers may have to import external CSS files. So, clearly separate code fragments using comments. Use more * symbols to make the heading of each section in a stylesheet more visible.

CSS Comments: Divide and Conquer Example

/*

**************************

Section for Header style

**************************

*/

Such sections make it easy to immediately recognize individual blocks while scanning through the code. The divide and conquer approach may not work for large projects since one file may become too big.

For large projects, you might need to divide the code into multiple files to make it easy to see groups of code fragments. In such cases, the master stylesheet has the role of importing groups.

Using a master stylesheet results in a clean code that is easy to understand and maintain. However, it causes unnecessary server requests, so keep that in mind if you want to use it.

CSS Comments: Master Stylesheet Example

/*——————————————————————

[Master Stylesheet]

——————————————————————-*/

@import “reset.css”;

@import “layout.css”;

@import “colors.css”;

@import “typography.css”;

@import “flash.css”;

– Defining a Table of Contents

Having a table of contents in a stylesheet helps with the overview of the structure of the code. Through CSS comments, you can include a table of contents that shows a tree overview of the layout. You can rely on some keywords like content group or header section to make it easy to navigate.

What’s more, specific elements that likely require often updates after finishing the project can be part of the table of contents. This way, it becomes easier to find them without scanning the entire code. One way of adding a table of content is through simple enumeration as shown below.

Using the example below, jumping to the Sidebar section is easy using the search tool to find 4. Sidebar. It is fast and effective.

CSS Comments: Defining a Table of Contents Example

/*——————————————————————

[Table of contents]

1. Body

2. Header / #header

3. Navigation / #navbar

4. Sidebar / #sidebar

5. Right column / #rightcolumn

6. Left column / #leftcolumn

7. Content / #content

8. Boxes / .box

9. Search / #search

10. Advertisem*nts / .ads

11. Sideblog / #sideblog

12. Footer / #footer

13. RSS / #rss

——————————————————————-*/

Also, it is possible to use CSS comments to create an indented table of content. Defining a table of contents makes it easy for others to read and comprehend your CSS code. It is especially useful when working in a team since it saves a lot of time.

– Defining Custom Typography and Colors

CSS comments provide a way of quickly referencing variables like custom colors and typography. Often, web development considers typography and colors as constants. Since CSS does not have constants, developers use CSS comments to define some at the top of the CSS file.

Usually, developers use comments to come up with a color glossary. As such, it gives them a quick reference to colors they use on a website to avoid mistakes. Also, it gives them a quick list of colors that they can change if necessary.

Example 1 – Defining Custom Colors Through CSS Comments

/*——————————————————————

[Color codes]

Background: #ffffff (white)

Content: #CCCCFF (light blue)

Header h1: #000066 (dark blue)

Header h2: #333399 (mid blue)

Footer: #b5cede (dark black)

a (standard): #333333 (dark grey)

a (visited): #666666 (mid grey)

a (active): #cc0000 (pink)

——————————————————————-*/

Moreover, developers use the same approach to define typography. They use CSS comments to include important notes that help comprehend the thinking behind the definitions.

Example 2 – Defining Custom Typography Using CSS Comments

/*——————————————————————

[Typography]

Body: 1.2em/1.6em Helvetica, Georgia, Verdana, Times New Roman;

Headers: 2.7em/1.4em Helvetica, Arial, “Lucida Sans Unicode”, Georgia, Times New Roman;

Sidebar heading: 1.5em Helvetica, Trebuchet MS, Arial, Times New Roman;

Input, textarea: 1.1em Helvetica, Georgia, Arial, Times New Roman;

Notes: reducing heading by 0.4em with every subsequent heading level

——————————————————————-*/

– Using Keywords in Comments – For Better Overview of New Changes

One interesting way developers can highlight recent changes to their CSS code is through CSS comments. Apart from indenting changed or new lines in a CSS file, developers can use keywords such as @new. This allows them to navigate to the keyword and remove changes that cause issues during execution.

CSS Comments: Using Keywords in Comments Example

#content ul li a {

display: block;

background-color: #ccc;

border-bottom: 5px solid #ddd; /* @new */

margin: 2px 0 2px 0;

padding: 4px; /* @new */

}

Comments in code are important. So, take time to explain components, their function, limitations, and how they are built. Avoid a scenario where others have to guess the purpose of uncommon CSS code. The comment style needs to be consistent and simple.

  • Whenever possible place comments in a new line above the subject
  • Ensure the length of the line is sensible
  • Use comments to break CSS code into sections

Conclusion

Commenting in CSS helps developers achieve sensible structuring by leaving useful notes and hints that are useful. In this article, you will find out examples on how to adopt CSS comments so that you can:

  • CSS Comments: Best Practices to Comment Like a Pro - Position Is Everything (4)Add comments to complex code
  • Organize CSS code
  • Assist others to comprehend your strategy and thinking
  • Understand your code in future
  • Make it easy to find changes
  • Define custom color and typography

The goal of comments in CSS is to have a code that is easy to read and maintain. Using CSS comments saves you and anyone who reviews your code a lot of trouble. If you keep practicing it long-term through our guide, that will help you understand the code years later when you go through it.

5/5 - (10 votes)

Related posts:

  1. CSS Background Image Not Working: How to Resolve and Avoid Bugs
  2. CSS Dynamic Height: Easy Techniques for Better Website Readability
  3. CSS Word Spacing: Techniques That Can Make Your Content Catchy
  4. CSS Selectors: How to Definitively Style a Website
  5. CSS Layout: Comprehensive Guide To Practical Layouts in CSS
  6. CSS Backface Visibility: How To Create Amazing Visual Effects
  7. CSS Background Blend Mode: Bring Photoshop Effects to the Web
  8. Fly Out Menus: How To Create Such Menus Using CSS
  9. CSS Hide on Mobile: Explanation of Seven Techniques That You Can Use
  10. CSS Nested Classes: How To Write More Organized and Maintainable CSS
  11. CSS Border Not Showing: Why It Happens and How You Can Solve It
  12. CSS Width: A Definitive Guide on How to Size Web Page Elements
CSS Comments: Best Practices to Comment Like a Pro - Position Is Everything (2024)

References

Top Articles
332-428 Ford FE Engine Forum-OT - Y block forum and adjusting Y block valves
Jiffy Lube 103Rd And Kedzie
SZA: Weinen und töten und alles dazwischen
Mybranch Becu
Unit 30 Quiz: Idioms And Pronunciation
Bin Stores in Wisconsin
Air Canada bullish about its prospects as recovery gains steam
35105N Sap 5 50 W Nit
Tap Tap Run Coupon Codes
سریال رویای شیرین جوانی قسمت 338
Crazybowie_15 tit*
Buckaroo Blog
Best Restaurants In Seaside Heights Nj
Call Follower Osrs
Regular Clear vs Low Iron Glass for Shower Doors
Capitulo 2B Answers Page 40
The Connecticut Daily Lottery Hub
今月のSpotify Japanese Hip Hopベスト作品 -2024/08-|K.EG
The most iconic acting lineages in cinema history
Les Schwab Product Code Lookup
Unterwegs im autonomen Freightliner Cascadia: Finger weg, jetzt fahre ich!
Where to Find Scavs in Customs in Escape from Tarkov
2020 Military Pay Charts – Officer & Enlisted Pay Scales (3.1% Raise)
Fort Mccoy Fire Map
Viha Email Login
Ahn Waterworks Urgent Care
Samantha Aufderheide
Nz Herald Obituary Notices
Purdue 247 Football
25 Best Things to Do in Palermo, Sicily (Italy)
Apartments / Housing For Rent near Lake Placid, FL - craigslist
Timeline of the September 11 Attacks
2011 Hyundai Sonata 2 4 Serpentine Belt Diagram
Cars & Trucks - By Owner near Kissimmee, FL - craigslist
Miles City Montana Craigslist
Tracking every 2024 Trade Deadline deal
Co10 Unr
LG UN90 65" 4K Smart UHD TV - 65UN9000AUJ | LG CA
Omnistorm Necro Diablo 4
Go Upstate Mugshots Gaffney Sc
Reese Witherspoon Wiki
Casamba Mobile Login
Deepwoken: How To Unlock All Fighting Styles Guide - Item Level Gaming
Big Reactors Best Coolant
Squalicum Family Medicine
Tommy Bahama Restaurant Bar & Store The Woodlands Menu
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
60 Days From August 16
Best brow shaping and sculpting specialists near me in Toronto | Fresha
Phumikhmer 2022
Worlds Hardest Game Tyrone
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 5986

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.