我最初想在我的 HTML 文档中包含一个 .css,它加载多个其他 .css 文件,以便为开发目的划分一些代码块。
我已经创建了一个测试页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Recipe Site</title>
<link rel='stylesheet' href='/css/main.css'>
<link rel='stylesheet' href='/css/site_header.css'>
<!-- Let google host jQuery for us, maybeb replace with their api -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="/js/main.js"></script>
</head>
<body>
<div id="site_container">
<div id="site_header"><?php include_once($r->base_dir . "inc/site_header.inc.php"); ?><!-- Include File, Update on ajax request. --></div>
<div id="site_content">
Some main content.
</div>
<div id="site_footer"><?php include_once($r->base_dir . "inc/site_footer.inc.php"); ?><!-- Include File, Update on ajax request. --></div>
</div>
</body>
</html>
文件:/css/main.css
/* Reset Default Padding & Margin */
* {
margin: 0;
padding: 0;
border: 0;
}
/* Set Our Float Classes */
.clear { clear: both; }
.right { float: right; }
.left { float: left; }
/* Setup the main body/site container */
body {
background: url(/images/wallpaper.png) repeat;
color: #000000;
text-align: center;
font: 62.5%/1.5 "Lucida Grande", "Lucida Sans", Tahoma, Verdana, sans-serif;
}
#site_container {
background-color: #FFFFFF;
height: 100%;
margin-left: auto;
margin-right: auto;
text-align: left;
width: 100%;
}
/* Some style sheet includes */
/* @import "/css/site_header.css"; */
/* Default Font Sizes */
h1 { font-size: 2.2em; }
h2 { font-size: 2.0em; }
h3 { font-size: 1.8em; }
h4 { font-size: 1.6em; }
h5 { font-size: 1.4em; }
p { font-size: 1.2em; }
/* Default Form Layout */
input.text {
padding: 3px;
border: 1px solid #999999;
}
/* Default Table Reset */
table {
border-spacing: 0;
border-collapse: collapse;
}
td{
text-align: left;
font-weight: normal;
}
/* Cause not all browsers know what HTML5 is... */
header { display:block;}
footer { display:block;}
现在是文件:/css/site_header.css:
#site_header {
background-color: #c0c0c0;
height: 100px;
position: absolute;
top: 100px;
width: 100%;
}
问题:
当我使用上面的代码时,site_header div 没有任何格式/背景。 当我从 site_header.css 的 HTML 文档中删除链接行,而是使用 @import url("/css/site_header.css");在我的 main.css 文件中,相同的结果——没有为同一个 div 呈现任何内容。
现在,当我从 site_header.css 中获取 CSS 标记并将其添加到 main.css 时,div 会得到很好的渲染...
所以我想知道是否有多个 css 文件以某种方式不起作用......或者在我以前的 css 末尾具有该 css 标记可能会产生某种冲突,尽管我找不到它会发生的原因。
最佳答案
@import
指令必须在您的 CSS 中排在第一位。一旦一种样式被浏览器选中,所有其他导入行将被忽略。
引用WC3:
"any @import rules must precede all other rules (except the @charset rule, if present)"
参见 http://www.w3.org/TR/CSS2/cascade.html#at-import
需要考虑的一件事是,每个@import 仍然会导致一个 HTTP 请求,因此它并不比使用多个链接标记更有效。事实上,它可能效率较低,因为导入可能是顺序请求而不是并行请求。 See this article . IMO 它也增加了复杂性,因为您最终在两个地方管理 CSS 引用(标记的 head 标签加上 1 个或多个 CSS 文件)而不是一个简单的链接标签列表。
我还建议您在网站处于生产阶段时可以在何处组合 CSS 文件,因为这会减少 HTTP 开销。
关于html - CSS 导入或多个 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2518807/