Do double forward-slash comments work in CSS code?

Is using a double-forward slash (//) for single-line comments in CSS considered reliable and supported by mainstream browsers and CSS interpreters according to the specification?

Answer №1

When it comes to CSS, comments made using double slashes // are considered invalid. According to the CSS spec, only the following is mentioned about comments:

4.3.2. Consume comments

This section explains how to consume comments from a string of code points, without returning anything.

If the next two input code points are U+002F SOLIDUS (/) followed by a U+002A ASTERISK (*), consume them along with all subsequent code points until the first U+002A ASTERISK (*) followed by a U+002F SOLIDUS (/), or until reaching an EOF (End of File) code point. Repeat this step if necessary.

If the previous paragraph ended by consuming an EOF code point, it signifies a parse error.

No return value specified.

In essence, only /* */ are recognized as valid comments in CSS. The spec does not acknowledge //.

However, in certain CSS processors like Less and SASS, // is considered valid.


In response to your query:

...can you rely on browsers to understand that's a comment

No, most browsers will interpret the syntax regardless and likely deem it as a syntax error rather than recognizing it as a comment. The outcome might vary across browsers, but utilizing it would result in undefined behavior.

Browser Response to Double Slash Comments

The application of the following rules results in varying outcomes across different browsers. One style includes using double slashes at the start of the property, while the other has // right before the value.

#some {
    width: 500px;
    /*height: 400px;*/
    //color: blue;
    background-color: //red;
}

Firefox

https://i.sstatic.net/EjMrL.png

In Firefox ESR 52.9.0, a small yellow warning triangle appears adjacent to color and background-color due to //color being an invalid CSS property, and //red representing an incorrect background-color value.

Chrome

https://i.sstatic.net/gTjuY.png

Intriguingly, in Chrome 68.0.3440.106, the line //color:blue may not even appear in the elements panel, indicating that Chrome possibly considers it a comment. However, since the recognition of // as comments is not standard, relying on it is not recommended. Furthermore, the warning for background-color persists due to //red being an incorrect value.

Safari

https://i.sstatic.net/pVgpW.png

Safari 11.1.2 exhibits similar behavior to Chrome where the properties starting with // are not listed, and the values led by // generate syntax errors.

Internet Explorer 11

https://i.sstatic.net/AquCI.png

Internet Explorer 11.0.9600.19080 interprets //color: blue as a rule property lacking a value, akin to //color: blue: ;. It also identifies background-color: //red as erroneous and doesn't execute it.


It is important to mention that in the case of the following:

#some {
//width: 400px;
/* height: 400px; */
}

Most browsers recognize and allow toggling the /* */ property in Developer Tools. In contrast, Chrome and Safari do not list the // led rule, making it impossible to toggle it as with /* */.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When the browser is refreshed, jQuery will automatically scroll the window down

I created a div that matches the height and width of the window to simulate a "home screen." When scrolling down to view the content, everything works fine. However, after refreshing the browser, it automatically scrolls back to where you were before. I wa ...

CSS transitions/transforms are failing to impact the necessary elements

I'm currently grappling with advanced transitions and transforms, and encountering some challenges with a section I'm developing. I've managed to get everything working as needed, except for the fact that there is an h5 element positioned ov ...

Remove the bootstrap styling from a section of the webpage

I am in the process of setting up a preview box for an HTML editor on one of my pages. I created a basic <div id="preview"></div> style container where I occasionally input my HTML source, which is working adequately. The issue arises when Boo ...

access various paths to distinct iframes

<?php // Specify the directory path, can be either absolute or relative $dirPath = "C:/xampp/htdocs/statistics/pdf/"; // Open the specified directory and check if it's opened successfully if ($handle = opendir($dirPath)) { // Keep readin ...

Controlling Material-UI using a custom .css stylesheet

Are there alternative methods for customizing Material-UI components using CSS stylesheets? I'm aware of the {makeStyles} method and JSS overrides, but I find it messy in the code and confusing when trying to modularize it in other files. Is there a ...

