简体   繁体   English

CSS 在样式表中不起作用

[英]CSS not working in stylesheet

I have a set of Styles that were first created inside the style attribute on a page.我有一组样式,它们首先在页面的样式属性中创建。

I want to move it from being on the page itself into a stylesheet.我想将它从页面本身移到样式表中。

however, when I move it to a .css file, the page breaks, move the code back to the html doc and it works fine again.但是,当我将它移到 .css 文件时,分页符,将代码移回 html 文档,它又可以正常工作了。

This makes absolutely no sense, moving styles from a style to a css file shouldnt break the code should it?这绝对没有意义,将样式从样式移动到 css 文件不应该破坏代码吗?

Am I missing something?我错过了什么吗? I am not changing any of the code, its simply a copy and paste.我没有更改任何代码,它只是复制和粘贴。

This is just a shot in the dark as (at the time of this post) you haven't provided source code.这只是黑暗中的一个镜头,因为(在撰写本文时)您还没有提供源代码。

Make sure you're linking to your stylesheet using a link tag in the head of the HTML document.确保使用 HTML 文档headlink标记链接到样式表。

If you had:如果你有:

<style type="text/css">
/* <![CDATA[ */
#someid
{
  margin: 0;
  padding: 3px 12px;
}
/* ]]> */
</style>

You'll need to have你需要有

#someid
{
  margin: 0;
  padding: 3px 12px;
}

in your CSS file with:在您的 CSS 文件中使用:

<link rel="stylesheet" type="text/css" href="path/to/style.css" />

to link to the stylesheet.链接到样式表。

Some common newbie mistakes include:一些常见的新手错误包括:

  • <style type="text/css" src="path/to/style.css"> : because it's a similar syntax to the <script> tag, which would make sense, but is invalid <style type="text/css" src="path/to/style.css"> :因为它的语法与<script>标签相似,这很有意义,但无效
  • <link rel="stylesheet" src="path/to/style.css"> : but link elements use href not src <link rel="stylesheet" src="path/to/style.css"> :但link元素使用 href 而不是 src
  • placing link elements within the body: although browsers will tend to manage link elements in the body, there are likely going to be some errors, and it's not a defined behavior在 body 中放置link元素:虽然浏览器会倾向于在 body 中管理link元素,但可能会出现一些错误,这不是一个定义的行为
  • not specifying a doctype declaration: allows the browser to go into quirks mode, which is never a good idea.指定 doctype 声明:允许浏览器进入怪癖模式,这从来都不是一个好主意。

You should make sure the stylesheet is properly imported.您应该确保正确导入样式表。 Sometimes the @import doesn't work well if not used accordingly, so always reference your stylesheet:有时,如果不相应地使用@import无法正常工作,因此请始终参考您的样式表:

<link rel="stylesheet" type="text/css" href="name-of-stylesheet.css" />

Always remember to close the <link> tag as it's a self-close tag.永远记住关闭<link>标签,因为它是一个自关闭标签。 I think @zzzzBov forgot to mention that.我认为@zzzzBov 忘了提及这一点。

Finally, if that doesn't work, try to override some of the styles by physically writing (above the </head> section) something like:最后,如果这不起作用,请尝试通过物理编写(在</head>部分上方)来覆盖某些样式,例如:

<style type="text/css">
    body { background: blue; }
    * { color: red; }
</style>

and see if that gives you a blue background and red colored text.看看这是否会给你一个蓝色背景和红色文本。 It should.它应该。 After that, try to implement the referencing method and make sure you reference the stylesheet file to the right directory.之后,尝试实现引用方法并确保将样式表文件引用到正确的目录。

Good luck!祝你好运!

I had the same problem, but the cause was not some mistake in the code but the fact that the .css file was loaded with some delay after making the modifications in it.我遇到了同样的问题,但原因不是代码中的错误,而是 .css 文件在对其进行修改后加载延迟的事实。 The server needed 5 - 10 minutes to update the changes.服务器需要 5 到 10 分钟来更新更改。

I had this problem as well, and the reason was that the path had to be updated for some url() references since the css file was in another folder than the html file it previously was called from.我也有这个问题,原因是必须更新某些 url() 引用的路径,因为 css 文件位于另一个文件夹中,而不是之前调用它的 html 文件。

So basically所以基本上

background-image: url('patterns/debut_dark.png');

had to be changed to必须改为

background-image: url('../patterns/debut_dark.png');

不要在 .css 文件中包含<style type="text/css"></style>

I had the same issue and was quite frustrating.我有同样的问题,非常令人沮丧。 I had a css file that was properly referenced, however not all the elements were being loaded from it.我有一个正确引用的 css 文件,但并非所有元素都从中加载。 As it turns out, it was a cache problem in Chrome.事实证明,这是 Chrome 中的缓存问题。 After clearing it and restarting the window, the css elements were working correctly.清除它并重新启动窗口后,css 元素工作正常。

Ran across same problem.遇到同样的问题。 Found there were lines in my css file that should have been commented out (a block of colour palette information that I had cut and paste to the top of the file for quick reference).发现我的 css 文件中有一些应该被注释掉的行(我剪切并粘贴到文件顶部以供快速参考的调色板信息块)。

If all your syntax seems fine, then its most likely a browser cache problem that we can easily fix.如果您的所有语法看起来都很好,那么它很可能是我们可以轻松修复的浏览器缓存问题。 In your html/php file, reference your new .css style sheet (eg styles.css) by adding an extra random parameter.在您的 html/php 文件中,通过添加一个额外的随机参数来引用您的新 .css 样式表(例如 styles.css)。 This will force browsers visiting your page to fetch your latest styles.css.这将强制访问您页面的浏览器获取您最新的 styles.css。

In the of your html/php file, you should have something like this to load your new styles.css file:在你的 html/php 文件中,你应该有这样的东西来加载你的新 styles.css 文件:

<link rel="stylesheet" type="text/css" href="styles.css" />

simply change it to be like this:只需将其更改为这样:

<link rel="stylesheet" type="text/css" href="styles.css?ref=v1" />

that extra "?ref=v1" will prevent browsers from re-using the styles.css file they have cached, and will force browsers to get your very latest styles.css file.额外的“?ref=v1”将阻止浏览器重新使用它们缓存的styles.css文件,并强制浏览器获取最新的styles.css文件。 As you make updates to the styles.css file and upload them to your web server, just change the "v1" to "v2" etc. or whatever naming system you like so that browsers are forced to reload the latest styles.css.当您更新styles.css 文件并将它们上传到您的Web 服务器时,只需将“v1”更改为“v2”等或您喜欢的任何命名系统,以便浏览器强制重新加载最新的styles.css。 Note adding this "?ref=v1" to the link does not need you to change the name of your styles.css file (you can change the file name but i find that gets messy).请注意,将此“?ref=v1”添加到链接不需要您更改您的styles.css 文件的名称(您可以更改文件名,但我发现它会变得混乱)。 This is a simple and clean way to force browsers into re-fetching your very latest .css file.这是强制浏览器重新获取最新的 .css 文件的简单而干净的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM