OK, let's look at your legal obligations. CC BY-SA 4 says in s3(a)(2) that
You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information.
which isn't, I'd agree, terribly definitive. It's generally well-understood (see best practice guidelines, qv) that for inlined images all good attributions should be placed by the image in question. I understand that for an image used as background that's not possible; your question is whether non-rendering content (eg, HTML or JS comments) constitute a "reasonable manner" for including the attribution.
I would argue they do not. You can't put the attribution beside the image, because the image is used in a way that doesn't permit it. But you should make as few departures from best-practice for inlined images as are required by the way the image is used to have the best chance of justifying your behaviour as "reasonable".
Consider a footer on any page that uses CC BY-SA content for background. Or one might have a specific page for all attributions related to (eg) BY and BY-SA content used on the site, linked to from the top of every page. These are both comparably prominent to the inlined image attribution examples, and easily done for your usage case, and I would argue that they are therefore comparably reasonable.
In summary: IMO, putting the acknowledgments in HTML comments buries them more than is necessitated by the usage case.