【问题标题】:Right-justify single item in a centered, column-aligned flex container在居中、列对齐的 flex 容器中右对齐单个项目
【发布时间】:2020-12-20 21:57:32
【问题描述】:

我搜索并发现了关于在行对齐的 flex 上右对齐项目的讨论,但不是列对齐的。

我有几行文字。每条线水平居中,容器垂直放置。但是,有一行在页脚中,我想将其右对齐。

我已经尝试了我能想到的所有组合来右对齐页脚(align-self 的各种值,将页脚更改为使用 inline-block,更改页脚中的 justify-content 等),以及所有内容导致它被对齐。

设置了两个section元素中的CSS;我无法改变它们。我可以在footer 中为所欲为(在合理范围内)。

如何让页脚中的文本右对齐,即在窗口的右侧,即text-align: right 如果不在 flex 容器中会做什么?

<section>
  <p>Lorem ipsum</p>
  <p>Morbi at interdum odio</p>
  <p>Maecenas mi ex</p>
  <footer>
    <p>Nunc ultricies</p>
  </footer>
</section>
section{
  align-items: center;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: calc(70vh - 3em);
}

section > *{
  margin: 0;
}

footer {
  margin-top: 1em;
}

【问题讨论】:

    标签: css flexbox


    【解决方案1】:

    align-self:flex-end

    section{
      align-items: center;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      justify-content: center;
      min-height: calc(70vh - 3em);
    }
    
    section > *{
      margin: 0;
    }
    
    footer {
      margin-top: 1em;
      align-self:flex-end;
    }
    <section>
      <p>Lorem ipsum</p>
      <p>Morbi at interdum odio</p>
      <p>Maecenas mi ex</p>
      <footer>
        <p>Nunc ultricies</p>
      </footer>
    </section>

    text-align,如果您制作页脚width:100%

    section{
      align-items: center;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      justify-content: center;
      min-height: calc(70vh - 3em);
    }
    
    section > *{
      margin: 0;
    }
    
    footer {
      margin-top: 1em;
      text-align:right;
      width:100%;
    }
    <section>
      <p>Lorem ipsum</p>
      <p>Morbi at interdum odio</p>
      <p>Maecenas mi ex</p>
      <footer>
        <p>Nunc ultricies</p>
      </footer>
    </section>

    【讨论】:

    • 好悲痛。我在 justify 上尝试了 end、self-end 和 flex-end,但不知何故错过了 align-self 上的 flex-end,或者可能是在我更改了其他导致它无法工作的东西时这样做了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2023-01-26
    • 2021-10-29
    • 2022-01-24
    • 2019-01-16
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多