Add a stylesheet link programmatically in ASP.NET

Add a stylesheet link programmatically in ASP.NET

Code snippet for programmatically adding a CSS link in ASP.NET.

Migrated article

This article was migrated from an older iteration of our website, and it could deviate in design and functionality.

Here’s a code snippet used to programmatically insert a stylesheet link to an external CSS file:

// Create the <link> element for the CSS file
var stylesheet = new HtmlLink { Href = "/path/to/stylesheet.css" };
stylesheet.Attributes.Add("rel","stylesheet");
stylesheet.Attributes.Add("type","text/css");
 
// Add it to the <head> element of the page
Page.Header.Controls.Add(stylesheet);
Last updated: 2016-07-13