【问题标题】:Why is the margin not fitting into the div?为什么边距不适合div?
【发布时间】:2021-06-23 21:53:22
【问题描述】:

我的按钮边距不适合我的 div。这是相关的 HTML 和 CSS。

.card {
  justify-content: flex-start;
  box-shadow: 0 4px 8px 0 rgb(0 0 0 / 20%);
  margin: 8px;
}
.card {
  margin-bottom: 16px;
  padding: 0px 8px;
  float: left;
}
.button {
  border: none;
  padding: 8px;
  color: white;
  background-color: black;
  text-align: center;
  cursor: pointer;
  width: 100%;
}
.button:hover {
  background-color: #555;
}
.title {
  color: grey;
}
.container {
  padding: 0px 16px;
}
.card > img {
  width: 100%;
}
    <div class="ccard">
        <div class="card">
            <img src="https://pbs.twimg.com/profile_images/1115280588322295808/N1mHLfHy_400x400.png" alt="Jane">
            <div class="container">
                <h2>Jane Doe</h2>
                <p class="title">CEO & Founder</p>
                <p>Jane has had 9 years of experience in the tech industry and knows it like the back of her hand. She's very warm and welcoming towards new customers.</p>
                <p>janedoe@cabbagedude.com</p>
                <p>
                    <button class="button">Contact</button>
                </p>
            </div>
        </div>
    </div>

它以前一直对我有用,但现在不起作用。除了在卡片周围添加类 ccard 之外,我也没有更改任何内容。

这就是我的样子。

【问题讨论】:

  • 您的 css 中有 .ccard,但 html 中有 class="card"
  • 这能回答你的问题吗? How to disable margin-collapsing?
  • pistevw,对不起,我忘了添加一些东西

标签: html css margin


【解决方案1】:

您需要将box-sizing: border-box; 添加到所有相关元素,以便在给定的include 边框和填充(我想您的意思是那些,因为按钮上没有任何边距) width,否则会添加到宽度上,如果宽度为100%会导致溢出。

【讨论】:

  • 我的意思是页边距
  • 在这种情况下,@CBroe 正确地将您指向边距折叠。如果给父元素(.container)添加一个 1px 的 padding-bottom,p 标签上的 margin-bottom 就不会再折叠了。
  • 谢谢!这是我需要的,对此感到抱歉
猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 2020-05-17
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
相关资源
最近更新 更多