拉伸 div 到父级的高度
Stretching div to height of parent
我知道这个问题的一些变体已经被问过很多次了。
但是,我似乎无法使用推荐的方法让它工作。
为什么 flex: 1
似乎被忽略了?
目的是让红色容器填满其下方可用的space。
CodeSandbox link here.
import React from "react";
import { render } from "react-dom";
import styled, { createGlobalStyle, ThemeProvider } from "styled-components";
const Style = createGlobalStyle`
body, html {
height: 100%;
background: green;
margin: 0;
padding: 0;
overflow-x: hidden;
overflow-y: auto;
}
`;
const FlexCol = styled.div`
display: flex;
flex-direction: column;
`;
const Layout = ({ children }) => {
return (
<div>
<Header>header</Header>
<FlexCol>{children}</FlexCol>
</div>
);
};
const Header = styled.div`
height: 50;
background: blue;
`;
const Home = () => {
return (
<div>
<Feed />
</div>
);
};
const FeedContainer = styled.div`
display: flex;
flex-direction: column;
flex: 1;
background: red;
`;
const Feed = () => <FeedContainer>something</FeedContainer>;
const App = () => {
return (
<Layout>
<Home />
</Layout>
);
};
render(
<ThemeProvider theme={{}}>
<React.Fragment>
<Style />
<App />
</React.Fragment>
</ThemeProvider>,
document.getElementById("root")
);
您的设置中缺少的是正文和布局之间的中间容器的高度。您在其中呈现 React 应用程序的容器:#root.
在其上添加 100% 高度可解决问题:
const Style = createGlobalStyle`
body, html {
height: 100%;
background: green;
margin: 0;
padding: 0;
overflow-x: hidden;
overflow-y: auto;
}
#root { //<-- this thingie
height: 100%;
}
`;
这是更新后的沙盒:https://codesandbox.io/s/xrw84wkw54
为了将来参考,这也可以通过设置 Layout
样式并在相关位置使用 flexGrow
来实现。
import React from "react";
import { render } from "react-dom";
import styled, { createGlobalStyle, ThemeProvider } from "styled-components";
const Style = createGlobalStyle`
body, html {
height: 100%;
background: green;
margin: 0;
padding: 0;
overflow-x: hidden;
overflow-y: auto;
}
`;
const FlexCol = styled.div`
display: flex;
flex-direction: column;
flex-grow: 1;
`;
const Layout = ({ children }) => {
return (
<div style={{ height: "100vh", display: "flex", flexDirection: "column" }}>
<Header>header</Header>
<FlexCol>{children}</FlexCol>
</div>
);
};
const Header = styled.div`
height: 50;
background: blue;
`;
const Home = () => {
return (
<div
style={{
height: "100%"
}}
>
<Feed />
</div>
);
};
const FeedContainer = styled.div`
display: flex;
flex: 1;
background: red;
height: 100%;
width: 100px;
`;
const Feed = () => <FeedContainer>something</FeedContainer>;
const App = () => {
return (
<Layout>
<Home />
</Layout>
);
};
render(
<ThemeProvider theme={{}}>
<React.Fragment>
<Style />
<App />
</React.Fragment>
</ThemeProvider>,
document.getElementById("root")
);
这给出:
我知道这个问题的一些变体已经被问过很多次了。
但是,我似乎无法使用推荐的方法让它工作。
为什么 flex: 1
似乎被忽略了?
目的是让红色容器填满其下方可用的space。
CodeSandbox link here.
import React from "react";
import { render } from "react-dom";
import styled, { createGlobalStyle, ThemeProvider } from "styled-components";
const Style = createGlobalStyle`
body, html {
height: 100%;
background: green;
margin: 0;
padding: 0;
overflow-x: hidden;
overflow-y: auto;
}
`;
const FlexCol = styled.div`
display: flex;
flex-direction: column;
`;
const Layout = ({ children }) => {
return (
<div>
<Header>header</Header>
<FlexCol>{children}</FlexCol>
</div>
);
};
const Header = styled.div`
height: 50;
background: blue;
`;
const Home = () => {
return (
<div>
<Feed />
</div>
);
};
const FeedContainer = styled.div`
display: flex;
flex-direction: column;
flex: 1;
background: red;
`;
const Feed = () => <FeedContainer>something</FeedContainer>;
const App = () => {
return (
<Layout>
<Home />
</Layout>
);
};
render(
<ThemeProvider theme={{}}>
<React.Fragment>
<Style />
<App />
</React.Fragment>
</ThemeProvider>,
document.getElementById("root")
);
您的设置中缺少的是正文和布局之间的中间容器的高度。您在其中呈现 React 应用程序的容器:#root.
在其上添加 100% 高度可解决问题:
const Style = createGlobalStyle`
body, html {
height: 100%;
background: green;
margin: 0;
padding: 0;
overflow-x: hidden;
overflow-y: auto;
}
#root { //<-- this thingie
height: 100%;
}
`;
这是更新后的沙盒:https://codesandbox.io/s/xrw84wkw54
为了将来参考,这也可以通过设置 Layout
样式并在相关位置使用 flexGrow
来实现。
import React from "react";
import { render } from "react-dom";
import styled, { createGlobalStyle, ThemeProvider } from "styled-components";
const Style = createGlobalStyle`
body, html {
height: 100%;
background: green;
margin: 0;
padding: 0;
overflow-x: hidden;
overflow-y: auto;
}
`;
const FlexCol = styled.div`
display: flex;
flex-direction: column;
flex-grow: 1;
`;
const Layout = ({ children }) => {
return (
<div style={{ height: "100vh", display: "flex", flexDirection: "column" }}>
<Header>header</Header>
<FlexCol>{children}</FlexCol>
</div>
);
};
const Header = styled.div`
height: 50;
background: blue;
`;
const Home = () => {
return (
<div
style={{
height: "100%"
}}
>
<Feed />
</div>
);
};
const FeedContainer = styled.div`
display: flex;
flex: 1;
background: red;
height: 100%;
width: 100px;
`;
const Feed = () => <FeedContainer>something</FeedContainer>;
const App = () => {
return (
<Layout>
<Home />
</Layout>
);
};
render(
<ThemeProvider theme={{}}>
<React.Fragment>
<Style />
<App />
</React.Fragment>
</ThemeProvider>,
document.getElementById("root")
);
这给出: