在可变高度的父项中垂直对齐 div
Vertically align div in a parent of variable height
我有一个包装器,它的高度是可变的(取决于左边框中的内容)。我希望此包装内的 button
在右侧框内垂直对齐。
我无法让它工作,虽然我已经尝试了很多东西,包括 css-tricks' post on centering,我已将其放在评论下。我也尝试修改 flexbox
,但没有成功。
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
}
.left {
float: left;
width: 70%;
}
.right {
position: relative;
overflow: hidden; /* to induce block formatting context */
}
.button {
width: 3em;
border: 1px solid #999;
margin: 0 auto;
/*
// The below didn't work! the `button` vanishes.
position: absolute;
top: 50%;
transform: translateY(-50%);
*/
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div>
<div class="right">
<div class="button">button</div>
</div>
</div>
您可以使用 display:inline-block;
而不是 float left;
并且不要忘记 remove extra spaces 这是由 display:inline-block;
发生的。
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
}
.left,
.right
{
display:inline-block;
vertical-align:middle;
}
.left {
width: 70%;
}
.right {
position: relative;
overflow: hidden; /* to induce block formatting context */
text-align:center;
width:30%;
}
.button {
width: 3em;
border: 1px solid #999;
margin: 0 auto;
/*
// The below didn't work! the `button` vanishes.
position: absolute;
top: 50%;
transform: translateY(-50%);
*/
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div><div class="right">
<div class="button">button</div>
</div>
</div>
这里的主要问题是您需要左右 div 自动具有相同的高度。除了 javascript,只有 flexbox 和 css 表允许这样做。所以...
Flexbox可以做到这一点。
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
display: flex;
}
.left {
flex: 0 0 70%;
}
.right {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
}
.button {
width: 3em;
border: 1px solid #999;
text-align: center;
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div>
<div class="right">
<div class="button">button</div>
</div>
</div>
或...CSS 表
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
display: table;
table-layout: fixed;
}
.left {
display: table-cell;
width: 70%;
}
.right {
display: table-cell;
width: 30%;
vertical-align: middle;
text-align: center;
}
.button {
width: 3em;
border: 1px solid #999;
margin: auto;
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div>
<div class="right">
<div class="button">button</div>
</div>
</div>
如果您愿意使用 calc
:
.button {
width: 3em;
border: 1px solid #999;
margin: calc(50% - 0.5em) 0 calc(50% - 0.5em) 0;
不过有一个问题:calc 不接受其他参数,例如按钮的高度;所以我假设它是 1em(因为你没有定义高度)。
看看这个风格的锻炼,
.wrapper{
width: 20em;
margin: 1em;
border: 1px solid #999;
}
.left{
float: left;
width: 15em;
}
.right{
width: 5em;
float: right;
}
.button {
width: 3em;
border: 1px solid #999;
margin: 1em;
}
我有一个包装器,它的高度是可变的(取决于左边框中的内容)。我希望此包装内的 button
在右侧框内垂直对齐。
我无法让它工作,虽然我已经尝试了很多东西,包括 css-tricks' post on centering,我已将其放在评论下。我也尝试修改 flexbox
,但没有成功。
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
}
.left {
float: left;
width: 70%;
}
.right {
position: relative;
overflow: hidden; /* to induce block formatting context */
}
.button {
width: 3em;
border: 1px solid #999;
margin: 0 auto;
/*
// The below didn't work! the `button` vanishes.
position: absolute;
top: 50%;
transform: translateY(-50%);
*/
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div>
<div class="right">
<div class="button">button</div>
</div>
</div>
您可以使用 display:inline-block;
而不是 float left;
并且不要忘记 remove extra spaces 这是由 display:inline-block;
发生的。
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
}
.left,
.right
{
display:inline-block;
vertical-align:middle;
}
.left {
width: 70%;
}
.right {
position: relative;
overflow: hidden; /* to induce block formatting context */
text-align:center;
width:30%;
}
.button {
width: 3em;
border: 1px solid #999;
margin: 0 auto;
/*
// The below didn't work! the `button` vanishes.
position: absolute;
top: 50%;
transform: translateY(-50%);
*/
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div><div class="right">
<div class="button">button</div>
</div>
</div>
这里的主要问题是您需要左右 div 自动具有相同的高度。除了 javascript,只有 flexbox 和 css 表允许这样做。所以...
Flexbox可以做到这一点。
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
display: flex;
}
.left {
flex: 0 0 70%;
}
.right {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
}
.button {
width: 3em;
border: 1px solid #999;
text-align: center;
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div>
<div class="right">
<div class="button">button</div>
</div>
</div>
或...CSS 表
.wrapper {
width: 20em;
margin: 1em;
border: 1px solid #999;
overflow: hidden;
display: table;
table-layout: fixed;
}
.left {
display: table-cell;
width: 70%;
}
.right {
display: table-cell;
width: 30%;
vertical-align: middle;
text-align: center;
}
.button {
width: 3em;
border: 1px solid #999;
margin: auto;
}
<div class="wrapper">
<div class="left">Every person is worth just as much as the things they busy themselves with.</div>
<div class="right">
<div class="button">button</div>
</div>
</div>
如果您愿意使用 calc
:
.button {
width: 3em;
border: 1px solid #999;
margin: calc(50% - 0.5em) 0 calc(50% - 0.5em) 0;
不过有一个问题:calc 不接受其他参数,例如按钮的高度;所以我假设它是 1em(因为你没有定义高度)。
看看这个风格的锻炼,
.wrapper{
width: 20em;
margin: 1em;
border: 1px solid #999;
}
.left{
float: left;
width: 15em;
}
.right{
width: 5em;
float: right;
}
.button {
width: 3em;
border: 1px solid #999;
margin: 1em;
}