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

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

How do I fix the sizing of my grid cells?

问题

我正在尝试复制这个网站 https://rickandmortyapi.com/,我正在使用网格布局使我的版本更具响应性。当我将grid-template-columns设置为repeat(auto-fit, minmax(589.19, 1fr))时,网格单元格会在水平方向延伸到页面的末尾,尽管我设置了minmax。我这样做的原因是要根据浏览器窗口的大小来改变每行的单元格数量。

  1. main {
  2. display: grid;
  3. grid-template-columns: repeat(auto-fit, minmax(589.19px, 1fr));
  4. gap: 0;
  5. overflow-x: hidden;
  6. }

这是您提供的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 -->

  1. const mainEl = document.querySelector(&quot;main&quot;);
  2. const main = async () =&gt; {
  3. const characters = await fetchAllCharacters();
  4. const maxCells = 6;
  5. for (let i = 0; i &lt; Math.min(characters.length, maxCells); i++) {
  6. const character = characters[i];
  7. const episode = await fetchJsonAsync(character.episode[0]);
  8. mainEl.insertAdjacentHTML(&#39;beforeend&#39;, renderCharacterHTML(character, episode));
  9. }
  10. };
  11. const renderCharacterHTML = (character, episode) =&gt; `
  12. &lt;article class=&quot;character-card&quot;&gt;
  13. &lt;div class=&quot;image-container&quot;&gt;
  14. &lt;img src=&quot;${character.image}&quot; alt=&quot;Character&quot; height=&quot;100%&quot; width=&quot;100%&quot;&gt;
  15. &lt;/div&gt;
  16. &lt;div class=&quot;character-info&quot;&gt;
  17. &lt;div class=&quot;section&quot;&gt;
  18. &lt;h2 class=&quot;orangefy&quot;&gt;${character.name}&lt;/h2&gt;
  19. &lt;div class=&quot;status&quot;&gt;
  20. &lt;span class=&quot;status-dot ${getStatusColor(character.status)}&quot;&gt;&lt;/span&gt;
  21. &lt;span class=&quot;status&quot;&gt;${character.status} - ${character.species}&lt;/span&gt;
  22. &lt;/div&gt;
  23. &lt;/div&gt;
  24. &lt;div class=&quot;section&quot;&gt;
  25. &lt;span class=&quot;greytext&quot;&gt;Last known location:&lt;/span&gt;
  26. &lt;span class=&quot;orangefy&quot;&gt;${character.location.name}&lt;/span&gt;
  27. &lt;/div&gt;
  28. &lt;div class=&quot;section&quot;&gt;
  29. &lt;span class=&quot;greytext&quot;&gt;First seen in:&lt;/span&gt;
  30. &lt;span class=&quot;orangefy&quot;&gt;${episode.name}&lt;/span&gt;
  31. &lt;/div&gt;
  32. &lt;/div&gt;
  33. &lt;/article&gt;
  34. `;
  35. const getStatusColor = (status) =&gt; {
  36. switch (status.toLowerCase()) {
  37. case &#39;unknown&#39;:
  38. return &#39;grey&#39;;
  39. case &#39;dead&#39;:
  40. return &#39;red&#39;;
  41. case &#39;alive&#39;:
  42. return &#39;green&#39;;
  43. default:
  44. return &#39;&#39;;
  45. }
  46. };
  47. const fetchAllCharacters = async () =&gt; {
  48. const response = await fetchJsonAsync(&#39;https://rickandmortyapi.com/api/character&#39;);
  49. return response.results;
  50. };
  51. const fetchJsonAsync = async (url) =&gt; {
  52. try {
  53. let response = await fetch(url);
  54. if (!response.ok) throw new Error(response.statusText);
  55. return response.json();
  56. } catch (err) {
  57. console.error(&#39;Error fetching JSON:&#39;, err);
  58. }
  59. };
  60. main();

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

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. 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;
  6. }
  7. .top-container{
  8. position: relative;
  9. height: 465px;
  10. padding-bottom: 0;
  11. }
  12. .showcase {
  13. background-color: rgb(39,43,51);
  14. }
  15. .showcase-inner {
  16. margin: 0;
  17. padding-top: 80px;
  18. padding-bottom: 80px;
  19. padding-left: 10px;
  20. padding-right: 10px;
  21. }
  22. .footer {
  23. background-color: rgb(32,35,41);
  24. padding-top: 50px;
  25. padding-bottom: 21px;
  26. }
  27. h1{
  28. font-size: 100px;
  29. text-align: center;
  30. font-weight: 1000;
  31. color: rgb(32,35,41);
  32. }
  33. h2{
  34. font-weight: 1000;
  35. font-size: x-larges;
  36. }
  37. main {
  38. display: grid;
  39. grid-template-columns: repeat(auto-fit, minmax(589.19, 1fr));
  40. gap: 0;
  41. overflow-x: hidden;
  42. }
  43. .image-container{
  44. display: flex;
  45. }
  46. .image-container img{
  47. width: 229.2px;
  48. height: 220px;
  49. border-radius: 10px 0px 0px 10px;
  50. }
  51. article{
  52. margin-left: 5px;
  53. margin-right: 5px;
  54. }
  55. .character-card{
  56. display: flex;
  57. margin: 13.5px;
  58. background-color: rgb(60,62,68);
  59. border-radius: 10px;
  60. box-shadow: 0 0 10px rgba(37, 37, 37, 0.5);
  61. }
  62. .character-info{
  63. display: flex;
  64. flex-direction: column;
  65. padding: 13.5px;
  66. position: relative;
  67. }
  68. .section{
  69. display: flex;
  70. flex-direction: column;
  71. width: 330px;
  72. height: 64.33px;
  73. color: rgb(245,245,245);
  74. }
  75. .greytext{
  76. color: rgb(158,158,158);
  77. padding-top: 5px;
  78. padding-bottom: 10px;
  79. }
  80. .navbar {
  81. background-color: #ffffff;
  82. list-style-type: none;
  83. margin: 0;
  84. padding: 0;
  85. overflow: hidden;
  86. display: flex;
  87. justify-content: space-between;
  88. }
  89. .navbar li {
  90. float: left;
  91. }
  92. .navbar li a {
  93. display: block;
  94. color: #333;
  95. text-align: center;
  96. padding: 14px 16px;
  97. text-decoration: none;
  98. transition: color 0.2s;
  99. }
  100. .content-links{
  101. margin-left: auto;
  102. padding: 10px;
  103. margin-right: 10px;
  104. font-size: 20px;
  105. font-weight: 750;
  106. }
  107. .content-links a:hover{
  108. color: rgb(255,152,0);
  109. }
  110. .homebutton{
  111. margin-right: auto;
  112. }
  113. .homebutton img{
  114. width: 50px;
  115. height: 50px;
  116. }
  117. .status {
  118. display: flex;
  119. align-items: center;
  120. font-weight: 600;
  121. }
  122. .status-dot {
  123. width: 8px;
  124. height: 8px;
  125. border-radius: 50%;
  126. display: inline-block;
  127. margin-right: 5px;
  128. }
  129. .status-dot.grey {
  130. background-color: rgb(158,158,158);
  131. }
  132. .status-dot.red {
  133. background-color: rgb(214,61,46);
  134. }
  135. .status-dot.green {
  136. background-color: rgb(85,204,68);
  137. }
  138. .about-content{
  139. display: flex;
  140. flex-direction: column;
  141. padding: 5px;
  142. padding-bottom: 100px;
  143. margin-left: 500px;
  144. margin-right: 500px;
  145. font-size: 110%;
  146. color: rgb(66,66,66);
  147. }
  148. .abouth2 {
  149. font-size: 35px;
  150. font-weight: 800;
  151. margin-bottom: 30px;
  152. color: rgb(32,35,41);
  153. }
  154. .abouth3 {
  155. font-size: 25px;
  156. font-weight: 800;
  157. margin-top: 10px;
  158. margin-bottom: 10px;
  159. color: rgb(32,35,41);
  160. }
  161. .ablink {
  162. text-decoration-color: rgb(255,152,0);
  163. text-decoration-thickness: 2px;
  164. text-underline-offset: 4px;
  165. color: rgb(66,66,66);
  166. }
  167. .ablink:hover {
  168. text-decoration: none;
  169. color: rgb(255,152,0);
  170. transition: color 0.2s;
  171. }
  172. .footer-info {
  173. display: flex;
  174. justify-content: center;
  175. list-style: none;
  176. padding-top: 40px;
  177. margin: 0;
  178. font-size: 14px;
  179. font-weight: 750;
  180. color: rgb(158, 158, 158);
  181. }
  182. .footer-info li {
  183. margin-right: 20px;
  184. }
  185. .socials {
  186. display: flex;
  187. justify-content: center;
  188. align-items: center;
  189. padding-top: 2px;
  190. padding-bottom: 2px;
  191. }
  192. .socials a {
  193. margin: 14px;
  194. }
  195. .socials a:hover svg path {
  196. fill: rgb(255, 152, 0);
  197. }
  198. .sign {
  199. color: rgb(158, 158, 158);
  200. text-align: center;
  201. font-size: 13px;
  202. padding-top: 15px;
  203. padding-bottom: 50px;
  204. }
  205. .footer-link {
  206. color:rgb(255, 255, 255);
  207. font-weight: 700;
  208. text-decoration-color: rgb(255,152,0);
  209. text-decoration-thickness: 2px;
  210. text-underline-offset: 4px;
  211. }
  212. .footer-link:hover {
  213. text-decoration: none;
  214. color: rgb(255,152,0);
  215. transition: color 0.2s;
  216. }
  217. .banner {
  218. position: absolute;
  219. left: 50%;
  220. transform: translateX(-50%);
  221. bottom: 0;
  222. }
  223. .banner svg {
  224. width: 480px;
  225. height: auto;
  226. }
  227. .banner-text{
  228. display: flex;
  229. justify-content: center;
  230. align-items:center;
  231. z-index: 10;
  232. position: relative;
  233. top: 150px;
  234. }
  235. .poweredby {
  236. display: flex;
  237. justify-content: center;
  238. align-items: center;
  239. padding: 20px;
  240. padding-top: 30px;
  241. }
  242. .poweredby-icon {
  243. padding-left: 18px;
  244. padding-right: 18px;
  245. }
  246. .server-status {
  247. display: flex;
  248. align-items: center;
  249. text-justify: center;
  250. justify-content: center;
  251. font-weight: 700;
  252. font-size: 14px;
  253. padding-top: 10px;
  254. }
  255. .server-status-dot {
  256. display: inline-block;
  257. width: 9px;
  258. height: 9px;
  259. border-radius: 50%;
  260. background-color: rgb(85,204,68);
  261. margin-left: 10px;
  262. }
  263. .status-link {
  264. text-decoration: none;
  265. color: rgb(158, 158, 158);
  266. }
  267. .status-link:hover {
  268. color: rgb(255,152,0);
  269. transition: color 0.2s;
  270. }
  271. .orangefy:hover {
  272. color: rgb(255,152,0);
  273. }

<!-- language: lang-html -->

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Rick and Morty API&lt;/title&gt;
  5. &lt;meta charset=&quot;UTF-8&quot;&gt;
  6. &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;ul class=&quot;navbar&quot;&gt;
  10. &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;
  11. &lt;li class=&quot;content-links&quot;&gt;&lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
  12. &lt;/ul&gt;
  13. &lt;div class=&quot;top-container&quot;&gt;
  14. &lt;h1 class=&quot;banner-text&quot;&gt;The Rick and Morty API&lt;/h1&gt;
  15. &lt;/div&gt;
  16. &lt;div class=&quot;showcase&quot;&gt;
  17. &lt;div class=&quot;showcase-inner&quot;&gt;
  18. &lt;main&gt;&lt;/main&gt;
  19. &lt;/div&gt;
  20. &lt;/div&gt;
  21. &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
  22. &lt;/body&gt;
  23. &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.

  1. main {
  2. display: grid;
  3. grid-template-columns: repeat(auto-fit, minmax(589px, 1fr));
  4. gap: 0;
  5. overflow-x: hidden;
  6. }

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:

确定