We are seeking a solution for our component that includes a background image, requiring it to be loaded through CSS. Our front-end developer prefers using
background: url(/*path here*/)...
. The proposed solution involves retrieving the image path from Sitecore and embedding it in the HTML as follows:
@string src = // Get image path from Sitecore().Field("Picture");
<div style="background: url(@src) left top no-repeat;"> ... </div>
However, this approach presents two issues:
- It complicates the process for content editors to update the image, as they must manually change it through
edit item
. - It seems like a makeshift fix or workaround.
The question arises: Is there a method to modify the CSS of an element using Razor/Sitecore, specifically targeting the background:
property?