我不明白这种行为。这是一个错误吗?我该怎么做才能使正确的填充和边框保持在(橙色)内容之外?
更新:我想要滚动。问题是右边的填充和边框没有被推到右边,而是与(橙色)内容重叠。
<!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>
答案 0 :(得分:4)
在滚动窗格上使用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;
}
&#13;
<body>
<div id='scrollpane'>
<div id='grid'>
<div>1fr</div>
<div>auto</div>
<div>200px</div>
<div>1fr</div>
</div>
</div>
</body>
&#13;
答案 1 :(得分:2)
可以通过定位 -
来解决将position: absolute
和min-width: 100%
添加到#grid
。
将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>