Arn*_*kus 5 css styled-components
我在 CSS 方面遇到了麻烦。目前正在努力实现两件事。
我尝试使用包装器。但这似乎不是包装器在这里的正确解决方案。老实说,我认为我缺乏解决这个问题的具体知识。所以我无法产生并尝试新的想法。
export const NavbarWrapper = styled.div`
margin-right: auto;
margin-left: auto;
max-width: 600px;
padding-right: 24px;
padding-left: 24px;
`;
const ContainerStyled = styled.div`
.content_container {
display: flex;
}
.img {
width: 100%;
}
`;
export default function App() {
return (
<ContainerStyled>
<Navbar />
<NavbarWrapper>
<div className="content_container">
<p className="title">
This should be responsive to navbar on all sizes
</p>
<img className="img" src="/image.jpg" alt="grey bridge" />
</div>
</NavbarWrapper>
</ContainerStyled>
);
}
Run Code Online (Sandbox Code Playgroud)
可重现的示例为了进行检查,我建议单击 codeSandbox 浏览器上的第三个按钮以获得更好的查看体验。
在Wrapper组件具有网格的情况下尝试此解决方案。因此,想法是创建 3 列,并将内容仅放入 2 和 3 列中。另外,将图像视图限制为 2 列。
import styled from "styled-components";
export const NavbarWrapper = styled.div`
min-width: 100%;
display: grid;
grid-template-columns: 1fr minmax(320px, 600px) 1fr;
`;
Run Code Online (Sandbox Code Playgroud)
import styled from "styled-components";
import { Navbar } from "./navbar/Navbar";
import { NavbarWrapper } from "./Wrapper";
import "./styles.css";
const ContainerStyled = styled.div`
.content_container {
display: flex;
grid-column: 2 / 2 span;
}
.img {
display: flex;
flex: 1 0 calc(100% - 29vmin);
max-height: 400px;
object-fit: cover;
overflow: hidden;
}
`;
export default function App() {
return (
<ContainerStyled>
<Navbar />
<NavbarWrapper>
<div className="content_container">
<p className="title">
This should be responsive to navbar on all sizes
</p>
<img className="img" src="/image.jpg" alt="grey bridge" />
</div>
</NavbarWrapper>
</ContainerStyled>
);
}
Run Code Online (Sandbox Code Playgroud)
最后,我们需要grid-column: 2;向导航栏添加一个属性,以保持导航链接居中。
ul {
grid-column: 2; /* new line */
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
display: flex;
}
Run Code Online (Sandbox Code Playgroud)