Reputation: 43
I am no expert in coding, so I decided to ask about two things I can't find answers to.
Do the empty spaces and comments in HTML code affect page rendering? I use a template as my personal website. In order to understand everything I've done so far in HTML files, I comment a lot. I also use ENTER SPACES between sections, so I can see everything clearly.
I've read about removing comments from CSS and HTML files, but didn't find a reason to do so.
Upvotes: 3
Views: 2130
Reputation: 723809
Inter-element whitespace can affect inline layout in CSS, most (in?)famously demonstrated in questions like How to remove the space between inline-block elements? that seem to treat it as some sort of virulent plague that must be eradicated. As long as you're not abusing inline layout for things it wasn't meant for, though, you'll find whitespace to be an asset rather than a hurdle in inline layout. See also the white-space
property.
Inter-element whitespace has no effect on any other kind of layout in CSS. Not block layout, table layout, float layout, flex layout, nada. If it does, it's a browser bug.
Provided there is no inter-element whitespace outside of a comment (between it and any elements), comments should have no effect on any layout. The following fragments are equivalent:
<div></div><!--
Here be dragons
--><div></div>
<div></div><div></div>
Some versions of Internet Explorer (particularly the older ones) may build DOM trees incorrectly in the presence of comments, but such issues are few and far between and not worth worrying about until you encounter one yourself.
The main reason for stripping comments and insignificant whitespace from HTML is to cut down on file size. Even if you're using tabs and not spaces for indentation, they do add up. However since minifiers don't have knowledge of layout in CSS, they won't be able to tell you if removing a certain space will have adverse effects on layout.
Upvotes: 4