CDLee CDLee - 2 months ago 12
HTML Question

Is there a way to write a really long HTML attribute value in more than one line?

Trying to keep my HTML readable. Often my HTML attribute has a really long URL for it's value

Example:

<img src="https://www.google.com/logos/doodles/2016/25th-anniversary-of-armenias-independence-6257593681969152.2-hp2x.jpg"
data-alt-src="https://www.google.com/logos/doodles/2016/us-voter-registration-day-reminder-5701453076234240-hp2x.gif" />


While these aren't too long, sometimes the URL can be much more extensive. These long lines look terrible in my editor (using WebStorm). It doesn't work if I cut the long URL into multiple lines. I use "+" to concatinate strings in Java or other programming languages, but I have no idea what to do in HTML.

I feel like this is a stupid question, but I had no luck searching for a simple answer. Could you guys help me out?

Thank you!

Answer

Actually the code should still work even if you cut the line in half. HTML will view the entire bit of code as one line until it hits the line break, the ">". If your editor is giving you an error on the broken line try running it and if it doesn't work still then there might be another issue. Just make sure that you don't put a space in between it. So for example

<img src="https://www.google.com/logos/doodles/2016
/25th-anniversary-of-armenias-independence-6257593681969152.2-hp2x.jpg"

data-alt-src="https://www.google.com/logos/doodles/2016/us-
voter-registration-day-reminder-5701453076234240-hp2x.gif" />

Would work but

<img src="https://www.google.com/logos/doodles/2016/25th-
   anniversary-of-armenias-independence-6257593681969152.2-hp2x.jpg"

data-alt-src="https://www.google.com/logos/doodles/2016
  /us-voter-registration-day-reminder-5701453076234240-hp2x.gif" />

Would not.