当前位置: 首页 > news >正文

CSS手写题

html, body {

height: 100%;

margin: 0;

}

让页面元素可以使用百分比高度,并清除浏览器边距,确保布局从视口左上角开始,避免不必要的滚动条或者空白边距

1.两栏布局

<body><div class="left">红色</div><div class="right">蓝色</div>
</body>.left{height: 100%;width: 200px;float: left;background-color: red;
}.right{height: 100%;background-color: blue;
}

<body>
<div class="min"><div class="left">红色</div><div class="right">蓝色</div>
</div>
</body>.min{height: 100%;position:relative;
}
.left{position: absolute;height: 100%;width: 200px;background-color: red;
}
.right{height: 100%;background-color: blue;margin-left: 200px;
}
<body>
<div class="min"><div class="left">红色</div><div class="right">蓝色</div>
</div>
</body>.min{display: flex;height: 100%;
}
.left{height: 100%;width: 200px;float: left;background-color: red;
}.right{flex:1;height: 100%;background-color: blue;
}

2.三栏布局

<div class="container"><div class="left">红色</div><div class="min">黄色</div><div class="right">蓝色</div>
</div>.container{display: flex;height: 100%;
}
.left{width: 200px;background-color: red;
}
.min{flex:1;background-color: yellow;
}
.right{width: 200px;background-color: blue;
}

<div class="container"><div class="left">红色</div><div class="min">黄色</div><div class="right">蓝色</div>
</div>.container{position: relative;height: 100%;
}
.left{top: 0;//距离其定位父容器的顶部为0像素left: 0;position: absolute;width: 200px;background-color: red;height: 100%;
}
.min{margin-left: 200px;margin-right: 200px;background-color: yellow;height: 100%;
}
.right{top: 0;right: 0;position: absolute;width: 200px;background-color: blue;height: 100%;
}

3.圣杯布局和双飞翼布局

两者目标:

  • 实现左右两栏宽度固定中间栏宽度自适应
  • 保证中间栏内容优先渲染(SEO 和用户体验更好);
  • 三栏等高布局,即任意一栏高度变化,其他栏同步撑高。
<div class="content"><div class="center"><h1>主区域</h1></div><div class="left"><h1>左区域</h1></div><div class="right"><h1>右区域</h1></div>
</div>.content{padding-left: 300px;    // 给左盒子留位置padding-right: 200px;   // 给右盒子留位置overflow: hidden;       // 父级添加overfloat属性 清除浮动 让父盒子拥有高度
} 
// 三个盒子都要浮动
.center,.left,.right{float: left;
}
.center{background-color: pink;// 中间盒子宽度必须是百分之百width: 100%;height: 100px;
}
.left{background-color: skyblue;height: 100px;width: 300px;margin-left: -100%;right: 300px;position: relative;
}
.right{background-color: greenyellow;height: 100px;width: 200px;margin-right: -200px;
}
  1. 三栏都使用 float: left 浮动;
  2. 中间栏 width: 100%,左右栏固定宽度;
  3. 使用 负 margin 将左右栏拉到中间栏两侧;
  4. 父容器设置 padding-leftpadding-right 为左右栏腾出空间;
  5. 左右栏使用 position: relative + left/right 偏移到正确位置
  6. conten放在最前面保证最先加载

<div class="main-wrap"><div class="main">中间内容</div>
</div>
<div class="left">左边</div>
<div class="right">右边</div>.main-wrap {width: 100%;float: left;
}.main {margin-left: 200px;margin-right: 150px;height: 200px;background: red;
}.left,
.right {float: left;height: 200px;
}.left {width: 200px;background: green;margin-left: -100%;
}.right {width: 150px;background: blue;margin-left: -150px;
}
  1. 中间栏外面再包一层 .main-wrap,设置为 width: 100%
  2. 三栏都使用 float: left
  3. 中间栏通过 margin-leftmargin-right 为左右栏留出空间;
  4. 左右栏使用 负 margin 移动到中间栏两侧;
  5. 不需要使用 position: relative
http://www.dtcms.com/a/278839.html

相关文章:

  • 精密模具冷却孔内轮廓检测方法探究 —— 激光频率梳 3D 轮廓检测
  • Redis单线程详解
  • H2 与高斯数据库兼容性解决方案:虚拟表与类型处理
  • Ai问答之空间站星等
  • MMKV 存储json list数据(kotlin)
  • Spring Boot 设置滚动日志logback
  • RocketMq部署模式简介
  • 高斯代数基本定理的一种证明
  • 【论文阅读】Thinkless: LLM Learns When to Think
  • Foundry 私钥管理指南:方法与安全最佳实践
  • 《大数据技术原理与应用》实验报告一 熟悉常用的Linux操作和Hadoop操作
  • PHP password_hash() 函数
  • Fiddler——抓取https接口配置
  • 【解决办法】越疆Dobot CR5 桌面客户端DobotStudio Pro连不上机器人
  • 在Ubuntu系统下使用mpstat工具监控CPU性能
  • 深地之下的智慧触角:Deepoc具身智能如何为矿业机器人铸就“感知之核”
  • CSS3 粘性定位解析:position sticky
  • Go从入门到精通(23) - 一个简单web项目-使用数据库存储数据
  • 解决chrome v2 版本插件不支持
  • 上下文管理器 和 contextlib 模块
  • [硬件电路-22]: 为什么模拟电路信号处理运算的精度不如数字信号处理运算?
  • 《Llava:Visual Instruction Tuning》论文精读笔记
  • 基于Chinese-CLIP与ChromaDB的中文图像检索功能实现
  • 人工智能如何重构能源系统以应对气候变化?
  • 动态规划题解——单词拆分【LeetCode】
  • openEuler系统PCIE降速方法简介
  • 【2025/07/14】GitHub 今日热门项目
  • Self - RAG工作步骤
  • 【HTML】五子棋(精美版)
  • 【Java EE】多线程-初阶 认识线程(Thread)