How to escape quotes in inline styles? How to escape quotes in inline styles? xml xml

How to escape quotes in inline styles?


use single quotes and I think it should be round brackets:

<div style="background: url('http://my-url.com/img.jpg')"></div>

The " works too (tested in jsFiddle):

<div style="background: url("http://my-url.com/img.jpg")">test</div>


First off, why?

You should be using () instead of '{}'

This way is best:

<div style="background: url('http://my-url.com/img.jpg')"></div>

This way is fine:

<div style="background: url("http://my-url.com/img.jpg")"></div>

This also works:

<div style="background: url(http://my-url.com/img.jpg)"></div>

This doesn't work:

<div style="background: url(\"http://my-url.com/img.jpg\")"></div>

Note: Remove the space after url.


The second option is the correct one, as far as escaping is concerned:

<div style="background: url {"http://my-url.com/img.jpg"}"></div>

To escape double-quotes in HTML you use ", whether in attributes or not.

See this jsfiddle (taken from this SO answer).