CSS怎么實(shí)現(xiàn)頭部和底部固定中間出現(xiàn)滾動(dòng)條

本篇內(nèi)容介紹了“CSS怎么實(shí)現(xiàn)頭部和底部固定中間出現(xiàn)滾動(dòng)條”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的濱海網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

原理說明

利用flex布局,很容易實(shí)現(xiàn)“左右兩邊固定,剩余100%”的布局模式

利用flex-direction: column;樣式,就很容易實(shí)現(xiàn)“頂部和底部固定,中間100%”的情況

要設(shè)置html,body的高度為100%;否則設(shè)置的div高度為100%是0px;

必須要保證設(shè)置的控件高度從html>body>div>....>div 需要一層一層的繼承下來

案例(原理說明)

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <title>Title</title>

</head>

<style>

  /*設(shè)置html和body的高度為顯示屏的高度*/

  html, body {

    height: 100%;

    margin: 0;

  }

  .wrap {

    display: -webkit-box;

    display: -webkit-flex;

    display: -ms-flexbox;

    display: flex;

    -webkit-box-orient: vertical;

    -webkit-flex-direction: column;

    -ms-flex-direction: column;

    /*布局方向是垂直的*/

    flex-direction: column;

    width: 100%;

    height: 100%;

  }

  /*設(shè)置頂部和底部的高度*/

  .header, .footer {

    height: 40px;

    line-height: 40px;

    background-color: #D8D8D8;

    text-align: center;

  }

  /*設(shè)置高度是跟父元素的高度一致,100%;*/

  /*實(shí)際高度是 100% 減去頂部高度和底部高度,左右兩邊固定,中間是剩余100%原理一致*/

  .main {

    -webkit-box-flex: 1;

    -webkit-flex: 1;

    -ms-flex: 1;

    flex: 1;

    width: 100%;

    overflow: auto;

  }

</style>

<body>

<div class="wrap">

  <div class="header">header</div>

  <div class="main">

    <div style="height: 300%">彈性滾動(dòng)區(qū)域</div>

  </div>

  <div class="footer">footer</div>

</div>

</body>

</html>

案例二(回字形布局)

利用

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <title>Title</title>

</head>

<style>

  html,body{

    height: 100%;

    margin: 0;

  }

  .wrap{

    display: flex;

    flex-direction: column;

    height: 100%;

  }

  .header{

    height: 50px;

    padding: 15px;

  }

  .footer{

    height: 50px;

  }

  .main{

    flex-grow: 1;

    overflow: auto;

    display: flex;

    align-items: flex-start;

  }

  .left{

    width: 300px;

    background: yellowgreen;

  }

  .right{

    width: 300px;

    background: greenyellow;

  }

  .center{

    flex-grow: 1;

    background: aquamarine;

    height: 100%;

    overflow: auto;

  }

</style>

<body>

<div class="wrap">

  <div class="header">header</div>

  <div class="main">

    <div class="left">

      left

    </div>

    <div class="center">

      <div style="height: 300%">

        <div>center</div>

      </div>

    </div>

    <div class="right">

      right

    </div>

  </div>

  <div class="footer">footer</div>

</div>

</body>

</html>

設(shè)置html和body的高度為100%,則body的高度是顯示器的高度

利用flex布局,頭部和底部固定,中間設(shè)置為剩下的100%

中間部分,利用flex布局,左右兩邊固定,中間是剩下的100%

設(shè)置“中心”的高度為100%,則是參照父元素的高度,除去頂部和底部的高度的,剩下的100%高度

案例 (計(jì)算出中間組件的高度,剩下的百分百)

設(shè)置html和body的高度為100%,則body的高度為顯示器的高度,則子元素的高度是參考body的

頭部和底部固定,計(jì)算出中間的高度

利用flex布局,左右兩邊固定,中間為剩下的100%

高度設(shè)置為父元素的100%;中間如果內(nèi)容過多,則設(shè)置overflow:auto就會(huì)出現(xiàn)滾動(dòng)條

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<style>

  html,body{

    margin: 0;

    height: 100%;

  }

  .flex-study{

    line-height: 35px;

    height: calc(100% - 100px);

  }

  .flex{

    display: flex;

  }

  .header{

    width: 100%;

    background: #42a5f6;

  }

  .content{

    width: 100%;

    background: bisque;

    align-items: flex-start;

    height: 100%;

    overflow: hidden;

  }

  .left{

    width: 300px;

    background: yellowgreen;

  }

  .right{

    width: 300px;

    background: greenyellow;

  }

  .center{

    flex-grow: 1;

    background: aquamarine;

    height: 100%;

    overflow: auto;

  }

  .footer{

    width: 100%;

    background: blueviolet;

  }

</style>

<body>

<div class="flex-study">

  <div class="header">

    header

  </div>

  <div class="content flex">

    <div class="left">

      left

    </div>

    <div class="center">

      <div style="height: 300%">

        <div>center</div>

      </div>

    </div>

    <div class="right">

      right

    </div>

  </div>

  <div class="footer">

    footer

  </div>

</div>

</body>

</html>

“CSS怎么實(shí)現(xiàn)頭部和底部固定中間出現(xiàn)滾動(dòng)條”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

標(biāo)題名稱:CSS怎么實(shí)現(xiàn)頭部和底部固定中間出現(xiàn)滾動(dòng)條
文章鏈接:http://muchs.cn/article8/iiddop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、營銷型網(wǎng)站建設(shè)網(wǎng)站收錄、靜態(tài)網(wǎng)站、面包屑導(dǎo)航標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)