如何在我的 Sharepoint WebPart 中使用自定义 CSS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/536734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:18:39  来源:igfitidea点击:

How to use custom CSS with my Sharepoint WebPart?

asp.netcsssharepoint

提问by l3dx

'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?

我正在为 Sharepoint 开发我的第一个 WebPart,现在我想知道在哪里/如何包含/存储我的 CSS。我应该把我的 .css 文件放在哪里?我应该如何将它们包含在我的 webpart 中?

采纳答案by webwires

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

您应该使用可以链接或嵌入的 Web 部件资源。这篇文章很好地解释了如何使用它们:

Best Practices for Managing Web Part Resources

管理 Web 部件资源的最佳实践

回答by Geoff

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.

这确保 CSS 只注册和包含一次,并提供修改 CSS 的机会,而无需重新部署整个 dll。

回答by dghjfgjhfgj

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);

回答by Nat

Embedding the css into the webpart is fine if you never ever plan to change the CSS for the webpart.

如果您从不打算更改 webpart 的 CSS,那么将 css 嵌入 webpart 是可以的。

I would recommend you include the css in either a separate file stored in the style library or change a css file linked in the page layout or master page (such as core.css).

我建议您将 css 包含在存储在样式库中的单独文件中,或者更改页面布局或母版页中链接的 css 文件(例如 core.css)。

Just define unique classes for your webpart if required and leave the definition of how that renders to each website. This allows different areas of your SharePoint implemenation to show the same webpart in different ways.

如果需要,只需为您的 webpart 定义唯一的类,并留下如何呈现给每个网站的定义。这允许 SharePoint 实现的不同区域以不同方式显示相同的 Web 部件。

The means you will never have to release a dll in order to change a minor look and feel issue.

这意味着您将永远不必发布 dll 来更改较小的外观和感觉问题。