html - 为什么 <head> 中链接元素的顺序很重要?

标签 html css syntax

来自 Code Academy 的 Make a Website: CSS Styling :

     <head>
         <link href="font.css" rel="stylesheet">
         <link href="main.css" rel="stylesheet">   
    </head>

"Inside the head element are two link elements. The order of these link elements matters.

The browser first sees the link for the custom font font.css, and makes the font available to use in the page.

Next the browser sees the link for main.css. Since the browser now knows about the custom font, we can use it from main.css to style elements."

我想知道为什么顺序很重要(链接到主 CSS 样式表之前的字体)?

我尝试以其他顺序执行此操作(在链接到字体之前链接到主样式表)并且它仍然有效。

最佳答案

当您覆盖 CSS 定义时,顺序很重要。这是级联样式表“级联”的一部分。如果 main.css 不包含任何字体定义,那么顺序无关紧要。

例如:您从设计师那里得到了一个默认的 CSS 文件,但您需要稍微调整一下。您无需编辑 default.css 文件,而是创建一个 custom.css 文件并仅更改您想要调整的少数定义。现在顺序很重要。

<head>
    <link href="default.css" rel="stylesheet">
    <link href="custom.css" rel="stylesheet">   
</head>

如果您将 custom.css 放在首位,那么您的更改将永远不会出现。

Here's an article进入各个层次的进一步细节。警告,它会让你头晕目眩。

关于html - 为什么 <head> 中链接元素的顺序很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27086085/

相关文章:

html - 双色文字

Javascript:如何只检查复选框中的一项

javascript - bootstrap中如何获取窗口的高度和宽度

javascript - 如何让 jQuery Accordion 在页面顶部打开?

java - 为什么 Object 成员变量在 Java 中不能既是 final 又是 volatile?

javascript - 当字体太小时 &lt;input&gt; 的按钮会上升

javascript - 为什么我无法将导航栏居中?

html - 在旧浏览器中垂直对齐图像

javascript - “for 循环”和可能的语法错误

PHP - 缩进 html block