为什么右下角的白色空白区域没有被填充为红色?

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

why the lower right white space is not filled by red

问题

我试图使用grid中的命名线来进行布局,但它不像我希望的那样工作,我知道我错在哪里,因为红色部分不想紧挨着页脚。

  1. <header class="header">
  2. HEADER
  3. </header>
  4. <div class="content">
  5. CONTENT
  6. </div>
  7. <section class="text">
  8. TEXT
  9. </section>
  10. <footer class="footer">
  11. FOOTER
  12. </footer>
  1. .container {
  2. height: 100vh;
  3. display: grid;
  4. grid-template-columns: [header-start content-start footer-start] 4fr [content-end footer-end section-start] 1fr [header-end section-end];
  5. grid-template-rows: [header-start] 1fr [hrader-end content-start] 3fr [content-end footer-start] 1fr [footer-end];
  6. gap: 10px;
  7. padding: 10px;
  8. }
  9. header {
  10. grid-column: header-start / header-end;
  11. background-color: blanchedalmond;
  12. }
  13. .content {
  14. grid-column: content-start / content-end;
  15. background-color: skyblue;
  16. }
  17. section {
  18. grid-column: section-start / section-end;
  19. background-color: coral;
  20. }
  21. footer {
  22. grid-column: footer-start / footer-end;
  23. background-color: lightseagreen;
  24. }
  25. /*居中*/
  26. .content,
  27. header,
  28. section,
  29. footer {
  30. display: flex;
  31. justify-content: center;
  32. align-items: center;
  33. }
英文:

I'm trying to use named lines with grid to do the layout but it doesn't work for me as it should I know where I went wrong because the red section doesn't want to go next to the footer

