css头部制作
CSS头部制作方法
使用<style>标签内联样式
在HTML文档的<head>部分直接嵌入CSS代码,适用于小型项目或快速原型开发。
<head>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: white;
padding: 1rem;
}
</style>
</head>
链接外部CSS文件
通过<link>标签引入外部CSS文件,适合中大型项目,便于维护和复用。
<head>
<link rel="stylesheet" href="styles/main.css">
</head>
使用@import规则
在CSS文件中导入其他CSS文件,适合模块化开发,但可能影响加载性能。

/* main.css */
@import url('reset.css');
@import url('typography.css');
媒体查询适配 在头部CSS中添加响应式设计,根据不同设备尺寸应用不同样式。
@media screen and (max-width: 768px) {
header {
padding: 0.5rem;
font-size: 14px;
}
}
预加载关键CSS 优化页面渲染性能,优先加载关键CSS资源。

<head>
<link rel="preload" href="critical.css" as="style">
<link rel="stylesheet" href="critical.css">
</head>
CSS变量定义
在:root伪类中定义全局CSS变量,方便统一管理设计系统。
:root {
--primary-color: #3498db;
--header-height: 60px;
}
header {
background-color: var(--primary-color);
height: var(--header-height);
}
浏览器兼容前缀 针对不同浏览器引擎添加前缀,确保样式兼容性。
header {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
字体声明优化
使用@font-face定义自定义字体,并指定加载策略。
@font-face {
font-family: 'CustomFont';
src: url('fonts/custom.woff2') format('woff2'),
url('fonts/custom.woff') format('woff');
font-display: swap;
}






