Site icon Donner's Daily Dose of Drama

ASP.Net MasterPage: How to use the page name in CSS skinning

Sometimes it can be convenient to create CSS styles that are unique to a specific page context, either because a page requires a special rule or override due to its features, or because some style rule must be applied based on the page context.

I have used this for instance in Drupal skinning when a certain color identifies the page, and rules for header font colors are created specific to pages. How can such a class id be generated “automatically” in ASP.Net, without hardcoding the page name into the markup (or, if a Masterpage is used, into the code-behind)?

Simple. My MasterPage markup contains the following:

<body class="<% = System.IO.Path.GetFileNameWithoutExtension(Request.PhysicalPath)%>"></body>

Assuming that I have a page named index.aspx, I now can create CSS rules that only apply to this page:

body.index div.title { color: #f00; }

Exit mobile version