為什么我的道具不能正常工作?我的錯誤是什么?文本和圖像相互復制
Card.js:
import React from "react";
import "./Card.css";
function Card(props) {
return (
<div>
<img />
<span className="location-style">{props.location}</span>
<span className="view-style">{props.view}</span>
<h1 className="title-style">{props.title}</h1>
<p className="decs-style">{props.description}</p>
<span className="date-style">{props.date}</span>
</div>
);
}
export default Card;
MainContent.js:
import React from "react";
import "../Components/MainContent.css";
import Card from "../Components/Card.js";
import data from "./data.js";
function MainContent() {
const content = data.map((item) => {
return (
<Card
key={item.id}
coverImg={item.coverImg}
title={item.title}
location={item.location}
view={item.view}
description={item.description}
date={item.stats.date}
/>
);
});
return (
<>
<header>
<span className="header-text">my travel journal.</span>
</header>
<section>{content}</section>
</>
);
}
export default MainContent;
data.js:
export default [{
id: 1,
title: "Mount Fuji",
description: "Mount Fuji is the tallest mountain in Japan, standing at 3,776 meters (12,380 feet). Mount Fuji is the single most popular tourist site in Japan, for both Japanese and foreign tourists.",
view: 'View on Google Maps',
coverImg: "img here",
stats: {
date: '12 Jan, 2021 - 24 Jan, 2021',
},
location: "JAPAN",
},
{
id: 2,
title: "Sydney Opera House",
description: "The Sydney Opera House is a multi-venue performing arts centre in Sydney. Located on the banks of the Sydney Harbour, it is often regarded as one of the 20th century's most famous and distinctive buildings",
view: 'View on Google Maps',
coverImg: "img here",
stats: {
date: '12 Jan, 2021 - 24 Jan, 2021',
},
location: "AUSTRALIA",
},
{
id: 3,
title: "Geirangerfjord",
description: "The Geiranger Fjord is a fjord in the Sunnm?re region of M?re og Romsdal county, Norway. It is located entirely in the Stranda Municipality.",
view: 'View on Google Maps',
coverImg: "https://source.unsplash.com/3PeSjpLVtLg",
stats: {
date: '12 Jan, 2021 - 24 Jan, 2021',
},
location: "NORWAY"
}
]
Card.css:
.img-style {
position: absolute;
width: 125px;
height: 168px;
left: 40px;
top: 100px;
border-radius: 5px;
}
.location-style {
position: absolute;
width: 40px;
height: 10px;
left: 195px;
top: 119px;
font-family: 'Inter';
font-style: normal;
font-weight: 400;
font-size: 10.24px;
line-height: 12px;
letter-spacing: 0.17em;
color: #2B283A;
}
.view-style {
position: absolute;
width: 106px;
height: 13px;
left: 247px;
top: 119px;
font-family: 'Inter';
font-style: normal;
font-weight: 400;
font-size: 10.24px;
line-height: 12px;
/* identical to box height */
text-decoration-line: underline;
color: #918E9B;
}
.title-style {
position: absolute;
width: 326px;
height: 33px;
left: 184px;
top: 333px;
font-family: 'Inter';
font-style: normal;
font-weight: 700;
font-size: 25px;
line-height: 30px;
color: #2B283A;
}
.decs-style {
position: absolute;
width: 326px;
height: 45px;
left: 184px;
top: 203px;
font-family: 'Inter';
font-style: normal;
font-weight: 400;
font-size: 10.24px;
line-height: 150%;
/* or 15px */
color: #2B283A;
}
.date-style {
position: absolute;
width: 319px;
height: 11px;
left: 184px;
top: 380px;
font-family: 'Inter';
font-style: normal;
font-weight: 700;
font-size: 10.24px;
line-height: 12px;
color: #2B283A;
}
我猜你們的牌是疊在一起的吧?
發生這種情況是因為您使用的是
position: absolute;
,而包裝元素<div>
沒有position: relative;
。但您確實不應該使用
position
來“定位”元素,而應該使用正常流來布局元素,并且只對邊緣情況使用絕對定位,因為它會將元素從正常流中“拉出”。了解以下概念:
用于根據文檔中的自然流定位/布局元素。
還有很多概念,但學習這些應該會讓你在游戲中走得更遠。