Upon discovering that comments cannot be included within element attributes as per other users' statements, I became curious as to why this is the case. While it is considered bad practice, what exactly prevents it from working?
Referencing the specifications for HTML5 comments and HTML4.01 comments, I found the answer in the HTML4.01 spec:
Note that comments are markup
The opening "<!
" and closing ">
" symbols in a comment serve to indicate a declaration, while the "--
" sequence identifies it as a comment. This distinction becomes clear when comparing comment syntax to doctype declarations and CDATA sections.
Due to the inability to include declarations within attribute values (although not explicitly stated in the spec, it is implied), comments cannot be embedded within attribute values.
Interestingly, the HTML5 comments section does not mention the 'comments are markup' note. Nonetheless, I am confident that the same rules still apply.
If any of this information is incorrect, please feel free to share any necessary corrections.