CSS网格滚动问题:右侧边距和边框“重叠”内容

时间:2017-09-09 12:14:42

标签: html css html5 css3 css-grid

我不明白这种行为。这是一个错误吗?我该怎么做才能使正确的填充和边框保持在(橙色)内容之外?

更新:我想要滚动。问题是右边的填充和边框没有被推到右边,而是与(橙色)内容重叠。

enter image description here

<!doctype html>
<html>
<title>Test</title>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8'>
<style>
*, *:before, *:after { padding: 0px; border: 0px; margin: 0px; box-sizing: border-box; }
#grid > div { border: 20px solid orange; }
</style>
</head>
<body style='width: 90vw; height: 90vh;'>
  <div id='scrollpane' style='width: 100%; height: 100%; overflow: auto; border: 20px solid yellow;'>
    <div id='grid' style='width: 100%; display: grid; grid-template-columns: 1fr auto 200px 1fr; border: 20px solid lightsteelblue; padding: 20px;'>
      <div>1fr</div><div>auto</div><div>200px</div><div>1fr</div>
    </div>
  </div>
</body>
</html>

2 个答案:

答案 0 :(得分:4)

在滚动窗格上使用display: grid;

&#13;
&#13;
*, *:before, *:after {
padding: 0px;
border: 0px;
margin: 0px;
box-sizing: border-box;
}

body {
width: 90vw;
height: 90vh;
}

#scrollpane {
width: 100%;
height: 100%;
overflow: auto;
border: 20px solid yellow;
display: grid; /* solution */
justify-items: stretch; /* or 'start' if you do not want content to track the scrollpane width */
align-items: start; /* or 'stretch' if you want content to track the scrollpane height */
}

#grid {
width: 100%;
display: grid;
grid-template-columns: 1fr auto 200px 1fr;
padding: 20px;
border: 20px solid lightsteelblue;
}

#grid>div {
border: 20px solid orange;
}
&#13;
<body>
  <div id='scrollpane'>
    <div id='grid'>
      <div>1fr</div>
      <div>auto</div>
      <div>200px</div>
      <div>1fr</div>
    </div>
  </div>
</body>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

可以通过定位 -

来解决
  1. position: absolutemin-width: 100%添加到#grid

  2. position: relative添加到#scrollpane

  3. (当您将absolute 相对定位到scrollpane时,它将从流量中取出并且现在会出现溢出。)

    见下面的演示:

    *,
    *:before,
    *:after {
      padding: 0px;
      border: 0px;
      margin: 0px;
      box-sizing: border-box;
    }
    
    body {
      width: 90vw;
      height: 90vh;
    }
    
    #scrollpane {
      width: 100%;
      height: 100%;
      overflow: auto;
      border: 20px solid yellow;
      position: relative; /* ADDED */
    }
    
    #grid {
      position: absolute; /* ADDED */
      min-width: 100%; /* ADDED */
      /*width: 100%;*/
      display: grid;
      grid-template-columns: 1fr auto 200px 1fr;
      border: 20px solid lightsteelblue;
      padding: 20px;
    }
    
    #grid>div {
      border: 20px solid orange;
    }
    <div id='scrollpane'>
      <div id='grid'>
        <div>1fr</div>
        <div>auto</div>
        <div>200px</div>
        <div>1fr</div>
      </div>
    </div>

    或者更好的是,您也可以将scrollpane设为网格并提供align-items: flex-start - 请参阅下面的演示:

    *,
    *:before,
    *:after {
      padding: 0px;
      border: 0px;
      margin: 0px;
      box-sizing: border-box;
    }
    
    body {
      width: 90vw;
      height: 90vh;
    }
    
    #scrollpane {
      width: 100%;
      height: 100%;
      overflow: auto;
      border: 20px solid yellow;
      display: grid;/* ADDED */
      align-items: flex-start;/* ADDED */
    }
    
    #grid {
      width: 100%;
      display: grid;
      grid-template-columns: 1fr auto 200px 1fr;
      border: 20px solid lightsteelblue;
      padding: 20px;
    }
    
    #grid>div {
      border: 20px solid orange;
    }
    <div id='scrollpane'>
      <div id='grid'>
        <div>1fr</div>
        <div>auto</div>
        <div>200px</div>
        <div>1fr</div>
      </div>
    </div>