javascript - 如何定位子 div 从父 div 的开头到结尾?
问题描述
我正在尝试将FullWidthDiv
组件定位为从MainDiv
侧面的开头一直到结尾。我试过在一个绝对位置上放弃并玩弄 flex,但我不能让它不从SubDiv
组件开始。
有可能实现这一目标吗?
我的CodeSandbox方法。
这是我想要实现的目标
解决方案
这有点 hacky,但你可以ChildDiv
以align-items: center
.
此外,将 缩小FullWidthDiv
到 280。最后,您必须将flex: 1
,添加width: 100%
到 div 中,该 div 包含在组件SubChildDiv
内部App
。
export default function App() {
return (
<MainDiv>
<ChildDiv>
<div style={{flex: 1, width: '100%'}}>
<SubChildDiv />
<SubChildDiv />
</div>
<FullWidthDiv />
</ChildDiv>
</MainDiv>
);
}
const MainDiv = ({ children }) => (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
width: 250,
height: 250,
backgroundColor: "red",
padding: "1rem"
}}
>
{children}
</div>
);
const ChildDiv = ({ children }) => (
<div
style={{
width: 200,
height: 200,
backgroundColor: "blue",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "space-between"
}}
>
{children}
</div>
);
const SubChildDiv = () => (
<div
style={{
display: "flex",
width: "100%",
height: 30,
backgroundColor: "green",
border: "1px solid black"
}}
/>
);
const FullWidthDiv = () => (
<div
style={{
width: 280,
height: 30,
border: "1px solid black",
backgroundColor: "green"
}}
/>
);
如果你想要FullWidthDiv
100% 的宽度,你必须计算 100% 的宽度并从它的两侧添加填充,MainDiv
看起来像这样calc(100% + 2rem)
:
推荐阅读
- reactjs - 在 MUIDatatable 中的分页或过滤期间未显示加载程序
- javascript - 在域 Y 上定位来自域 X 的访问者
- bootstrap-4 - 使用引导类“collapsibe”将 dc.js 图表插入 div 时会变小
- flutter-layout - 交错的网格视图不在 Listview 内滚动
- python - 我对我对 Python 断言语句的使用有不同的看法,我想让你告诉我为什么我错了
- python-3.x - 如何使用依赖解析功能进行文本分类?
- asp.net-core - 在 azure 上使用 windows server core 2019 部署 asp.net core 3.1 网站
- c# - 如何检查该网站中是否有元素退出
- sql-server - 当主键数量不同时,同步框架不提供 Sql Compact
- node.js - 使用函数多次出站调用 Autopilot - 运行时应用程序超时