the*_*uls 8 javascript css sass flexbox reactjs
我有一个随着屏幕变小而需要包装的项目列表。还有另一个项目需要与它们保持特定的空间,特别是 8px。
问题是,当它们开始换行时,被换行的元素会留下一堆空间。
所有项目之间的间隔必须为 8 像素,包括不换行的项目。我怎样才能做到没有空白空间?
这是一个工作示例:
const App = () => {
return (
<div>
<p>see the items below, regardless of how many I have, I need to hide whatever doesn't fit on the line screen.</p>
<p>All items need to have 8px gap in between them, including the green one which does not wrap.</p>
<h5>How can this be done?</h5>
<div className="container">
<div className="wrap-container">
{Array.from({ length: 100 }).map((_, index) => <div className="item" key={index}>{index}</div>)}
</div>
<div className="non-wrap-item"> I need to be 8px from the last visible item</div>
</div>
</div>
)
}
ReactDOM.render(
<App />,
document.getElementById('app')
);Run Code Online (Sandbox Code Playgroud)
.container {
display: flex;
}
.wrap-container {
max-width: 500px;
display: flex;
flex-wrap: wrap;
height: 80px;
overflow: hidden;
gap: 8px;
border: 1px solid red;
margin-right: 8;
}
.item {
width: 80px;
height: 80px;
background-color: blue;
color: white;
}
.non-wrap-item {
width: 80px;
height: 80px;
background-color: green;
color: white;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>Run Code Online (Sandbox Code Playgroud)
注意:我见过类似的问题,但没有一个解决我始终需要项目之间有 8px 间隙的事实。我遇到的那些人我都失去了控制。有没有办法解决?
使用grid代替flexbox会更容易,如下所示:
const App = () => {
return (
<div>
<p>see the items below, regardless of how many I have, I need to hide whatever doesn't fit on the line screen.</p>
<p>All items need to have 8px gap in between them, including the green one which does not wrap.</p>
<h5>How can this be done?</h5>
<div className="container">
<div className="wrap-container">
{Array.from({ length: 100 }).map((_, index) => <div className="item" key={index}>{index}</div>)}
</div>
<div className="non-wrap-item"> I need to be 8px from the last visible item</div>
</div>
</div>
)
}
ReactDOM.render(
<App />,
document.getElementById('app')
);Run Code Online (Sandbox Code Playgroud)
.container {
display: grid;
grid-template-columns:1fr 80px;
gap: 8px;
/* if you want some max-width, put it here instead*/
max-width: 500px;
}
.wrap-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(80px,1fr));
gap: 8px;
}
.item {
height: 80px;
background-color: blue;
color: white;
}
.non-wrap-item {
height: 80px;
background-color: green;
color: white;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1987 次 |
| 最近记录: |