【发布时间】:2018-08-11 21:58:54
【问题描述】:
在经典布局中(页眉固定,主要和页脚固定),我想将主要元素的文本居中。出于本练习的目的,我想设置 line-height 使其等于主元素的高度,然后文本将垂直居中。绝对定位的主元素具有 10% 的顶部和底部填充,因此高 80%。
如何让 line-height 等于容器高度?
* { box-sizing: border-box; }
html { height: 100%; }
body { margin: 0;
font-size: 10px; }
header { border: 1px solid black;
position: fixed;
top: 0;
height: 10%;
left: 0;
right: 0;
}
main { border: 1px solid black; left: 0; right: 0;
position: absolute;
top: 10%;
/* height: 80%; */
bottom: 10%;
line-height: 80%;
}
footer { border: 1px solid black;
position: fixed;
height: 10%;
left: 0;
right: 0;
bottom: 0;
}
<header>Header</header>
<main><div>Main Div</div></main>
<footer>Footer</footer>
【问题讨论】: