【发布时间】:2021-04-13 16:31:58
【问题描述】:
我有 2 个框:面板和侧边栏,在一个弹性框中,带有 flex-direction: row 和 justify-content: space-between 我可以将它们并排水平对齐,中间有一个间隙。
现在我想修复右侧边栏的位置,即当向下滚动时,边栏应始终保持在屏幕顶部,无论面板的位置如何。我怎样才能用 CSS 做到这一点?
这里的工作示例:https://codesandbox.io/s/gallant-saha-zgmnw
代码在这里:
App.js:
import React from "react";
import "./App.scss";
import Panel from "./Panel";
import Sidebar from "./Sidebar";
export default function App() {
return (
<div className="App">
<Panel />
<Sidebar />
</div>
);
}
App.scss:
.App {
width: 1100px;
font-family: sans-serif;
text-align: center;
display: flex;
flex-direction: row;
justify-content: space-between;
}
Panel.js:
import React from "react";
import "./Panel.scss";
const Panel = () => {
return (
<>
<div className="panel">The main panel</div>
</>
);
};
export default Panel;
Sidebar.js:
import React from "react";
import "./Sidebar.scss";
const Sidebar = () => {
return (
<>
<div className="sidebar">The sidebar</div>
</>
);
};
export default Sidebar;
【问题讨论】:
-
使用绝对或粘性
-
@DCR 可以与父母
display: flex一起使用吗? -
显示:弹性;位置:相对;然后在你想要放置的元素中使用 position:absolute
标签: javascript reactjs flexbox