如何修复我的网格单元格大小?

huangapple go评论70阅读模式
英文:

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(&quot;main&quot;);

const main = async () =&gt; {
  const characters = await fetchAllCharacters();
  const maxCells = 6;

  for (let i = 0; i &lt; Math.min(characters.length, maxCells); i++) {
    const character = characters[i];
    const episode = await fetchJsonAsync(character.episode[0]);
    mainEl.insertAdjacentHTML(&#39;beforeend&#39;, renderCharacterHTML(character, episode));
  }
};

const renderCharacterHTML = (character, episode) =&gt; `
  &lt;article class=&quot;character-card&quot;&gt;
    &lt;div class=&quot;image-container&quot;&gt;
      &lt;img src=&quot;${character.image}&quot; alt=&quot;Character&quot; height=&quot;100%&quot; width=&quot;100%&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;character-info&quot;&gt;
      &lt;div class=&quot;section&quot;&gt;
        &lt;h2 class=&quot;orangefy&quot;&gt;${character.name}&lt;/h2&gt;
        &lt;div class=&quot;status&quot;&gt;
          &lt;span class=&quot;status-dot ${getStatusColor(character.status)}&quot;&gt;&lt;/span&gt;
          &lt;span class=&quot;status&quot;&gt;${character.status} - ${character.species}&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;section&quot;&gt;
        &lt;span class=&quot;greytext&quot;&gt;Last known location:&lt;/span&gt;
        &lt;span class=&quot;orangefy&quot;&gt;${character.location.name}&lt;/span&gt;
      &lt;/div&gt;
      &lt;div class=&quot;section&quot;&gt;
        &lt;span class=&quot;greytext&quot;&gt;First seen in:&lt;/span&gt;
        &lt;span class=&quot;orangefy&quot;&gt;${episode.name}&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/article&gt;
`;

const getStatusColor = (status) =&gt; {
  switch (status.toLowerCase()) {
    case &#39;unknown&#39;:
      return &#39;grey&#39;;
    case &#39;dead&#39;:
      return &#39;red&#39;;
    case &#39;alive&#39;:
      return &#39;green&#39;;
    default:
      return &#39;&#39;;
  }
};

const fetchAllCharacters = async () =&gt; {
  const response = await fetchJsonAsync(&#39;https://rickandmortyapi.com/api/character&#39;);
  return response.results;
};

const fetchJsonAsync = async (url) =&gt; {
  try {
    let response = await fetch(url);
    if (!response.ok) throw new Error(response.statusText);
    return response.json();
  } catch (err) {
    console.error(&#39;Error fetching JSON:&#39;, err);
  }
};

main();

<!-- language: lang-css -->

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, &#39;Segoe UI&#39;, Roboto, Oxygen, Ubuntu, Cantarell, &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, 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 -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;Rick and Morty API&lt;/title&gt;
        &lt;meta charset=&quot;UTF-8&quot;&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
    &lt;/head&gt;

    &lt;body&gt;
        &lt;ul class=&quot;navbar&quot;&gt;
            &lt;li class=&quot;homebutton&quot;&gt;&lt;a href=&quot;landing.html&quot;&gt;&lt;img src=&quot;RMAPI-homepagebutton.png&quot; alt=&quot;Home&quot;&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;content-links&quot;&gt;&lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
        &lt;div class=&quot;top-container&quot;&gt;
            &lt;h1 class=&quot;banner-text&quot;&gt;The Rick and Morty API&lt;/h1&gt;
        &lt;/div&gt;
        &lt;div class=&quot;showcase&quot;&gt;
            &lt;div class=&quot;showcase-inner&quot;&gt;
                &lt;main&gt;&lt;/main&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;

<!-- 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...!

huangapple
  • 本文由 发表于 2023年6月13日 18:33:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定