英文:
Why justify-self: flex-start did not move div to bottom of a column?
问题
我需要将div显示为3级列:标题、内容和页脚,内容可以具有不同的高度,页脚必须位于底部。我这样做:
<div class="news-content-block">
<div class="news-title">
<a href="/link">
{{ $news->title }}
</a>
</div>
<div class="news-content-wrapper">
<div class="news-content">TEXT OF different length</div>
</div>
<div class="user-reactions-block-wrapper">
FOOTER CONTENT
</div>
</div>
并定义了以下样式:
.news-content-block {
display: flex;
flex: 1;
flex-direction: column;
justify-content: start;
padding: 20px;
}
.news-title {
position: relative;
width: 814px;
height: 24px;
}
.user-reactions-block-wrapper {
display: flex;
justify-self: flex-end;
align-self: flex-start;
}
在"user-reactions-block-wrapper"类中,我设置了:
display: flex;
justify-self: flex-end;
我希望看到这个类位于底部,但这并没有发生...
在网上搜索时,我找到了页面:
https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self
其中包含以下内容:
justify-self: flex-start; /* Equivalent to 'start'. Note that justify-self is ignored in Flexbox layouts. */
justify-self: flex-end; /* Equivalent to 'end'. Note that justify-self is ignored in Flexbox layouts. */
如何实现这一目标?
英文:
I need to showdiv as column with 3 levels : header,content, footer
content can be with different height and footer must be at bottom. i do it as :
<div class="news-content-block">
<div class="news-title">
<a href="/link">
{{ $news->title }}
</a>
</div>
<div class="news-content-wrapper">
<div class="news-content"> TEXT OF different length
</div>
</div>
<div class="user-reactions-block-wrapper">
FOOTER CONTENT
</div>
</div>
and styles defined :
.news-content-block{
display: flex;
flex: 1;
flex-direction: column;
justify-content: start;
padding: 20px;
}
.news-title {
position: relative;
width: 814px;
height: 24px;
}
.news-content-wrapper {
}
.user-reactions-block-wrapper {
display: flex;
justify-self: flex-end;
align-self:flex-start;
}
As in class user-reactions-block-wrapper I set
display: flex;
justify-self: flex-end;
I expected to see this class at bottom, but that did not happen...
Searching in net I found page :
https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self
with :
justify-self: flex-start; /* Equivalent to 'start'. Note that justify-self is ignored in Flexbox layouts. */
justify-self: flex-end; /* Equivalent to 'end'. Note that justify-self is ignored in Flexbox layouts. */
How that can be done ?
答案1
得分: 2
首先,您需要为您的 news-content-block
元素指定一个 height
,否则高度将根据其子元素的内容自动调整。然后,您需要在 user-reactions-block-wrapper
上添加 flex:1
,这将设置元素的高度以获取剩余空间的全部高度。通过这样做并添加 align-items: flex-end
,它将使其内容与底部对齐。希望这有所帮助。
.news-content-block{
display: flex;
flex: 1;
flex-direction: column;
justify-content: start;
padding: 20px;
height: 100vh;
border: 1px solid black; /* 用于参考点的添加 */
}
.news-title {
position: relative;
width: 814px;
height: 24px;
}
.user-reactions-block-wrapper {
display: flex;
flex: 1;
align-items: flex-end;
}
<div class="news-content-block">
<div class="news-title">
<a href="/link">
{{ $news->title }}
</a>
</div>
<div class="news-content-wrapper">
<div class="news-content"> TEXT OF different length
</div>
</div>
<div class="user-reactions-block-wrapper">
FOOTER CONTENT
</div>
</div>
英文:
First you need to specify a height
for your news-content-block
element or else the height will be auto based on the content of its children elements. Then you need to add flex:1
on your user-reactions-block-wrapper
which this will set the height of the element to get the full height of the remaining space. By doing this and adding align-items: flex-end
it will align its content to the bottom. Hope that helps.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.news-content-block{
display: flex;
flex: 1;
flex-direction: column;
justify-content: start;
padding: 20px;
height: 100vh;
border: 1px solid black; /* added for reference points */
}
.news-title {
position: relative;
width: 814px;
height: 24px;
}
.user-reactions-block-wrapper {
display: flex;
flex: 1;
align-items: flex-end;
}
<!-- language: lang-html -->
<div class="news-content-block">
<div class="news-title">
<a href="/link">
{{ $news->title }}
</a>
</div>
<div class="news-content-wrapper">
<div class="news-content"> TEXT OF different length
</div>
</div>
<div class="user-reactions-block-wrapper">
FOOTER CONTENT
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论