Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom CSS with my Sharepoint WebPart?

'ello!

I'm developing my first WebPart for Sharepoint, and now I'm wondering where/how to include/store my CSS. Where should I put my .css files? How should I include them in my webpart?

like image 687
l3dx Avatar asked Feb 11 '09 13:02

l3dx


People also ask

How do I enable custom CSS?

To enable this feature, visit Jetpack → Settings → Writing in your site's dashboard. Scroll down to the Theme Enhancements section and toggle on the Enhance CSS customization panel option. Starting with WordPress 4.7, you can now add custom CSS to your own theme directly from the Customizer.


3 Answers

This is my Approach

protected override void CreateChildControls()
{
    CssRegistration.Register("/_layouts/STYLES/WebPartName/styles.css");
}

This ensures the CSS is registered and included only once and gives the opportunity to modify the CSS without redepolying the whole dll.

like image 58
Geoff Avatar answered Oct 26 '22 07:10

Geoff


You should use Web Part Resources which can be linked or embedded. This article does a good job of explaining how to use them:

Best Practices for Managing Web Part Resources

like image 28
webwires Avatar answered Oct 26 '22 07:10

webwires


U can also use:

HtmlLink linkCss = new HtmlLink();

//Defining attributes and values of the link tag
linkCss.Attributes.Add("href", "StyleSheet1.css");
linkCss.Attributes.Add("type", "text/css");
linkCss.Attributes.Add("rel", "Stylesheet");

//Add HtmlLink instance to the header of the current page
Page.Header.Controls.Add(linkCss);
like image 38
dghjfgjhfgj Avatar answered Oct 26 '22 06:10

dghjfgjhfgj