Flexbox布局 - 相等的高度和fittext

时间:2018-05-31 09:56:41

标签: html css flexbox fittextjs

我正在尝试使用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>
&#13;
&#13;
&#13;

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

enter image description here

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

我还计划使用fittext.js或类似方法使按钮和上面的文字适合弹性项目。

我哪里错了?

3 个答案:

答案 0 :(得分:1)

问题

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

修复

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

  • flex:1;.section1移除.section2。这会阻止这些div扩展以填充可用空间
  • align-items: center;justify-content: space-evenly;添加到.bottom_content。这将使.section1.section2 div
  • 中心对齐并均匀分隔
  • display: flex;添加到.bottom。这将使.bottom展开以适应可用空间
  • flex: 1;更改为flex: 1 0 auto;上的.bottom。当窗口的高度很小时,这将阻止.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>

答案 1 :(得分:0)

我评论了一些代码。请检查

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>

答案 2 :(得分:0)

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;
}