代码之家  ›  专栏  ›  技术社区  ›  Rolando

如何在Bootstrap 4中创建适合屏幕的基本4区域模板(不带滚动或中间区域的显式高度设置)?

  •  0
  • Rolando  · 技术社区  · 8 年前

    在Bootstrap 4中,我如何(或最好的方法是什么)生成a(基本?)看起来像底部图像的布局模板?

    要求:

    1. 请注意,我希望content1和content2的div/container填充导航栏和页脚之间的空间,以完全适合任何用户的浏览器窗口 高度不得超过,并在整个页面上创建滚动条。
    2. 页脚应始终保持在content1和content2之下,并且在用户页面底部始终可见,就像一个粘性页脚。
    3. 在content2(或content1)中创建的任何新容器/div都应该自动适应content2(或content1)的整个区域。
    4. 我希望避免在content1和content2上设置显式高度,除非它是类名,但我不希望将700px之类的特定内容设置为高度,因为我希望它适合任何用户的显示,700px可能正好适合某些用户,但在content1和content2以及页脚之间为其他用户留有大量空白。
    5. 导航栏和页脚区域可以显式设置高度,必要时也可以显式设置content1和content2的边距/填充。我只是想避免显式设置content1和content2的高度。

    我看到的所有示例都是一个“可滚动”页面,其中的内容可以超出用户的浏览器高度,用户可以在页面上下滚动以查看内容,而在我的示例中,我希望下面图像中的4个区域与我的屏幕完全匹配,这样就没有滚动。如果由于content1中的文本内容溢出而导致滚动,那么溢出:auto应该允许滚动content1或content2,而不是整个页面。

    enter image description here

    如果我所要求的是不可能的,请提供一个样本,这将是理想的,以实现我所寻找的最佳替代方案。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Rahul    8 年前

    这很容易使用 Bootstrap@4 或使用 flex-box CSS . 使用检查代码段 Bootstrap@4 .

    body{
      height: 100%;
      width: 100%;
    }
    .main-wrapper{
      background-color: #f5f5f5;
      height: 100%;
      left: 0;
      padding-bottom: 50px;
      padding-top: 50px;
      position: fixed;
      top: 0;
      width: 100%;
    }
    @media (max-width: 991px){
      .main-wrapper{
        overflow-x: hidden;
        overflow-y: auto;
      }
    }
    .header-wrapper{
      background-color: #009688;
      color: white;
      height: 50px;
      width: 100%;
    }
    .footer-wrapper{
      background-color: #121212;
      color: white;
      height: 50px;
      width: 100%;
    }
    .body-wrapper{
      background-color: #f0f0f0;
      width: 100%;
    }
    .content-1{
      background-color: #2DC050;
      color: white;
      padding: 30px;
      width: 100%;
    }
    .content-2{
      background-color: #1FA325;
      color: white;
      padding: 30px;
      width: 100%;
    }
    @media (min-width: 992px){
      .content-1,
      .content-2{
        overflow-x: hidden;
        overflow-y: auto;
      }
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
    <div class="main-wrapper d-flex flex-row">
      <div class="header-wrapper fixed-top d-flex flex-row align-items-center justify-content-center">
        I'm fixed header. I will be alwase visible on top. I'm fixed height.
      </div>
      <div class="body-wrapper d-flex flex-row flex-wrap">
        <div class="col-12 col-lg-6 d-flex flex-row pl-0 pr-0">
          <div class="content-1 d-flex flex-column text-center">
            <h1>I'm content One. My position is left. I'm alwase fit with any browser window except header and footer height. Also I'm scrollable if content isn't fit.</h1>
            <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
          </div>
        </div>
        <div class="col-12 col-lg-6 d-flex flex-row pl-0 pr-0">
          <div class="content-2 d-flex flex-column">
            <h1>I'm content Two. My position is right. I'm alwase fit with any browser window except header and footer height. Also I'm scrollable if content isn't fit.</h1>
            <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
          </div>
        </div>
      </div>
      <div class="footer-wrapper fixed-bottom d-flex flex-row align-items-center justify-content-center">
        I'm fixed footer. I will be alwase visible on bottom. I'm fixed height.
      </div>
    </div>
    1. 始终使用页眉和页脚 fixed .
    2. 内容1和内容2将 scroll-able if浏览器 min-width 是 992px .
    3. 整个页面将 可滚动 if浏览器 max-width 是 991px .

    编辑用于 Ronaldo 评论 . 答案是肯定的,这是可能的。检查底部代码段。

    body{
      height: 100%;
      width: 100%;
    }
    .main-wrapper{
      background-color: #f5f5f5;
      height: 100%;
      left: 0;
      padding-bottom: 50px;
      padding-top: 50px;
      position: fixed;
      top: 0;
      width: 100%;
    }
    .header-wrapper{
      background-color: #009688;
      color: white;
      height: 50px;
      width: 100%;
    }
    .footer-wrapper{
      background-color: #121212;
      color: white;
      height: 50px;
      width: 100%;
    }
    .body-wrapper{
      background-color: #f0f0f0;
      width: 100%;
    }
    @media (max-width: 991px){
      .body-wrapper{
        overflow-x: hidden;
        overflow-y: auto;
      }
    }
    .content-1{
      background-color: #2DC050;
      color: white;
      padding: 30px;
      width: 100%;
    }
    .content-2{
      background-color: #1FA325;
      color: white;
      min-height: 700px;
      padding: 30px;
      width: 100%;
    }
    @media (min-width: 992px){
      .content-1{
        overflow-x: hidden;
        overflow-y: auto;
      }
    }
    .content-2-1{
      background-color: #0D420F;
      padding: 20px;
      width: 100%;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
    <div class="header-wrapper fixed-top d-flex flex-row align-items-center justify-content-center">
        I'm fixed header. I will be alwase visible on top. I'm fixed height.
    </div>
    <div class="main-wrapper d-flex flex-row">
      <div class="body-wrapper d-flex flex-row flex-wrap">
        <div class="col-12 col-lg-6 d-flex flex-row pl-0 pr-0">
          <div class="content-1 d-flex flex-column text-center">
            <h1>I'm content One. My position is left. I'm alwase fit with any browser window except header and footer height. Also I'm scrollable if content isn't fit.</h1>
            <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
          </div>
        </div>
        <div class="col-12 col-lg-6 d-flex flex-row pl-0 pr-0">
          <div class="content-2 d-flex flex-row">
            <div class="content-2-1 d-flex flex-column align-items-center justify-content-center text-center">
              I'm Content 2 child element. My parent content-2 background color name is Forest Green. My background color is Myrtle. My width and height is 100%. I follow my parent height.
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer-wrapper fixed-bottom d-flex flex-row align-items-center justify-content-center">
        I'm fixed footer. I will be alwase visible on bottom. I'm fixed height.
    </div>
    1. 新代码段包括 html 结构已更改。
    2. 去除 page scroll 从…起 .main-wrapper class 并添加到 body-wrapper 班 .
    3. 如果需要 child 盒 100% width 和 height 然后 parent 应该是 display: flex; flex-direction: row; 和 小孩 盒 应该是 width: 100%; // width can be changed if it multiple 小孩 box. 检查 .content-2 和 .content-2-1 CSS 密码

    注意:所有代码段都将签入完整页面视图,并调整浏览器窗口的大小以检查移动响应。