我把我的 CSS 放在哪里,如何从母版页链接到它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3520257/
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 22:36:52  来源:igfitidea点击:

Where do I put my CSS, how do I link to it from the master page?

csssharepoint-2010sharepoint-designer

提问by Anastasiosyal

I have installed SharePoint 2010 on a single machine in farm mode pointing to a db on another server. I would like to start dabbling into branding the site by doing something that I initially thought to be trivially straightforward, link to a custom CSS from a SharePoint 2010 master page.

我已在指向另一台服务器上的数据库的场模式下在一台机器上安装了 SharePoint 2010。我想通过做一些我最初认为非常简单的事情来开始涉足网站品牌化,从 SharePoint 2010 母版页链接到自定义 CSS。

I have uploaded a custom css (Let's call it custom.css) using SharePoint designer in the Site Assets. What syntax do I need to put to link to it? I have tried the following:

我在网站资产中使用 SharePoint 设计器上传了一个自定义 css(我们称之为 custom.css)。我需要使用什么语法来链接到它?我尝试了以下方法:

<SharePoint:CssRegistration name="custom.css" After="corev4.css" runat="server"/> 

But the server cannot find the CSS file. I receive the following error:

但是服务器找不到 CSS 文件。我收到以下错误:

Cannot make a cache safe URL for "1033/styles/custom.css", file not found. Please verify that the file exists under the layouts directory.

无法为“1033/styles/custom.css”创建缓存安全 URL,找不到文件。请验证该文件是否存在于 layouts 目录下。

  • I've assumed I need to use SharePoint:CssRegistration - Is my assumption correct?
  • So what is it exactly that I need to put in the name tag to link to a css uploaded via SharePoint designer?
  • Am I even on the right track or would you suggest an alternative way of putting this together?
  • 我假设我需要使用 SharePoint:CssRegistration - 我的假设是否正确?
  • 那么我到底需要在名称标签中放入什么才能链接到通过 SharePoint 设计器上传的 css?
  • 我是否走在正确的轨道上,或者您是否会建议将其组合在一起的替代方法?

回答by Rich Bennema

I would use Alternate CSS first. But other options are:

我会首先使用替代 CSS。但其他选择是:

  • If you put the CSS file in Style Library, you can do this:

    <SharePoint:CssRegistration name="<% $SPUrl:~SiteCollection/Style Library/custom.css%>" runat="server"/>

  • Put the CSS on the server as 14\TEMPLATE\LAYOUTS\1033\STYLES\custom\custom.css and then you can do this:

    <SharePoint:CssRegistration name="custom/custom.css" runat="server"/>

  • 如果你把 CSS 文件放在样式库中,你可以这样做:

    <SharePoint:CssRegistration name="<% $SPUrl:~SiteCollection/Style Library/custom.css%>" runat="server"/>

  • 将 CSS 放在服务器上作为 14\TEMPLATE\LAYOUTS\1033\STYLES\custom\custom.css 然后你可以这样做:

    <SharePoint:CssRegistration name="custom/custom.css" runat="server"/>

Putting the file into its own directory is considered best practice so that it does not interfere with updates to out of the box files.

将文件放入其自己的目录被认为是最佳实践,这样它就不会干扰对开箱即用文件的更新。