html 容器边缘未正确应用

r3i60tvu  于 2024-01-04  发布在  其他
关注(0)|答案(1)|浏览(227)

我在CSS中的margin属性上遇到了一些问题。我将每个section标签设置为不同的颜色,并将每个section中的所有内容包含在div中。我的意图是拥有一个处理内容边距的容器,同时保持section标签接触在一起。然而,下面我提供了我的代码和结果的屏幕截图。它似乎是边缘适用于部分和分隔它们,相对于内容容器。有人知道为什么这样写吗?还有什么更好的方法?

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="styles.css">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <body>
  11. <main class="card-container">
  12. <section class="sec-1">
  13. <div class="content-container">
  14. <h1>Join our community</h1>
  15. <h3>30-day, hassle-free money back guarantee</h3>
  16. <p>Gain access to our full library of tutorials along with expert code reviews.
  17. Perfect for any developers who are serious about honing their skills.</p>
  18. </div>
  19. </section>
  20. <section class="sec-2">
  21. <div class="content-container">
  22. <h2>Monthly Subscription</h2>
  23. <p>$29 per month</p>
  24. <p>Full access for less than $1 a day</p>
  25. <div class="btn-container">
  26. <button>Sign Up</button>
  27. </div>
  28. </div>
  29. </section>
  30. <section class="sec-3">
  31. <div class="content-container">
  32. <h2>Why us</h2>
  33. <ul>
  34. <li>Tutorials by industry experts</li>
  35. <li>Peer & expert code review</li>
  36. <li>Coding exercises</li>
  37. <li>Access to our GitHub repos</li>
  38. <li>Community forum</li>
  39. <li>Flashcard decks</li>
  40. <li>New videos every week</li>
  41. </ul>
  42. </div>
  43. </section>
  44. </main>
  45. </body>
  46. </html>

个字符


的数据
这里是期望的结果供参考:



我曾尝试在一个单独的项目中这样做,它的工作,但这个特定的项目是顽固的。

emeijp43

emeijp431#

使用填充,因为你必须提供内部空间,而不是外部空间:

**边距:**元素外部的空间,影响其相对于其他元素的位置。
**填充:**元素内部的空间,影响内容与元素边框之间的距离。

  1. .content-container {
  2. padding: 2rem;
  3. }

字符串

  1. @import url('https://fonts.googleapis.com/css2?family=Karla:wght@400;700&display=swap');
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. body {
  8. display: flex;
  9. justify-content: center;
  10. align-items: center;
  11. height: 100vh;
  12. font-family: 'Karla', sans-serif;
  13. color: #fff;
  14. background-color: hsl(204, 43%, 93%);
  15. }
  16. .card-container {
  17. margin: 0 2rem;
  18. }
  19. .content-container {
  20. padding: 2rem;
  21. }
  22. .sec-1 {
  23. background-color: #fff;
  24. color: black;
  25. }
  26. .sec-2 {
  27. background-color: hsl(179, 62%, 43%);
  28. }
  29. .sec-3 {
  30. background-color: hsla(179, 62%, 43%, 0.806);
  31. }
  32. ul {
  33. list-style: none;
  34. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="styles.css">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <body>
  11. <main class="card-container">
  12. <section class="sec-1">
  13. <div class="content-container">
  14. <h1>Join our community</h1>
  15. <h3>30-day, hassle-free money back guarantee</h3>
  16. <p>Gain access to our full library of tutorials along with expert code reviews.
  17. Perfect for any developers who are serious about honing their skills.</p>
  18. </div>
  19. </section>
  20. <section class="sec-2">
  21. <div class="content-container">
  22. <h2>Monthly Subscription</h2>
  23. <p>$29 per month</p>
  24. <p>Full access for less than $1 a day</p>
  25. <div class="btn-container">
  26. <button>Sign Up</button>
  27. </div>
  28. </div>
  29. </section>
  30. <section class="sec-3">
  31. <div class="content-container">
  32. <h2>Why us</h2>
  33. <ul>
  34. <li>Tutorials by industry experts</li>
  35. <li>Peer & expert code review</li>
  36. <li>Coding exercises</li>
  37. <li>Access to our GitHub repos</li>
  38. <li>Community forum</li>
  39. <li>Flashcard decks</li>
  40. <li>New videos every week</li>
  41. </ul>
  42. </div>
  43. </section>
  44. </main>
  45. </body>
  46. </html>
展开查看全部

相关问题