CSS 100% 从 div 而不是整个页面

标签 css height

我正在尝试使用 CSS 中的 calc 进行计算。几个小时后,我发现我的实际 div 给出了错误的 100% 数字。

这是一个小例子:

html, body, .calender {
	position: relative;
	height: 100%;
	overflow: hidden;
}

.appointmentsA {
	background: blue;
}

.appointmentsB {
	background: red;
}

.calender {
	display: table;
	width: 100%;
	table-layout: fixed;
	padding: 10px 10px 0 10px;
}
.calender .row {
	display: table-cell;
	padding: 0 10px;
}

.appointments {
	height: 100%;
}

.appointments:before {
			content: "test";
			position: absolute;
			top: calc(100% - 18px);
			color: #fff;
		}
<main class="calender">
	<div class="row">
		<h1 class="title">ABC</h1>
		<div class="appointments appointmentsA"></div>
	</div>
	<div class="row">
		<h1 class="title">DEF</h1>
		<div class="appointments appointmentsB"></div>
	</div>
</main>

红色和蓝色 block 的高度与容器 row 和主父级的高度相同。

是否有非 javascript 方法来获取彩色 block 的 100% 高度? 如果彩色 block 具有真正的 100%,则“:after”元素将正确显示,而不仅仅是半个文本。

图片:example

最佳答案

你的意思是为全高col设置颜色?如果是,设置 position: absolute 为 title:

* {
  box-sizing: border-box;
}

html,
body,
.calender {
  position: relative;
  height: 100%;
  overflow: hidden;
}

.appointmentsA {
  background: blue;
}

.appointmentsB {
  background: red;
}

.calender {
  display: table;
  width: 100%;
  table-layout: fixed;
  padding: 10px 10px 0 10px;
}

.calender .row {
  display: table-cell;
  padding: 0 10px;
}

.appointments {
  height: 100%;
}

.title {
  position: absolute;
}
<main class="calender">
  <div class="row">
    <h1 class="title">ABC</h1>
    <div class="appointments appointmentsA"></div>
  </div>
  <div class="row">
    <h1 class="title">DEF</h1>
    <div class="appointments appointmentsB"></div>
  </div>
</main>

或者对于彩色 div:

* {
  box-sizing: border-box;
}

html,
body,
.calender {
  position: relative;
  height: 100%;
  overflow: hidden;
}

.appointmentsA {
  background: blue;
}

.appointmentsB {
  background: red;
}

.calender {
  display: table;
  width: 100%;
  table-layout: fixed;
  padding: 10px 10px 0 10px;
}

.calender .row {
  display: table-cell;
  padding: 0 10px;
  position: relative;
}

.appointments {
  height: 100%;
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.title {
  position: relative;
  z-index: 2;
}
<main class="calender">
  <div class="row">
    <h1 class="title">ABC</h1>
    <div class="appointments appointmentsA"></div>
  </div>
  <div class="row">
    <h1 class="title">DEF</h1>
    <div class="appointments appointmentsB"></div>
  </div>
</main>

关于CSS 100% 从 div 而不是整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683403/

相关文章:

css - height:auto 无法正常工作,div 不会随着内容展开

ios - scrollview 具有不明确的可滚动内容高度和宽度

javascript - 根据滚动高度的 100% 修复标题

css - 创建带圆 Angular 的表格

html - html li 的 float 问题

javascript - 在鼠标悬停时淡出图片,然后淡入不同的图片

wpf - 如何在 WPF 中找到任意字形的精确大小?

html - 打印预览中不显示 CSS 剪辑路径 : why?

css - 在圆圈内裁剪和居中的背景图像

button - Flutter 更改 AppBar 内 OutlineButton 的高度?