JSFiddle - React, Tailwind, and code Playground
HTML
<button class="load">Load Products</button>
<hr>
<ul class="lists"></ul>
<div class="overlay">
<div class="modal">
<div class="photo">
<img src="" >
</div>
<div class="content">
<h2 class="title"></h2>
<h3 class="category"></h3>
<p class="description"></p>
<p class="price"></p>
</div>
<button class="close">✖</button>
</div>
</div>
CSS
* {
margin: 0;
padding: 0;
list-style: none;
}
html, body {
width: 100%;
height: 100%;
}
.load {
font-size: 24px;
background-color: bisque;
padding: 5px 10px;
border-radius: 8px;
cursor: pointer;
transform: scale(.8);
transition: scale .2s;
}
.load:hover {
transform: scale(1);
}
ul {
display: grid;
grid-template-columns: repeat(auto-fill, 200px);
grid-gap: 20px;
justify-content: space-evenly;
}
li {
display: flex;
flex-direction: column;
}
li div {
width: 150px;
height: 200px;
margin: 0 auto;
}
li div img {
width: 100%;
height: 100%;
vertical-align: middle;
}
li button {
width: 100px;
margin: 0 auto;
background-color: transparent;
border-radius: 8px;
font-size: 18px;
cursor: pointer;
transition: background .2s;
margin-top: 5px;
}
li button:nth-of-type(2):hover {
background-color: #f77;
}
li button:hover {
background-color: bisque;
}
span {
text-align: center;
font-size: 18px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.overlay {
display: none;
position: fixed;
top: 0;
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, .5);
}
.modal {
position: absolute;
display: none;
justify-content: space-around;
gap: 10px;
top: 50%;
left: 50%;
padding: 30px;
transform: translate(-50%, -50%);
width: 600px;
background-color: #fff;
border-radius: 10px;
}
.photo {
display: flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
width: 140px;
}
img {
vertical-align: middle;
width: 100%;
}
.content {
display: flex;
flex-direction: column;
justify-content: center;
}
.category {
margin: 10px 0;
color: gray;
}
.description {
margin-bottom: 10px;
color: gray;
font-size: 16px;
}
.price {
font-size: 24px;
font-weight: bold;
color: red;
}
.price::before {
content: 'US$';
margin-right: 8px;
}
.close {
position: absolute;
top: 0;
right: 0;
background-color: bisque;
...
JavaScript
const loadButton = document.querySelector('.load');
const lists = document.querySelector('.lists');
const overlay = document.querySelector('.overlay');
const modal = document.querySelector('.modal');
const closeButton = document.querySelector('.close');
const productTitle = document.querySelector('.title');
const ProductCategory = document.querySelector('.category');
const productDescription = document.querySelector('.description');
const productPrice = document.querySelector('.price');
const productImage = document.querySelector('.photo img');
function openModal() {
overlay.style.display = 'block';
modal.style.display = 'flex';
}
function closeModal() {
overlay.style.display = 'none';
modal.style.display = 'none';
}
loadButton.onclick = () => {
const url = 'https://fakestoreapi.com/products';
fetch(url)
.then(res=>res.json())
.then(items=> {
for (const item of items) {
const li = document.createElement('li');
const div = document.createElement('div');
const img = document.createElement('img');
const span = document.createElement('span');
const detailButton = document.createElement('button');
const deleteButton = document.createElement('button');
span.textContent = item.title;
detailButton.textContent = 'Detail';
deleteButton.textContent = 'Delete';
img.src = item.image;
li.dataset.title = item.title;
li.dataset.category = item.category;
li.dataset.description = item.description;
li.dataset.price = item.price;
li.dataset.image = item.image;
detailButton.onclick = () => {
const target = event.target;
const list = target.closest('li');
productTitle.textContent = list.dataset.title;
ProductCategory.textContent = list.dataset.category;
productDescription.textContent = list.dataset.description
productPrice.textContent =...