【问题标题】:Centering multiple text with flexbox使用 flexbox 使多个文本居中
【发布时间】:2023-03-23 02:03:01
【问题描述】:

如何使用 flexbox 将多个文本居中居中。 Margin: auto;text align: right; 不起作用。

这是我的代码: http://jsfiddle.net/L5mgy1sj/28/

我需要very long text 2very long text 3 坐在中间,可能有5px; 的边距将它们分开;

全视图: http://jsfiddle.net/L5mgy1sj/28/show

提前致谢。

【问题讨论】:

    标签: html css responsive-design flexbox


    【解决方案1】:

    像这样使用margin:auto

    .flex-container {
      display: flex;
      flex-wrap: wrap;
    }
    
    .flex-item:nth-child(2) {
      margin-left: auto;
      margin-right: 5px;
    }
    
    .flex-item:nth-child(3) {
      margin-right: auto;
      margin-left: 5px;
    }
    
    .flex-item {
      line-height: 50px;
      color: white;
      background: pink;
      font-weight: bold;
      font-size: 2em;
      text-align: center;
    }
    
    @media only screen and (max-width: 400px) {
      .flex-container {
        justify-content: center;
      }
      .flex-item:nth-child(n) {
        margin: 5px;
      }
    }
    <div class="flex-container">
      <div class="flex-item"> text 1</div>
      <div class="flex-item"> text 2</div>
      <div class="flex-item"> text 3</div>
      <div class="flex-item"> text 4</div>
    </div>

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2018-02-26
      • 1970-01-01
      • 2018-07-02
      • 2017-03-26
      • 1970-01-01
      • 2021-02-18
      • 2021-09-04
      相关资源
      最近更新 更多