当前位置:首页 > CSS

css制作固定模板

2026-02-12 15:31:00CSS

使用CSS创建固定模板

固定模板通常指具有固定头部、底部或侧边栏的布局结构。以下是几种常见实现方法:

css制作固定模板

固定头部和底部

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial;
        }
        .header {
            position: fixed;
            top: 0;
            width: 100%;
            height: 60px;
            background: #333;
            color: white;
            text-align: center;
            line-height: 60px;
        }
        .content {
            margin-top: 60px;
            margin-bottom: 60px;
            padding: 20px;
            height: calc(100vh - 140px);
            overflow-y: auto;
        }
        .footer {
            position: fixed;
            bottom: 0;
            width: 100%;
            height: 60px;
            background: #333;
            color: white;
            text-align: center;
            line-height: 60px;
        }
    </style>
</head>
<body>
    <div class="header">头部区域</div>
    <div class="content">主要内容区域</div>
    <div class="footer">底部区域</div>
</body>
</html>

固定侧边栏布局

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial;
        }
        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 200px;
            height: 100vh;
            background: #333;
            color: white;
        }
        .main-content {
            margin-left: 200px;
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="sidebar">侧边栏</div>
    <div class="main-content">主要内容区域</div>
</body>
</html>

使用CSS Grid创建固定模板

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial;
            display: grid;
            grid-template-rows: auto 1fr auto;
            grid-template-columns: 100%;
            min-height: 100vh;
        }
        header {
            grid-row: 1;
            background: #333;
            color: white;
            padding: 20px;
            text-align: center;
        }
        main {
            grid-row: 2;
            padding: 20px;
        }
        footer {
            grid-row: 3;
            background: #333;
            color: white;
            padding: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <header>头部区域</header>
    <main>主要内容区域</main>
    <footer>底部区域</footer>
</body>
</html>

响应式固定模板

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial;
        }
        .container {
            display: flex;
            min-height: 100vh;
        }
        .sidebar {
            width: 250px;
            background: #333;
            color: white;
            position: fixed;
            height: 100vh;
        }
        .content {
            margin-left: 250px;
            padding: 20px;
            flex: 1;
        }
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                height: auto;
                position: relative;
            }
            .content {
                margin-left: 0;
            }
            .container {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="sidebar">侧边栏</div>
        <div class="content">主要内容区域</div>
    </div>
</body>
</html>

关键CSS属性说明

  • position: fixed - 使元素固定在视口中
  • top/bottom/left/right - 控制固定元素的位置
  • calc() - 用于动态计算尺寸
  • vh - 视口高度单位
  • grid - CSS网格布局
  • flex - 弹性盒布局
  • @media - 响应式设计媒体查询

这些方法可以根据具体需求组合使用,创建出各种固定布局模板。

标签: 模板css
分享给朋友:

相关文章

css动画制作

css动画制作

CSS动画制作方法 使用@keyframes规则定义动画序列,通过animation属性将动画应用到元素上。以下是一个基础示例: @keyframes fadeIn { from { opaci…

用css制作导航条

用css制作导航条

水平导航条制作 使用display: inline-block或flexbox实现水平导航 为<ul>和<li>设置样式消除默认列表样式 添加padding和margin控制间…

css怎么制作段落

css怎么制作段落

段落样式基础设置 使用 p 标签定义段落,通过CSS控制字体、间距等属性: p { font-family: Arial, sans-serif; font-size: 16px;…

css表头制作

css表头制作

CSS 表头制作方法 固定表头(滚动时保持可见) 使用 position: sticky 属性可以创建滚动时固定的表头。确保父容器有明确的高度和 overflow 属性。 .table-conta…

css制作靶子

css制作靶子

CSS 制作靶子 使用 CSS 可以轻松创建一个靶子效果,主要通过嵌套的圆形元素和背景色实现。以下是实现方法: HTML 结构 <div class="target"> &…

vue实现a4打印模板

vue实现a4打印模板

Vue 实现 A4 打印模板 页面布局与样式设置 设置 A4 纸张尺寸(210mm × 297mm)的打印样式,通过 CSS 确保打印内容适配纸张。在 Vue 组件的 <style> 标签…