Currently I'm trying to use a partial view inside one of my views which uses a different css style sheet. However, the styles from the main view are being carried over because of the way styles are rendered in Razor: @Styles.Render("~/Content/css")
simply applies to everything, and I can't seem to find any documentation on scoped css in Razor.
Doing the below code does not work - I believe because Razor handles Styles.Render by simply placing in the head.
<div>
<style scoped>
@Styles.Render("~/Content/othercss")
</style>
@Html.Partial("~\\Views\\Layouts\\blog.cshtml")
</div>
Does anyone know of a workaround for this?
Styles.Render()
renders link tags for the browser to grab the CSS file, something like this:Which is not what is expected to be inside a
style
element. What you want to do it output the content of that CSS file, for example:Note that this isn't using your bundle, to do that you would need to access the bundle and render that somehow (outside of the scope of this answer).
Having said all this, note that the docs for the
scoped
attribute say this:A better option would be to scope your CSS in the first place. For example:
And now this style will only apply inside a
div
with id ofwrapper
. Ans if you're writing your CSS in something like SASS (which you really should be!), it's even easier: