【问题标题】:Flexbox Layout - equal heights and fittextFlexbox 布局 - 等高和 fittext
【发布时间】:2018-05-31 18:33:07
【问题描述】:

我正在尝试使用 flexbox 构建一个基本的用户界面,到目前为止我有这个..

  body,html {
  margin:0;
  padding:0;
  height:100%;
  }
  
  .container {
    height:100%;
    display:flex;
    flex-direction:column;
  }

  .top {
    flex:4;
    display:flex;
    background:wheat;
    align-items: center;
    justify-content: center;
  }

  .bottom {
    flex:1;
    background:teal;
  }

  .bottom_content {
    display:flex;
    flex-direction:column;
  }

  .section1 {
    flex:1;
    font-size:20px;
    text-align:center;
  }

  .section2 {
    flex:1;
  }
  
  .btn {
    background:red;
    color:white;
    padding:20px;
  }
<div class="container">

  <div class="top">
    <span>TOP CONTENT</span>
  </div>
  
  <div class="bottom">
    
    <div class="bottom_content">
      
      <div class="section1">
        <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacus quam, blandit non lacus in, venenatis tempor dolor. Aliquam in lectus lacus. </span>
      </div>
      
      <div class="section2">
        <div class="btn">
          THIS IS A BUTTON
        </div>
      </div>
    
    </div>
  
  </div>

  
</div>

我正在努力实现这一目标......

如何使底部的高度相等,并使其中的内容垂直和水平居中?

我还计划使用 fittext.js 或类似的东西来使按钮和上面的文本适合弹性项目。

我哪里错了?

【问题讨论】:

    标签: html css flexbox fittextjs


    【解决方案1】:

    问题

    您当前代码的问题是.bottom 没有填充可用空间,并且正在使用默认对齐和对齐方式。

    修复

    可以通过执行以下操作来实现所需的输出:

    • .section1.section2 中删除flex:1;。这会阻止这些 divs 扩展以填充可用空间
    • align-items: center;justify-content: space-evenly; 添加到.bottom_content。这将使.section1.section2 居中对齐并均匀隔开divs
    • display: flex; 添加到.bottom。这将使.bottom 扩展以适应可用空间
    • .bottom 上将flex: 1; 更改为flex: 1 0 auto;。当窗口高度较小时,这将阻止 .bottom 缩小尺寸

    body,
    html {
      margin: 0;
      padding: 0;
      height: 100%;
    }
    
    .container {
      height: 100%;
      display: flex;
      flex-direction: column;
    }
    
    .top {
      flex: 4;
      display: flex;
      background: wheat;
      align-items: center;
      justify-content: center;
    }
    
    .bottom {
      /*Change*/
      flex: 1 0 auto;
      background: teal;
      /*Add*/
      display: flex;
    }
    
    .bottom_content {
      display: flex;
      flex-direction: column;
      /*Add*/
      align-items: center;
      justify-content: space-evenly;
    }
    
    .section1 {
      /*Remove*/
      /*flex:1;*/
      font-size: 20px;
      text-align: center;
    }
    
    .section2 {
      /*Remove*/
      /*flex:1;*/
    }
    
    .btn {
      background: red;
      color: white;
      padding: 20px;
    }
    <div class="container">
      <div class="top">
        <span>TOP CONTENT</span>
      </div>
      <div class="bottom">
        <div class="bottom_content">
          <div class="section1">
            <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacus quam, blandit non lacus in, venenatis tempor dolor. Aliquam in lectus lacus. </span>
          </div>
          <div class="section2">
            <div class="btn">
              THIS IS A BUTTON
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 谢谢你,很好地修复了这个布局!当我缩小到更小的屏幕尺寸时,flexbox 项目的高度开始改变。有没有办法可以锁定尺寸来防止这种情况发生?
    • 没问题。您特别希望限制哪些高度?
    • 我希望 section1 和 section2 保持高度比,即;总是 50% 的高度。然后我打算使用 fittext.js 之类的东西来调整文本大小以适应
    • 随着屏幕宽度变小,底部框的高度会增加。我正在努力让他们保持身高
    • @fightstarr20 我不确定我是否理解您的要求,容器将展开以确保文本适合其中。你在寻找这样的东西吗? jsfiddle.net/h4ts2qrt/1
    【解决方案2】:

    我已经评论了你的一些代码。请检查

    body,
    html {
      margin: 0;
      padding: 0;
      height: 100%;
    }
    
    .container {
      height: 100%;
      display: flex;
      flex-direction: column;
    }
    
    .top {
      flex: 4;
      display: flex;
      background: wheat;
      align-items: center;
      justify-content: center;
    }
    
    .bottom {
      flex: 1;
      background: teal;
    }
    
    .bottom_content {
      display: flex;
      flex-direction: column;
    }
    
    .section1 {
      <!-- flex: 1;
      -->font-size: 20px;
      text-align: center;
    }
    
    .section2 {
      flex: 1;
    }
    
    .btn {
      background: red;
      color: white;
      padding: 20px;
      margin-top: 25px;
    }
    <div class="container">
    
      <div class="top">
        <span>TOP CONTENT</span>
      </div>
    
      <div class="bottom">
    
        <div class="bottom_content">
    
          <div class="section1">
            <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacus quam, blandit non lacus in, venenatis tempor dolor. Aliquam in lectus lacus. </div>
            <button class="btn">
              THIS IS A BUTTON
            </button>
          </div>
    
          <!-- <div class="section2"> -->
          <!-- <div class="btn"> -->
          <!-- THIS IS A BUTTON -->
          <!-- </div> -->
          <!-- </div> -->
    
        </div>
    
      </div>
    
    
    </div>

    【讨论】:

      【解决方案3】:

      justify-content:centerdisplay:flex 是关键。

      您可以根据需要调整边距,除了它应该可以解决问题。

      .bottom_content {
         display: flex;
         flex-direction: column;
         margin-top: 20px;
      }
      
      .section2 {
         flex: 1;
         display: flex;
         justify-content: center;
      }
      
      .btn {
         background: red;
         color: white;
         padding: 20px;
         width: fit-content;
         position: absolute;
         margin-top: 10px;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-22
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        • 1970-01-01
        • 2017-05-31
        • 1970-01-01
        相关资源
        最近更新 更多