当前位置:首页 > 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创建一个基础按钮需要定义padding、background-color、border和border-radius等属性。以下是一个简单示例: .button { pad…

css 导航栏制作

css 导航栏制作

水平导航栏制作 使用无序列表<ul>和<li>标签构建基础结构,通过CSS将列表项横向排列 <nav> <ul class="horizontal-na…

css怎么制作表格

css怎么制作表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,结合CSS可以自定义样式。以下是基本实现方法: HTML结构 <table> <th…

css制作详情页

css制作详情页

CSS制作详情页的关键步骤 布局规划 确定详情页的整体布局结构,通常包括头部、商品展示区、参数区、评价区等模块。使用Flexbox或Grid布局实现响应式设计,确保在不同设备上正常显示。 样式设计…

css 制作

css 制作

CSS 制作基础 CSS(层叠样式表)用于控制网页的样式和布局。通过CSS,可以定义字体、颜色、间距、背景等视觉效果,使HTML内容更具吸引力。 内联样式 直接在HTML元素的style属性中编写C…

css 边框制作

css 边框制作

基础边框设置 使用 border 属性可快速设置边框样式、宽度和颜色: .element { border: 1px solid #000; /* 宽度 | 样式 | 颜色 */ } 样式选…