[英]positioning div to bottom of parent div
Hello I hope someone can help me with this rather, I guess, simple question. 您好,我希望有人可以帮助我解决这个相当简单的问题。
Why is the div
with content €1.230 not aligned at the bottom of the chart-p
div? 为什么内容为€1.230的div
不在chart-p
div的底部对齐?
The desired behaviour is that the whole chart-r
is positioned to the bottom. 期望的行为是整个chart-r
都位于底部。 So that the white space is above the div
instead of below. 因此,空格位于div
上方而不是下方。
.chart-p { width: 300px; display: block; height: 236px; position: relative; background-color: lightblue; } .chart-r { margin-right: 1.5%; background-color: #E5F1F9; } .chart-r, .chart-z { display: inline-block; width: 48%; background-color: #FEE9A9; position: relative; top: 0px; left: 0; }
<div class="chart-p"> <div class="chart-r" style="height:115px;"> € 1.230</div> <div class="chart-z" style="height:236px;"> € 2.280</div> </div>
add this to your code 将此添加到您的代码
.chart-r {vertical-align:bottom;}
Working example : 工作示例:
.chart-p { width: 300px; display: block; height: 236px; position: relative; background-color: lightblue; } .chart-r { margin-right: 1.5%; background-color: #E5F1F9; vertical-align:bottom; } .chart-r, .chart-z { display: inline-block; width: 48%; background-color: #FEE9A9; position: relative; top: 0px; left: 0; }
<div class="chart-p"> <div class="chart-r" style="height:115px;"> € 1.230</div> <div class="chart-z" style="height:236px;"> € 2.280</div> </div>
.chart-r
isn't at the bottom of .chart-p
because there is nothing pushing it down. .chart-r
不在.chart-p
的底部,因为没有什么可以压低它的。
You can either add margin-top
to .chart-r
which is probably the solution you need. 您可以在.chart-r
添加margin-top
,这可能是您需要的解决方案。 Or you could add display: flex
and flex-direction: row
to the parent element and align-self: flex-end
to the child element you want to have on the bottom of the graph, like this: 或者,您可以将display: flex
和flex-direction: row
到父元素,并将align-self: flex-end
到要在图形底部显示的子元素,如下所示:
.chart-p { width: 300px; height: 236px; /* Flexbox */ display: flex; flex-direction: row; position: relative; background-color: lightblue; } .chart-r { margin-right: 1.5%; background-color: #E5F1F9; /* Align at end of flex */ align-self: flex-end; } .chart-r, .chart-z { display: inline-block; width: 48%; background-color: #FEE9A9; position: relative; top: 0px; left: 0; }
<div class="chart-p"> <div class="chart-r" style="height:115px;"> 1.230</div> <div class="chart-z" style="height:236px;"> 2.280</div> </div>
Hope this helps! 希望这可以帮助!
That's standard behaviour. 这是标准行为。 You can use a simple vertical-align:bottom;
您可以使用简单的vertical-align:bottom;
on your chart-r element if you want it at the bottom 如果要在图表r元素的底部
Use position:absolute
instead of relative. 使用position:absolute
而不是relative。
.chart-p { width: 300px; display: block; height: 236px; position: relative; background-color: lightblue; } .chart-r { margin-right: 1.5%; background-color: #E5F1F9; } .chart-r, .chart-z { display: inline-block; width: 48%; background-color: #FEE9A9; position: absolute; bottom: 0px; left: 0; } .chart-z{ left: 50%; }
<div class="chart-p"> <div class="chart-r" style="height:115px;"> € 1.230</div> <div class="chart-z" style="height:236px;"> € 2.280</div> </div>
<div class="chart-p">
<div class="chart-r" style="height:115px;">
1.230</div>
<div class="chart-z" style="height:236px;">
2.280</div>
</div>
<style>
.chart-p {
width: 300px;
height: 236px;
display: flex;
flex-direction: row;
align-items:flex-end;
position: relative;
background-color: lightblue;
}
.chart-r {
margin-right: 1.5%;
background-color: #E5F1F9;
}
.chart-r, .chart-z {
display: inline-block;
width: 48%;
background-color: #FEE9A9;
position: relative;
top: 0px;
left: 0;
}</style>
Add align-items:flex-end to the main div to solve problem. 在主div上添加align-items:flex-end以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.