3 回答

TA贡献1828条经验 获得超6个赞
设置背景图像有两种解决方案:
使用 CSS 中的背景属性
.divText {
background-image:url("https://b.zmtcdn.com/data/collections/271e593eb19475efc39021c6e92603db_1454591396.jpg");
}
使用定位来制作自定义布局
export default function App() {
return (
<div className="App">
<h1>Restaurant</h1>
<div
style={{
position: "relative",
height: "200px",
width: "300px"
}}
>
<img
style={{
width: "100%",
height: "100%"
}}
src="https://b.zmtcdn.com/data/collections/271e593eb19475efc39021c6e92603db_1454591396.jpg"
alt=""
className="img"
/>
<div className="divText"
style={{
position: "absolute",
bottom: 0,
height: "50%",
width: "100%",
color: "#fff",
backgroundColor: "#000",
opacity: 0.7
}}>
Lorem Ipsum! Lorem Ipsum! Lorem Ipsum!
</div>
</div>
</div>
);
}

TA贡献1831条经验 获得超9个赞
也许你可以做这样的事情?
""
.wrapper {
width: 400px;
height: 200px;
position: relative;
}
picture {
position: absolute;
z-index: -1;
}
picture img {
object-fit: cover;
top: 0;
}
.text-overlay {
top: 50%;
height: 50%;
position: relative;
background-color: white;
display: flex;
z-index: 2;
padding: 10px;
opacity: 0.8;
font-family: sans-serif;
}
<div class="wrapper">
<picture>
<img src="https://images.unsplash.com/photo-1601758064224-c3c5ec910095?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2047&q=80"
width="400px"
height="200px"
style=" "/>
</picture>
<div class="text-overlay">
<p>Hello</p>
</div>
</div>

TA贡献2021条经验 获得超8个赞
我的这个解决方案:
样式.css:
.divText {
position: absolute;
height: 50%;
background-color: white;
bottom: 0;
}
应用程序.js:
import React from "react";
import "./styles.css";
export default function App() {
return (
<div className="App">
<h1>Restaurant</h1>
<div
style={{
display: "flex",
flexDirection: "column",
justifyContent: "baseline",
alignItems: "baseline",
height: "200px",
width: "100%",
maxWidth: "300px",
position: "relative"
}}
>
<div
style={{
height: "inherit"
}}>
<img
style={{
width: "100%",
height: "100%"
}}
src="https://b.zmtcdn.com/data/collections/271e593eb19475efc39021c6e92603db_1454591396.jpg"
alt=""
className="img"
/>
</div>
<div className="divText">
<span
style={{
color: "#fff",
backgroundColor: "#000"
}}
>
Lorem Ipsum! Lorem Ipsum! Lorem Ipsum!
</span>
</div>
</div>
</div>
);
}
添加回答
举报