为什么 justify-self: flex-start 没有将 div 移动到列的底部?

huangapple go评论60阅读模式
英文:

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 :

&lt;div class=&quot;news-content-block&quot;&gt;
    &lt;div class=&quot;news-title&quot;&gt;
        &lt;a href=&quot;/link&quot;&gt;
            {{ $news-&gt;title }}
        &lt;/a&gt;
    &lt;/div&gt;

    &lt;div class=&quot;news-content-wrapper&quot;&gt;
        &lt;div class=&quot;news-content&quot;&gt; TEXT OF different length
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;user-reactions-block-wrapper&quot;&gt;
        FOOTER CONTENT
    &lt;/div&gt;
&lt;/div&gt;

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 &#39;start&#39;. Note that justify-self is ignored in Flexbox layouts. */
justify-self: flex-end; /* Equivalent to &#39;end&#39;. 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 -->

&lt;div class=&quot;news-content-block&quot;&gt;
    &lt;div class=&quot;news-title&quot;&gt;
        &lt;a href=&quot;/link&quot;&gt;
            {{ $news-&gt;title }}
        &lt;/a&gt;
    &lt;/div&gt;

    &lt;div class=&quot;news-content-wrapper&quot;&gt;
        &lt;div class=&quot;news-content&quot;&gt; TEXT OF different length
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;user-reactions-block-wrapper&quot;&gt;
        FOOTER CONTENT
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 13:16:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定