帝王谷资源网 Design By www.wdxyy.com

绝对定位方法:

(1)将父元素设置为相对定位,不写父元素的高度时,会随着左边的子元素高度变化而变化

.parent {
	/*关键代码*/
	position: relative;
	
	/*其他样式*/
	width: 800px;
	color: #fff;
	font-family: "Microsoft Yahei";
	text-align: center;
}

(2)左边一个元素有个最小高度的情况

.left {
	min-height: 700px;
	width: 600px;
}

(3)右边元素要想跟父元素的高度是一致,那么可以用绝对定位这样设置,如果不想同时写top和bottom,写一个时,再写上height:100%,也可以达到一样的效果

.right {
	/*关键代码*/
	width: 200px;
	position: absolute;
	top: 0;
	right: 0;
	bottom: 0;
	
	/*其他样式*/
	background: #ccc;
	
}

(4)完整例子代码:

<!DOCTYPE html>
<html >
<head>
	<meta charset="UTF-8">
	<title>子元素高度与父元素一致</title>
	<style>

		.parent{
			position: relative;
			background: #f89;

			width: 800px;
			color: #fff;
			font-family: "Microsoft Yahei";
			text-align: center;
		}
		.left {
			min-height: 700px;
			width: 600px;

		}
		.right {
			width: 200px;
			position: absolute;
			top: 0;
			right: 0;
			bottom: 0;

			background: #ccc;
			
		}
	</style>
</head>
<body>
	<div class="parent">
		<div class="left">
			左侧 left 不定高,parent的高度随着左侧left 的高度变化而变化,右侧也跟着变
		</div>
		<div class="right">
			这边的高度跟父元素高度一致
		</div>
	</div>
</body>
</html>

(5)效果

CSS子元素跟父元素的高度一致的实现方法

(6)问题来了:
 

如果右侧的子元素高度超出了.parent,怎么办?

.right-inner {
	background: limegreen;
	height: 1024px;
}
<div class="right">
	<div class="right-inner">right的子元素,高度为1024px,会撑破容器,给.right加上 overflow:auto 就防止溢出了</div>
</div>

效果图如下:

CSS子元素跟父元素的高度一致的实现方法

完整代码:

<!DOCTYPE html>
<html >
<head>
	<meta charset="UTF-8">
	<title>子元素高度与父元素一致</title>
	<style>

		.parent{
			position: relative;
			background: #f89;

			width: 800px;
			color: #fff;
			font-family: "Microsoft Yahei";
			text-align: center;
		}
		.left {
			min-height: 700px;
			width: 600px;

		}
		.right {
			width: 200px;
			position: absolute;
			top: 0;
			right: 0;
			height: 100%;

			overflow: auto;

			background: #ccc;
			
		}
		.right-inner {
			background: limegreen;
			height: 1024px;
		}
	</style>
</head>
<body>
	<div class="parent">
		<div class="left">
			左侧 left 不定高,parent的高度随着左侧left 的高度变化而变化,右侧也跟着变
		</div>
		<div class="right">
			<div class="right-inner">right的子元素,高度为1024px,会撑破容器,给.right加上 overflow:auto 就防止溢出了</div>
		</div>
	</div>
</body>
</html>

(7)其他资源

http://stackoverflow.com/questions/3049783/how-to-make-a-floated-div-100-height-of-its-parent

标签:
CSS,子元素,父元素,高度

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。