【发布时间】:2019-01-19 18:52:33
【问题描述】:
有人可以帮我解决 React / css / 媒体查询问题吗?我正在尝试创建一个具有全宽和全高背景图像的首页。当我使用 const = bg {..} 在反应文件中创建样式时,它适用于桌面宽度。但是在使用 css 文件时我无法让它工作。媒体查询应将图像 url 切换为较小的屏幕尺寸。
import React from "react";
import { connectContext } from "react-connect-context";
import { Context } from "../context";
import Menu from "./Menu";
import Footer from "./Footer";
import { Header } from "semantic-ui-react";
import "./Frontpage.css";
const textStyle = {
width: 220,
height: 50,
position: "absolute",
top: 0,
bottom: 0,
left: 0,
right: 0,
margin: "auto"
};
class Frontpage extends React.PureComponent {
render() {
return (
<React.Fragment>
<Menu {...this.props} />
<div className="bg">
<Header
as="h1"
content="Evolve App"
textAlign="center"
inverted
style={textStyle}
/>
</div>
<Footer />
</React.Fragment>
);
}
}
export default connectContext(Context)(Frontpage);
Frontpage.css 文件如下所示:
bg {
background-image: url(../img/Frontpage_desktop.jpeg);
background-position: "center";
background-repeat: "no-repeat";
opacity: 0.6;
height: "100vh";
}
@media only screen and (min-width: 768px) and (max-width: 991px) {
bg {
background-image: url(../img/Frontpage_tablet.jpg);
background-position: "center";
background-repeat: "no-repeat";
opacity: 0.6;
height: "100vh";
}
}
@media only screen and (max-width: 767px) {
bg {
background-image: url(../img/Frontpage_mobile.jpg);
background-position: "center";
background-repeat: "no-repeat";
opacity: 0.6;
height: "100vh";
}
}
【问题讨论】:
标签: css reactjs media-queries