使用flex时滚动div而不是整个页面

时间:2018-07-20 00:35:31

标签: html css flexbox

我想滚动content div而不是整个页面,我该怎么做?

<html>
<head>
</head>
<body>
<div>
  <div style="display: flex">
    <div style="margin-right: 50px">Sidebar</div>
    <div>
      <div>Header</div>
      <div id="content" style="overflow-x: auto; white-space: nowrap">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
        pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
        laborum.
      </div>
    </div>
  </div>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

@Searene这是您使用flexbox寻找的答案。 有两个摘要:一个是滚动所有内容,另一个是将标题固定到位。

这是非常简单和干净的。没有固定的宽度或CSS hacks。

<html>
<head>
</head>
<body>
<div>
  <div style="display: flex;flex-direction: row;border: solid 10px">
    <div style="margin-right: 50px; background:red">Sidebar</div>
    <div style="background:blue; overflow-x:auto;">
      <div>Header</div>
      <div style="white-space: nowrap;">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
        pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
        laborum.
      </div>
    </div>
  </div>
</div>
</body>
</html>

在此代码段中,HEADER保持不变,并且仅内容滚动。

<html>
<head>
</head>
<body>
<div>
  <div style="display: flex;flex-direction: row;border: solid 10px">
    <div style="margin-right: 50px; background:red">Sidebar</div>
    <div style="background:blue; overflow:hidden">
      <div>Header stays in place</div>
      <div id="content" style="white-space: nowrap; overflow:auto">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
        pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
        laborum.
      </div>
    </div>
  </div>
</div>
</body>
</html>

答案 1 :(得分:-1)

我最终放弃了flex,并使用float解决了这个问题。

<html>
<head>
</head>
<body style="margin: 0">
<div style="margin-right: 50px; width: 50px; float: left">Sidebar</div>
<div style="float: left; width: calc(100% - 100px)">
  <div>Header</div>
  <div id="content" style="overflow-x: auto; white-space: nowrap">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
    magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
    laborum.
  </div>
</div>
</body>
</html>