英文:
How do I fix the sizing of my grid cells?
问题
我正在尝试复制这个网站 https://rickandmortyapi.com/,我正在使用网格布局使我的版本更具响应性。当我将grid-template-columns设置为repeat(auto-fit, minmax(589.19, 1fr))时,网格单元格会在水平方向延伸到页面的末尾,尽管我设置了minmax。我这样做的原因是要根据浏览器窗口的大小来改变每行的单元格数量。
main {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(589.19px, 1fr));
gap: 0;
overflow-x: hidden;
}
这是您提供的CSS代码的一部分,用于设置网格布局,并解决了您的问题。如果需要进一步的翻译,请告诉我。
英文:
I'm trying to replicate this website https://rickandmortyapi.com/ I'm making my version more responsive by using grid layouts. When I set the grid-template-columns to repeat(auto-fit, minmax(589.19, 1fr)) the grid cells extend horizontally to the end of the page despite me setting the minmax. The reason I did this is to have the number of cells per row change depending on the size of the browser window.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const mainEl = document.querySelector("main");
const main = async () => {
const characters = await fetchAllCharacters();
const maxCells = 6;
for (let i = 0; i < Math.min(characters.length, maxCells); i++) {
const character = characters[i];
const episode = await fetchJsonAsync(character.episode[0]);
mainEl.insertAdjacentHTML('beforeend', renderCharacterHTML(character, episode));
}
};
const renderCharacterHTML = (character, episode) => `
<article class="character-card">
<div class="image-container">
<img src="${character.image}" alt="Character" height="100%" width="100%">
</div>
<div class="character-info">
<div class="section">
<h2 class="orangefy">${character.name}</h2>
<div class="status">
<span class="status-dot ${getStatusColor(character.status)}"></span>
<span class="status">${character.status} - ${character.species}</span>
</div>
</div>
<div class="section">
<span class="greytext">Last known location:</span>
<span class="orangefy">${character.location.name}</span>
</div>
<div class="section">
<span class="greytext">First seen in:</span>
<span class="orangefy">${episode.name}</span>
</div>
</div>
</article>
`;
const getStatusColor = (status) => {
switch (status.toLowerCase()) {
case 'unknown':
return 'grey';
case 'dead':
return 'red';
case 'alive':
return 'green';
default:
return '';
}
};
const fetchAllCharacters = async () => {
const response = await fetchJsonAsync('https://rickandmortyapi.com/api/character');
return response.results;
};
const fetchJsonAsync = async (url) => {
try {
let response = await fetch(url);
if (!response.ok) throw new Error(response.statusText);
return response.json();
} catch (err) {
console.error('Error fetching JSON:', err);
}
};
main();
<!-- language: lang-css -->
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
.top-container{
position: relative;
height: 465px;
padding-bottom: 0;
}
.showcase {
background-color: rgb(39,43,51);
}
.showcase-inner {
margin: 0;
padding-top: 80px;
padding-bottom: 80px;
padding-left: 10px;
padding-right: 10px;
}
.footer {
background-color: rgb(32,35,41);
padding-top: 50px;
padding-bottom: 21px;
}
h1{
font-size: 100px;
text-align: center;
font-weight: 1000;
color: rgb(32,35,41);
}
h2{
font-weight: 1000;
font-size: x-larges;
}
main {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(589.19, 1fr));
gap: 0;
overflow-x: hidden;
}
.image-container{
display: flex;
}
.image-container img{
width: 229.2px;
height: 220px;
border-radius: 10px 0px 0px 10px;
}
article{
margin-left: 5px;
margin-right: 5px;
}
.character-card{
display: flex;
margin: 13.5px;
background-color: rgb(60,62,68);
border-radius: 10px;
box-shadow: 0 0 10px rgba(37, 37, 37, 0.5);
}
.character-info{
display: flex;
flex-direction: column;
padding: 13.5px;
position: relative;
}
.section{
display: flex;
flex-direction: column;
width: 330px;
height: 64.33px;
color: rgb(245,245,245);
}
.greytext{
color: rgb(158,158,158);
padding-top: 5px;
padding-bottom: 10px;
}
.navbar {
background-color: #ffffff;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display: flex;
justify-content: space-between;
}
.navbar li {
float: left;
}
.navbar li a {
display: block;
color: #333;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: color 0.2s;
}
.content-links{
margin-left: auto;
padding: 10px;
margin-right: 10px;
font-size: 20px;
font-weight: 750;
}
.content-links a:hover{
color: rgb(255,152,0);
}
.homebutton{
margin-right: auto;
}
.homebutton img{
width: 50px;
height: 50px;
}
.status {
display: flex;
align-items: center;
font-weight: 600;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
display: inline-block;
margin-right: 5px;
}
.status-dot.grey {
background-color: rgb(158,158,158);
}
.status-dot.red {
background-color: rgb(214,61,46);
}
.status-dot.green {
background-color: rgb(85,204,68);
}
.about-content{
display: flex;
flex-direction: column;
padding: 5px;
padding-bottom: 100px;
margin-left: 500px;
margin-right: 500px;
font-size: 110%;
color: rgb(66,66,66);
}
.abouth2 {
font-size: 35px;
font-weight: 800;
margin-bottom: 30px;
color: rgb(32,35,41);
}
.abouth3 {
font-size: 25px;
font-weight: 800;
margin-top: 10px;
margin-bottom: 10px;
color: rgb(32,35,41);
}
.ablink {
text-decoration-color: rgb(255,152,0);
text-decoration-thickness: 2px;
text-underline-offset: 4px;
color: rgb(66,66,66);
}
.ablink:hover {
text-decoration: none;
color: rgb(255,152,0);
transition: color 0.2s;
}
.footer-info {
display: flex;
justify-content: center;
list-style: none;
padding-top: 40px;
margin: 0;
font-size: 14px;
font-weight: 750;
color: rgb(158, 158, 158);
}
.footer-info li {
margin-right: 20px;
}
.socials {
display: flex;
justify-content: center;
align-items: center;
padding-top: 2px;
padding-bottom: 2px;
}
.socials a {
margin: 14px;
}
.socials a:hover svg path {
fill: rgb(255, 152, 0);
}
.sign {
color: rgb(158, 158, 158);
text-align: center;
font-size: 13px;
padding-top: 15px;
padding-bottom: 50px;
}
.footer-link {
color:rgb(255, 255, 255);
font-weight: 700;
text-decoration-color: rgb(255,152,0);
text-decoration-thickness: 2px;
text-underline-offset: 4px;
}
.footer-link:hover {
text-decoration: none;
color: rgb(255,152,0);
transition: color 0.2s;
}
.banner {
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 0;
}
.banner svg {
width: 480px;
height: auto;
}
.banner-text{
display: flex;
justify-content: center;
align-items:center;
z-index: 10;
position: relative;
top: 150px;
}
.poweredby {
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
padding-top: 30px;
}
.poweredby-icon {
padding-left: 18px;
padding-right: 18px;
}
.server-status {
display: flex;
align-items: center;
text-justify: center;
justify-content: center;
font-weight: 700;
font-size: 14px;
padding-top: 10px;
}
.server-status-dot {
display: inline-block;
width: 9px;
height: 9px;
border-radius: 50%;
background-color: rgb(85,204,68);
margin-left: 10px;
}
.status-link {
text-decoration: none;
color: rgb(158, 158, 158);
}
.status-link:hover {
color: rgb(255,152,0);
transition: color 0.2s;
}
.orangefy:hover {
color: rgb(255,152,0);
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Rick and Morty API</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<ul class="navbar">
<li class="homebutton"><a href="landing.html"><img src="RMAPI-homepagebutton.png" alt="Home"></a></li>
<li class="content-links"><a href="about.html">About</a></li>
</ul>
<div class="top-container">
<h1 class="banner-text">The Rick and Morty API</h1>
</div>
<div class="showcase">
<div class="showcase-inner">
<main></main>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
<!-- end snippet -->
答案1
得分: 1
I have checked your grid-template-columns CSS is not applying in the main
tag.
I have tried and made some changes in CSS for the main
class which is giving desired output.
main
{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(589px, 1fr));
gap: 0;
overflow-x: hidden;
}
May be It will help you... Thank you...!
英文:
here I have checked your grid-template-columns css is not applying in tag main.
I have tried and made some changes in css for main class which is giving desired output.
main {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(589px, 1fr));
gap: 0;
overflow-x: hidden;
}
May be It will help you... Thank you...!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论