CSS等高布局的7种方式
前面的话
等高布局是指子元素在父元素中高度相等的布局方式。等高布局的实现包括伪等高和真等高,伪等高只是看上去等高而已,真等高是实实在在的等高。本文将介绍边框模拟、负margin这两种伪等高以及table实现、absolute实现、flex实现、grid实现和js判断这五种真等高布局
伪等高
边框模拟
因为元素边框和元素高度始终是相同高度,用元素的边框颜色来伪装左右两个兄弟元素的背景色。然后将左右两个透明背景的元素使用absolute覆盖在中间元素的左右边框上,实现视觉上的等高效果
[注意]左右两侧元素的内容高度不能大于中间元素内容高度,否则无法撑开容器高度
<style>
body,p{margin: 0;}
.parent{
position: relative;
}
.center{
box-sizing:border-box;
padding: 0 20px;
background-clip: content-box;
border-left: 210px solid lightblue;
border-right: 310px solid lightgreen;
}
.left{
position: absolute;
top: 0;
left: 0;
width: 200px;
}
.right{
position: absolute;
top: 0;
right: 0;
width: 300px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
<div class="left">
<p>left</p>
</div>
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
<div class="right">
<p>right</p>
</div>
</div>
负margin
因为背景是在padding区域显示的,设置一个大数值的padding-bottom,再设置相同数值的负的margin-bottom,使背景色铺满元素区域,又符合元素的盒模型的计算公式,实现视觉上的等高效果
[注意]如果页面中使用<a>锚点跳转时,将会隐藏部分文字信息
[注意]如果页面中的背景图片定位到底部,将会看不到背景图片
<style>
body,p{margin: 0;}
.parent{
overflow: hidden;
}
.left,.centerWrap,.right{
float: left;
width: 50%;
padding-bottom: 9999px;
margin-bottom: -9999px;
}
.center{
margin: 0 20px;
}
.left,.right{
width: 25%;
}
</style>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="centerWrap">
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
</div> <div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
真等高
table
table元素中的table-cell元素默认就是等高的
<style>
body,p{margin: 0;}
.parent{
display: table;
width: 100%;
table-layout: fixed;
}
.left,.centerWrap,.right{
display: table-cell;
}
.center{
margin: 0 20px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="centerWrap">
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
absolute
设置子元素的top:0;bottom:0;使得所有子元素的高度都和父元素的高度相同,实现等高效果
<style>
body,p{margin: 0;}
.parent{
position: relative;
height: 40px;
}
.left,.center,.right{
position: absolute;
top: 0;
bottom: 0;
}
.left{
left: 0;
width: 100px;
}
.center{
left: 120px;
right: 120px;
}
.right{
width: 100px;
right: 0;
}
</style>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
flex
flex中的伸缩项目默认都拉伸为父元素的高度,也实现了等高效果
<style>
body,p{margin: 0;}
.parent{
display: flex;
}
.left,.center,.right{
flex: 1;
}
.center{
margin: 0 20px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
grid
<style>
body,p{margin: 0;}
.parent{
display: grid;
grid-auto-flow: column;
grid-gap:20px;
}
</style>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
js
当子元素高度不同时,进行js判断,增加较低子元素的padding-bottom,使得各个子元素实现等高效果
<style>
body,p{margin: 0;}
.parent{overflow: hidden;}
.left,.center,.right{
float: left;
width: 25%;
}
.center{
width: 50%;
padding: 0 20px;
background-clip: content-box;
box-sizing: border-box;
}
</style>
<div class="parent" id="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
<script>
function getCSS(obj,style){
if(window.getComputedStyle){
return getComputedStyle(obj)[style];
}
return obj.currentStyle[style];
}
var oParent = document.getElementById('parent');
var oLeft = oParent.getElementsByTagName('div')[0];
var oCenter = oParent.getElementsByTagName('div')[1];
var oRight = oParent.getElementsByTagName('div')[2];
function eqHeight(obj1,obj2){
var oDis = obj1.clientHeight - obj2.clientHeight;
if(oDis > 0){
obj2.style.paddingBottom = parseFloat(getCSS(obj2,'padding-bottom')) + oDis + 'px';
}else{
obj1.style.paddingBottom = parseFloat(getCSS(obj1,'padding-bottom')) + Math.abs(oDis) + 'px';
}
}
eqHeight(oLeft,oCenter);
eqHeight(oLeft,oRight);
</script>
CSS等高布局的7种方式的更多相关文章
- CSS等高布局的6种方式
× 目录 [1]边框模拟 [2]负margin [3]table[4]absolute[5]flex[6]js 前面的话 等高布局是指子元素在父元素中高度相等的布局方式.等高布局的实现包括伪等高和真等 ...
- CSS全屏布局的5种方式
× 目录 [1]float [2]inline-block [3]table[4]absolute[5]flex[6]总结 前面的话 全屏布局在实际工作中是很常用的,比如管理系统.监控平台等.本文将介 ...
- CSS全屏布局的6种方式
前面的话 全屏布局在实际工作中是很常用的,比如管理系统.监控平台等.本文将介绍关于全屏布局的6种思路 float [1]float + calc 通过calc()函数计算出.middle元素的高度,并 ...
- 实现CSS等分布局的4种方式
× 目录 [1]float [2]inline-block [3]table[4]flex 前面的话 等分布局是指子元素平均分配父元素宽度的布局方式,本文将介绍实现等分布局的4种方式 思路一: flo ...
- 实现CSS等分布局的5种方式
前面的话 等分布局是指子元素平均分配父元素宽度的布局方式,本文将介绍实现等分布局的5种方式 float [思路一]float 缺点:结构和样式存在耦合性,IE7-浏览器下对宽度百分比取值存在四舍五入的 ...
- CSS实现水平垂直居中的1010种方式
转载自:CSS实现水平垂直居中的1010种方式 划重点,这是一道面试必考题,很多面试官都喜欢问这个问题,我就被问过好几次了 要实现上图的效果看似很简单,实则暗藏玄机,本文总结了一下CSS实现水平垂直居 ...
- 【css】清除浮动的几种方式
[css]清除浮动的几种方式 因为浮动框不在普通的文档流中,所以它不占据空间.如下面的代码: .news { background-color:gray; border:1px solid bla ...
- web网页 页面布局的几种方式(转)
web网页 页面布局的几种方式 转载 2017年06月16日 12:19:40 2218 网页基本布局方式: (1)流式布局 Fluid 流布局与固定宽度布局基本不同点 就在于对网站尺寸的侧量单位不同 ...
- CSS三栏布局的四种方法
总括: 不管是三栏布局还是两栏布局都是我们在平时项目里经常使用的,也许你不知道什么事三栏布局什么是两栏布局但实际已经在用,或许你知道三栏布局的一种或两种方法,但实际操作中也只会依赖那某一种方法,本文具 ...
随机推荐
- Luogu P3366 【模板】最小生成树
qwq #include<cstdio> #include<algorithm> using namespace std; ]; int n,m; struct abc { i ...
- Saltstack管理对象属性之grains和pillar组件
Grains组件 Grains是saltstack记录minion的一些静态信息组件,可以简单的理解为grains里面记录着每台minion的一些常用的属性,比如cpu.内存.磁盘.网络信息等,可以通 ...
- 在 Virtual Box 安装 Mac Os 并安装 Qt 开发应用
导读 由于 Beslyric-for-X 项目开发需要,开始尝试在 Mac Os 下开发 Qt 应用.尝试成功后,记录于此,希望对有类似需求的人有所帮助. 本文以开发 Beslyric-for-X 为 ...
- 如何扩展32位EXE程序的使用内存
1 运行Visual studio的命令行,执行下面命令:editbin /LARGEADDRESSAWARE “C:\Program Files\Skyline\TerraExplorer Pro\ ...
- 8-51单片机ESP8266学习-AT指令(单片机采集温湿度数据通过8266发送给C#TCP客户端显示)
http://www.cnblogs.com/yangfengwu/p/8785516.html 先写单片机端的程序 先把源码和资料链接放到这里 链接: https://pan.baidu.com/s ...
- mvn打包到私服的命令
1.mvn clean package install -Dmaven.test.skip=true deploy 2.docker清楚Nexus私服上包的命令: a) docker exec -it ...
- LOJ6089 小Y的背包计数问题 背包、根号分治
题目传送门 题意:给出$N$表示背包容量,且会给出$N$种物品,第$i$个物品大小为$i$,数量也为$i$,求装满这个背包的方案数,对$23333333$取模.$N \leq 10^5$ $23333 ...
- C# 获取电脑MAC地址,IP地址,物理内存,CPU序列号,硬盘ID..........................
上班很忙,自己做个记录 代码如下: 需要引入:System.Management 代码如下: using System; using System.Collections.Generic; using ...
- Jq_DOM元素方法跟JQuery 核心函数跟JQuery 事件方法
JQuery DOM 元素 函数 描述 .get() 从队列中删除所有未运行的项目. .ind ...
- Linux mount 命令进阶
笔者在<Linux mount 命令>一文中介绍了 mount 命令的基本用法,本文我们接着介绍 mount 命令的一些高级用法,比如 bind mounts(绑定挂载)和 shared ...