当前位置: 首页 > 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.xdnf.cn/news/1121347.html

相关文章:

  • 详解彩信 SMIL规范
  • Leaflet面试题及答案(81-100)
  • 代码随想录day34dp2
  • ARMv8.1原子操作指令(ll_sc/lse)
  • 苍穹外卖学习指南(java的一个项目)(老师能运行,但你不行,看这里!!)
  • python的微竞网咖管理系统
  • UI前端与数字孪生结合实践探索:智慧物流的仓储自动化管理系统
  • Java文件操作
  • Reactor 模式详解
  • 【Echarts】 电影票房汇总实时数据横向柱状图比图
  • ubuntu 22.04 anaconda comfyui安装
  • libimagequant windows 编译
  • 云手机常见问题解析:解决延迟、掉线等困扰
  • 机器学习中的朴素贝叶斯(Naive Bayes)模型
  • 新型eSIM攻击技术可克隆用户资料并劫持手机身份
  • Android 16系统源码_窗口动画(一)窗口过渡动画层级图分析
  • 在 Azure Linux 上安装 RustFS
  • 如何保护文件传输安全?文件传输加密
  • 实战:如何创建 AWS RDS 数据库
  • 从“有”到“优”:iPaaS 赋能企业 API 服务治理建设
  • Foundry 私钥管理指南:方法与安全最佳实践
  • 上下文管理器 和 contextlib 模块
  • 深入浅出Kafka Producer源码解析:架构设计与编码艺术
  • VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
  • mybatis-plus-jpa-support
  • 常用的OTP语音芯片有哪些?
  • Spring Boot启动原理:从main方法到内嵌Tomcat的全过程
  • Linux 系统下的 Sangfor VDI 客户端安装与登录完全攻略 (CentOS、Ubuntu、麒麟全线通用)
  • Git LFS 操作处理Github上传大文件操作记录
  • 第一章编辑器开发基础第一节绘制编辑器元素_4输入字段(4/7)