CSS 网格滚动问题:右侧填充和边框 "overlapping" 内容

CSS Grid scrolling issue: right-side padding and border "overlapping" content

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

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

<!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>

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

*, *: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;
}
<body>
  <div id='scrollpane'>
    <div id='grid'>
      <div>1fr</div>
      <div>auto</div>
      <div>200px</div>
      <div>1fr</div>
    </div>
  </div>
</body>

可以用定位-

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

  2. position: relative添加到#scrollpane

(当您将 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>