<!-- begin snippet: js hide: false console: true babel: false -->

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

  1. * {
  2. box-sizing: border-box;
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .container {
  7. height: 100vh;
  8. display: grid;
  9. grid-template-columns: [header-start content-start footer-start] 4fr [content-end footer-end section-start] 1fr [header-end section-end];
  10. grid-template-rows: [header-start] 1fr [hrader-end content-start] 3fr [content-end footer-start] 1fr [footer-end];
  11. gap: 10px;
  12. padding: 10px;
  13. }
  14. header {
  15. grid-column: header-start / header-end;
  16. background-color: blanchedalmond;
  17. }
  18. .content {
  19. grid-column: content-start / content-end;
  20. background-color: skyblue;
  21. }
  22. section {
  23. grid-column: section-start / section-end;
  24. background-color: coral;
  25. }
  26. footer {
  27. grid-column: footer-start / footer-end;
  28. background-color: lightseagreen;
  29. }
  30. /*centering*/
  31. .content,
  32. header,
  33. section,
  34. footer {
  35. display: flex;
  36. justify-content: center;
  37. align-items: center;
  38. }

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

  1. &lt;html lang=&quot;en&quot;&gt;
  2. &lt;head&gt;
  3. &lt;meta charset=&quot;UTF-8&quot;&gt;
  4. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  5. &lt;link rel=&quot;stylesheet&quot; href=&quot;grid2.css&quot;&gt;
  6. &lt;title&gt;grid-line&lt;/title&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;div class=&quot;container&quot;&gt;
  10. &lt;header class=&quot;header&quot;&gt;
  11. HEADER
  12. &lt;/header&gt;
  13. &lt;div class=&quot;content&quot;&gt;
  14. CONTENT
  15. &lt;/div&gt;
  16. &lt;section class=&quot;text&quot;&gt;
  17. TEXT
  18. &lt;/section&gt;
  19. &lt;footer class=&quot;footer&quot;&gt;
  20. FOOTER
  21. &lt;/footer&gt;
  22. &lt;/div&gt;
  23. &lt;/body&gt;
  24. &lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

Sure, here is the translated content:

在你的 section 中添加 grid-row-end: span 2;

编辑: 我误解了问题,所以我编辑了代码片段。

  1. * {
  2. box-sizing: border-box;
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .container {
  7. height: 100vh;
  8. display: grid;
  9. grid-template-columns: [header-start content-start footer-start] 4fr [content-end footer-end section-start] 1fr [header-end section-end];
  10. grid-template-rows: [header-start] 1fr [hrader-end content-start] 3fr [content-end footer-start] 1fr [footer-end];
  11. gap: 10px;
  12. padding: 10px;
  13. }
  14. header {
  15. grid-column: header-start / header-end;
  16. background-color: blanchedalmond;
  17. }
  18. .content {
  19. grid-column: content-start / content-end;
  20. background-color: skyblue;
  21. }
  22. section {
  23. grid-column: section-start / section-end;
  24. grid-row-end: span 2; /* &lt;---- 添加这部分 */
  25. background-color: coral;
  26. }
  27. footer {
  28. grid-column: footer-start / footer-end;
  29. background-color: lightseagreen;
  30. }
  31. /*居中*/
  32. .content,
  33. header,
  34. section,
  35. footer {
  36. display: flex;
  37. justify-content: center;
  38. align-items: center;
  39. }
  1. <div class="container">
  2. <header class="header">
  3. HEADER
  4. </header>
  5. <div class="content">
  6. CONTENT
  7. </div>
  8. <section class="text">
  9. TEXT
  10. </section>
  11. <footer class="footer">
  12. FOOTER
  13. </footer>
  14. </div>

I hope this helps! If you have any more translation requests or questions, feel free to ask.

英文:

Add grid-row-end: span 2; in your section

EDIT: I missunderstood the question, so I edit the snippet.

<!-- begin snippet: js hide: true console: true babel: false -->

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

  1. * {
  2. box-sizing: border-box;
  3. margin: 0;
  4. padding: 0;
  5. }
  6. .container {
  7. height: 100vh;
  8. display: grid;
  9. grid-template-columns: [header-start content-start footer-start] 4fr [content-end footer-end section-start] 1fr [header-end section-end];
  10. grid-template-rows: [header-start] 1fr [hrader-end content-start] 3fr [content-end footer-start] 1fr [footer-end];
  11. gap: 10px;
  12. padding: 10px;
  13. }
  14. header {
  15. grid-column: header-start / header-end;
  16. background-color: blanchedalmond;
  17. }
  18. .content {
  19. grid-column: content-start / content-end;
  20. background-color: skyblue;
  21. }
  22. section {
  23. grid-column: section-start / section-end;
  24. grid-row-end: span 2; /* &lt;---- ADD THIS PART */
  25. background-color: coral;
  26. }
  27. footer {
  28. grid-column: footer-start / footer-end;
  29. background-color: lightseagreen;
  30. }
  31. /*centering*/
  32. .content,
  33. header,
  34. section,
  35. footer {
  36. display: flex;
  37. justify-content: center;
  38. align-items: center;
  39. }

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

  1. &lt;div class=&quot;container&quot;&gt;
  2. &lt;header class=&quot;header&quot;&gt;
  3. HEADER
  4. &lt;/header&gt;
  5. &lt;div class=&quot;content&quot;&gt;
  6. CONTENT
  7. &lt;/div&gt;
  8. &lt;section class=&quot;text&quot;&gt;
  9. TEXT
  10. &lt;/section&gt;
  11. &lt;footer class=&quot;footer&quot;&gt;
  12. FOOTER
  13. &lt;/footer&gt;
  14. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译后的代码部分:

第一部分:

  1. 这是布局:
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;link rel=&quot;stylesheet&quot; href=&quot;grid2.css&quot;&gt;
  7. &lt;title&gt;grid-line&lt;/title&gt;
  8. &lt;style&gt;
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. .container {
  15. height: 100vh;
  16. display: grid;
  17. grid-template-columns:repeat(1, 1fr, 150px);
  18. grid-template-rows:repeat(3, 200px, 200px, 200px);
  19. grid-template-areas:
  20. &quot;header header header header&quot;
  21. &quot;content content content section&quot;
  22. &quot;content content content section&quot;
  23. &quot;footer footer footer footer&quot;;
  24. gap: 10px;
  25. padding: 10px;
  26. }
  27. header {
  28. grid-area:header;
  29. background-color: blanchedalmond;
  30. }
  31. .content {
  32. grid-area:content;
  33. background-color: skyblue;
  34. }
  35. section {
  36. grid-area:section;
  37. background-color: coral;
  38. }
  39. footer {
  40. grid-area:footer;
  41. background-color: lightseagreen;
  42. }
  43. /*居中*/
  44. .content,
  45. header,
  46. section,
  47. footer {
  48. display: flex;
  49. justify-content: center;
  50. align-items: center;
  51. }
  52. &lt;/style&gt;
  53. &lt;/head&gt;
  54. &lt;body&gt;
  55. &lt;div class=&quot;container&quot;&gt;
  56. &lt;header class=&quot;header&quot;&gt;
  57. HEADER
  58. &lt;/header&gt;
  59. &lt;div class=&quot;content&quot;&gt;
  60. CONTENT
  61. &lt;/div&gt;
  62. &lt;section class=&quot;text&quot;&gt;
  63. TEXT
  64. &lt;/section&gt;
  65. &lt;footer class=&quot;footer&quot;&gt;
  66. FOOTER
  67. &lt;/footer&gt;
  68. &lt;/div&gt;
  69. &lt;/body&gt;
  70. &lt;/html&gt;

第二部分:

  1. 这是我认为你期望的方式,以防万一,在这种情况下,section 出现在 content 旁边:
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;link rel=&quot;stylesheet&quot; href=&quot;grid2.css&quot;&gt;
  7. &lt;title&gt;grid-line&lt;/title&gt;
  8. &lt;style&gt;
  9. * {
  10. box-sizing: border-box;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. .container {
  15. height: 100vh;
  16. display: grid;
  17. grid-template-columns:repeat(1, 1fr, 150px);
  18. grid-template-rows:repeat(3, 200px, 200px, 200px);
  19. grid-template-areas:
  20. &quot;header header header header&quot;
  21. &quot;content content content section&quot;
  22. &quot;content content content section&quot;
  23. &quot;footer footer footer section&quot;;
  24. gap: 10px;
  25. padding: 10px;
  26. }
  27. header {
  28. grid-area:header;
  29. background-color: blanchedalmond;
  30. }
  31. .content {
  32. grid-area:content;
  33. background-color: skyblue;
  34. }
  35. section {
  36. grid-area:section;
  37. background-color: coral;
  38. }
  39. footer {
  40. grid-area:footer;
  41. background-color: lightseagreen;
  42. }
  43. /*居中*/
  44. .content,
  45. header,
  46. section,
  47. footer {
  48. display: flex;
  49. justify-content: center;
  50. align-items: center;
  51. }
  52. &lt;/style&gt;
  53. &lt;/head&gt;
  54. &lt;body&gt;
  55. &lt;div class=&quot;container&quot;&gt;
  56. &lt;header class=&quot;header&quot;&gt;
  57. HEADER
  58. &lt;/header&gt;
  59. &lt;div class=&quot;content&quot;&gt;
  60. CONTENT
  61. &lt;/div&gt;
  62. &lt;section class=&quot;text&quot;&gt;
  63. TEXT
  64. &lt;/section&gt;
  65. &lt;footer class=&quot;footer&quot;&gt;
  66. FOOTER
  67. &lt;/footer&gt;
  68. &lt;/div&gt;
  69. &lt;/body&gt;
  70. &lt;/html&gt;

这是一种简化使用 display grid 属性的方式。

英文:

This is the layout:
<html lang="en">

  1. &lt;head&gt;
  2. &lt;meta charset=&quot;UTF-8&quot;&gt;
  3. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  4. &lt;link rel=&quot;stylesheet&quot; href=&quot;grid2.css&quot;&gt;
  5. &lt;title&gt;grid-line&lt;/title&gt;
  6. &lt;style&gt;
  7. * {
  8. box-sizing: border-box;
  9. margin: 0;
  10. padding: 0;
  11. }
  12. .container {
  13. height: 100vh;
  14. display: grid;
  15. grid-template-columns:repeat(1, 1fr, 150px);
  16. grid-template-rows:repeat(3, 200px, 200px, 200px);
  17. grid-template-areas:
  18. &quot;header header header header&quot;
  19. &quot;content content content section&quot;
  20. &quot;content content content section&quot;
  21. &quot;footer footer footer footer&quot;;
  22. gap: 10px;
  23. padding: 10px;
  24. }
  25. header {
  26. grid-area:header;
  27. background-color: blanchedalmond;
  28. }
  29. .content {
  30. grid-area:content;
  31. background-color: skyblue;
  32. }
  33. section {
  34. grid-area:section;
  35. background-color: coral;
  36. }
  37. footer {
  38. grid-area:footer;
  39. background-color: lightseagreen;
  40. }
  41. /*centering*/
  42. .content,
  43. header,
  44. section,
  45. footer {
  46. display: flex;
  47. justify-content: center;
  48. align-items: center;
  49. }
  50. &lt;/style&gt;
  51. &lt;/head&gt;
  52. &lt;body&gt;
  53. &lt;div class=&quot;container&quot;&gt;
  54. &lt;header class=&quot;header&quot;&gt;
  55. HEADER
  56. &lt;/header&gt;
  57. &lt;div class=&quot;content&quot;&gt;
  58. CONTENT
  59. &lt;/div&gt;
  60. &lt;section class=&quot;text&quot;&gt;
  61. TEXT
  62. &lt;/section&gt;
  63. &lt;footer class=&quot;footer&quot;&gt;
  64. FOOTER
  65. &lt;/footer&gt;
  66. &lt;/div&gt;
  67. &lt;/body&gt;
  68. &lt;/html&gt;

This is what I thought you were expecting, so just in case, in this one the section spawns next to the content:
<html lang="en">

  1. &lt;head&gt;
  2. &lt;meta charset=&quot;UTF-8&quot;&gt;
  3. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  4. &lt;link rel=&quot;stylesheet&quot; href=&quot;grid2.css&quot;&gt;
  5. &lt;title&gt;grid-line&lt;/title&gt;
  6. &lt;style&gt;
  7. * {
  8. box-sizing: border-box;
  9. margin: 0;
  10. padding: 0;
  11. }
  12. .container {
  13. height: 100vh;
  14. display: grid;
  15. grid-template-columns:repeat(1, 1fr, 150px);
  16. grid-template-rows:repeat(3, 200px, 200px, 200px);
  17. grid-template-areas:
  18. &quot;header header header header&quot;
  19. &quot;content content content section&quot;
  20. &quot;content content content section&quot;
  21. &quot;footer footer footer section&quot;;
  22. gap: 10px;
  23. padding: 10px;
  24. }
  25. header {
  26. grid-area:header;
  27. background-color: blanchedalmond;
  28. }
  29. .content {
  30. grid-area:content;
  31. background-color: skyblue;
  32. }
  33. section {
  34. grid-area:section;
  35. background-color: coral;
  36. }
  37. footer {
  38. grid-area:footer;
  39. background-color: lightseagreen;
  40. }
  41. /*centering*/
  42. .content,
  43. header,
  44. section,
  45. footer {
  46. display: flex;
  47. justify-content: center;
  48. align-items: center;
  49. }
  50. &lt;/style&gt;
  51. &lt;/head&gt;
  52. &lt;body&gt;
  53. &lt;div class=&quot;container&quot;&gt;
  54. &lt;header class=&quot;header&quot;&gt;
  55. HEADER
  56. &lt;/header&gt;
  57. &lt;div class=&quot;content&quot;&gt;
  58. CONTENT
  59. &lt;/div&gt;
  60. &lt;section class=&quot;text&quot;&gt;
  61. TEXT
  62. &lt;/section&gt;
  63. &lt;footer class=&quot;footer&quot;&gt;
  64. FOOTER
  65. &lt;/footer&gt;
  66. &lt;/div&gt;
  67. &lt;/body&gt;
  68. &lt;/html&gt;

This is a simplified way of using the display grid property.

huangapple
  • 本文由 发表于 2023年7月13日 14:35:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676529.html
匿名

发表评论

匿名网友

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

确定