Enhancing user experience by highlighting invalid input fields with a red border using client-side HTML5 validation and Bootstrap 5.2

When reviewing the Bootstrap 5.2 validation documentation, https://getbootstrap.com/docs/5.2/forms/validation/ "It has come to our attention that the client-side custom validation styles and tooltips are not accessible at this time, as they are not ...

The width property is not being passed down to the table

I'm experiencing difficulties with the scaling of my body content. When the page is initially opened, it scales to 100%. However, when I resize the screen to make it wider, the content gets bigger but when I try to make it smaller again, it remains a ...

Adjust the position of elements based on their individual size and current position

I am faced with a challenge regarding an element inside a DIV. Here is the current setup... <div id="parent"> <div id="child"></div> </div> Typically, in order to center the child within the parent while dynamically changing i ...

Arranging Tabs in an Uneven Stack: jQuery and CSS

Looking to stack 5 tabs on mobile with the odd tab at the top instead of the bottom? Currently set to 50% width and floated left, but they fill up the top first. <div class="tab-row"> <button class="tab active">A</button> <button ...

The table spills over the container and disappears underneath the navbar

After downloading a template table from the internet and customizing it to my liking, I encountered an issue where it does not adhere to the assigned settings (as shown in the image), but that's not the only problem. The dropdown navbar on my website ...

Is it possible for links to remain clickable even if they pass underneath a div

I have implemented a shadow using a div element to cover some content beneath it. However, I am facing an issue where the div that is under the shadow cannot be interacted with. Is there any solution to this problem? Thank you ...

Resolved navigation bar problems

As a beginner in web development, I am working hard to launch my first website. Today, I have a question for the stack overflow community regarding a fixed navbar issue. The navbar I have created is functioning properly on Chrome, but when it comes to Fire ...

Struggling with the elimination of bullet points in CSS navigation bars

I'm having trouble removing the bullet points from my navigation bar. I've tried using list-style-type: none and even adding !important, but it doesn't seem to work. Strangely enough, everything looks fine in Chrome, but I get an error when ...

Is there a way to manually trigger a re-render of all React components on a page generated using array.map?

Within my parent component (Game), I am rendering child components (Card) from an array. Additionally, there is a Menu component that triggers a callback to Game in order to change its state. When switching levels (via a button click on the Menu), I want a ...

I would like the background image to perfectly fit the dimensions of the parent container

https://i.sstatic.net/PlCYU.png Is there a way to prevent the background image from overflowing and causing a horizontal scroll bar to appear? The layout of the page consists of two columns, with each column taking up 50% of the width. The left column con ...

Why does the col-sm-* class affect the appearance on extra small screens?

For a current project, I have incorporated bootstrap into my design and have set up varying column widths for xs, sm, and md screens. Initially, I utilized col-xs-* and col-md-* to ensure the columns resized appropriately for xs, md, and lg screens. Howeve ...

Adjust the width of the container and rotate the text accordingly

I am looking to display text vertically (like a y-axis chart label) and need the ability to rotate text of varying lengths while keeping it on one line. My attempt to achieve this using CSS3 transforms can be seen in this JSFiddle: .rotate { transform ...

What steps should be taken to switch a class from one li element to another and remove it in the process?

As I develop the navigation bar for my website, I am faced with a challenge. I want to create a button-like behavior where clicking on an li element triggers a drop-down section underneath it without using the # attribute to prevent jumping to the top of t ...

Tips for Aligning h5 Headings with Unordered Lists

My footer contains the following code: HTML: <div class="container"> <div class="row"> <div class="col-md-6"> <div class="pull-right"> <h5><u><i>Information</i></u> ...

Incorporate a caret into an element using CSS styling

issue I encountered an issue where <textarea> and Javascript had some quirks when trying to transfer the value into a <pre> tag. My aim was to implement a blinking caret in the <pre> as if I were pressing F7. The reason behind not using ...