【问题标题】:Scrolling only content div, others should be fixed仅滚动内容 div,其他应固定
【发布时间】:2013-07-31 00:32:11
【问题描述】:

我有三个 div。我需要修复 header 和 left_side div 并滚动内容 div。我一直在寻找解决方案,并找到了 overflow 和 position 的东西。但我不能正确使用它。我怎样才能做到这一点?我会感谢每一种答案。

body {   
    width: 100%;
    height: auto;
    padding: 0;
    margin: 0px auto;
    font-family: Calibri, Georgia, Ubuntu-C;  
    font-size: 16px;
    margin-bottom: 20PX
}

#header{
    width: 100%;
    height: 139px;
    background-image: url('images/Header_grey.gif');   
}


#left_side {
    width: 210px;
    height: 700px;
    background-image: url('images/Left_side.gif');
    background-repeat:repeat-y;
    overflow:hidden; 
    position:absolute;
    font-size: 16px;
}

#content {
     height: auto;
     padding: 20px;
     margin-left: 230px;
     margin-right: 20px;
    padding-bottom: 30px
}
<div id="header">
</div>

<div id="left_side">    
</div>

<div id="content">
</div>

【问题讨论】:

    标签: html css scroll overflow css-position


    【解决方案1】:

    overflow: auto;在需要时添加滚动

    #header{
        width: 100%;
        height: 139px;
        background-image: url('images/Header_grey.gif');   
        overflow: hidden;  /* code added to prevent scroll */
    }
        
        
    #left_side{
        width: 210px;
        height: 700px;
        background-image: url('images/Left_side.gif');
        background-repeat:repeat-y;
        overflow:hidden;  /* code added to prevent scroll */
        position:absolute;
        font-size: 16px;
    }
    #content{
        height: auto;
        padding: 20px;
        margin-left: 230px;
        margin-right: 20px;
        padding-bottom: 30px;
        overflow: auto;  /* code added */
    }
    

    【讨论】:

    • height:auto 永远不会让滚动条变得可见。它宁愿滚动整个页面,因为高度将根据其内容采用。
    【解决方案2】:
    • 首先,您需要为内容区域设置一个固定高度。
    • 然后在 overflow:auto 那里

    您的代码中出现错误:: 您想要一个 div 的滚动条,但您将该 div 高度声明为 auto

    当高度为自动时,您不能要求滚动条,要拥有滚动条,您需要为该 div 设置一个固定高度,并且当内容高度大于 div 高度时,它将自动引入滚动条

    注意:因此您的 css 中的更改将是

    #content{
        height: 300px;/*..very important if you want scroll bar...*/
        overflow: auto; /*..will introduce scroll bar when needed..*/
        padding: 20px;
        margin-left: 230px;
        margin-right: 20px;
        padding-bottom: 30px
    }
    

    示例 :: FIDDLE

    【讨论】:

      【解决方案3】:

      position: sticky 在元素上,滚动时应该留在原地,在类似的情况下对我有用。 https://www.w3schools.com/css/tryit.asp?filename=trycss_position_sticky

      position: sticky;
      top: 0px;
      

      【讨论】:

        【解决方案4】:

        作为学习练习,我决定使用 CSS3 Flexbox 来更新答案。我还尝试更紧密地匹配 jstorm31 试图创建的布局。

        Ritabrata 的回答是正确的:如果您希望特定元素具有滚动条,则需要将其高度(和/或宽度)设置为固定大小并将溢出设置为自动。

        代码也可以在这里找到:Plunker

        style.css

        #header{
            overflow: hidden; 
            background-color: #880016; 
            height: 50px;
            border-bottom: 4px solid black;
            padding: 10px;
            font-size: 20px;
        }
        #side_and_content {
            display: flex;
        }
        #left_side{
            flex: 1;
            overflow:hidden; 
            background-color: #ED1B24;  
            height: 200px;
            border-right: 2px solid black;
            padding: 10px;
        }
        #content{
            flex: 5;
            overflow: auto;
            background-color: #FF7F26;   
            height: 200px;
            border-left: 2px solid black;
            padding: 10px;
        }
        

        index.html

        <div id="header">
            header header header header header header
        </div>
        <div id="side_and_content">
            <div id="left_side">  
                left side left side left side left side left side
            </div>
        
            <div id="content">
        CSS3 Flexbox Concepts: 
        Flexbox consists of flex containers and flex items.
        A flex container is declared by setting the display property of an element to either flex
         (rendered as a block) or inline-flex (rendered as inline).
        Inside a flex container there is one or more flex items.
        Note: Everything outside a flex container and inside a flex item is rendered as usual.
        Flexbox defines how flex items are laid out inside a flex container.
        Flex items are positioned inside a flex container along a flex line.
         By default there is only one flex line per flex container.<br>
         It is also possible to change the direction of the flex line.
        If we set the direction property to rtl (right-to-left), the text is drawn right to left, and also the flex line changes direction, which will change the page layout
            </div>
        </div>
        

        【讨论】:

          【解决方案5】:

          您可以只使用固定位置。 http://jsfiddle.net/Nrs2u/1/

          #header {
              position: fixed;
              z-index: 2;
              left: 0%;
              top: 0%;
              width: 100%;
              height: 10%;
              background-color: purple;
          }
          #side {
              position: fixed;
              z-index: 2;
              left: 0%;
              top: 10%;
              width: 10%;
              height: 90%;
              background-color: red;
          }
          #body {
              position: absolute;
              left: 10%;
              top: 10%;
              width: 90%;
              height: 300%;
              background-color: orange;
          }
          

          【讨论】:

            【解决方案6】:
            #left_side{
                ...
                overflow:auto;  
            
            }
            

            还设置一个padding-right 以在 div 的内部内容和滚动条之间创建一个空间

            【讨论】:

              【解决方案7】:

              如果您希望页眉和左侧在滚动时保持原位,则必须使用position:fixed

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-05-10
                • 1970-01-01
                • 2021-12-06
                • 2014-02-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-11-27
                相关资源
                最近更新 更多