【问题标题】:Background-color of a div fitting to content, rather than borders, due to margin collapsing?由于边距折叠,div的背景颜色适合内容而不是边框​​?
【发布时间】:2014-05-27 05:26:27
【问题描述】:

我想在表单下放置一个 div。我想知道为什么当 div 有显示块时,背景不会填满 div 的边框(带有背景颜色)但适合内容。边距 0,填充 0。 我找到了 2 个困扰我但我不明白的解决方案:如果我向 div 添加可见边框,则背景会一直填充到表单中。如果我将 div 的显示属性更改为内联阻止 div 背景填充到顶部。我要问的是为什么会发生这种情况,也许是解决它的更好方法。我是 CSS 初学者,谢谢。

    _______________________
    |      form            |
    |______________________|
     ----------------------- border of div sticking to form as wanted

  unwanted white space between border and content due to color not showing

    _______________________
    |     div content      |       
    |                      |
    |______________________|

     div{
         background-color: #3B3B3B;
         width:652px;
         margin:0;
         padding:0;
         display:block;
        }
    .formStyle {
               width: 652px;
               height: 64px;
               background: linear-gradient(-15deg, transparent 0%, #3b3b3b 80%), linear-gradient(15deg, transparent 0%, #3b3b3b 80%), linear-gradient(to bottom, #3b3b3b 70%, #000000 90%);
               margin:0;
               }

根据要求的 HTML:

            <form action="" class="formStyle">
                <img class="computerImage">
                <label>members login:</label>
                <input name="Your name" type="text" id="yourname" value="Your name">
                <input name="emailid" type="text" id="emailid" value="Email Id">
                <input name="" type="submit" class="button" value="GO">
            </form>
            <div>
                <h2>about total management?</h2>

                <p>Total Management is a free, tableless, W3C-compliant web design layout by Template World. This template has been
                    tested and proven compatible with all major browser environments and operating systems. You are free to modify
                    the design to suit your tastes in any way you like.</p>

                <p>We only ask you to not remove <span>"Design by Template World"</span> and the link http://www.templateworld.com
                    from the footer of the template.</p>

                <p class="lasttext">If you are interested in seeing more of our free web template designs feel free to visit our
                    website, Template World. We intend to add at least 25 new free templates in the coming month.</p>
            </div>
            </body>

【问题讨论】:

  • 请发布您的 HTML 代码。
  • 现在可以在底部看到html了,我加了。

标签: html block background-color css


【解决方案1】:

你的样式表有重置吗?

* {
    margin: 0;
    padding: 0;
  }

即使您的 div 有 margin:0padding:0,它也不适用于 div 内的 ph2,因此您有空格。

【讨论】:

  • 不需要为所有元素设置margin:0,只需为您的最后一句中提到的h2设置它。
  • @King King,是的,但这是一个标准的 CSS,以避免在他的项目的其余部分出现此类问题。
  • 它解决了 OP 的问题,但它可能无法清楚准确地解释 OP 的问题
  • 问题的实际原因是所谓的margin collapsing,在这种情况下margin-collapsing发生在孩子(h2)和父母( div)。因为div没有上边框,所以h2的边距会随着div的边距折叠,这样最大边距就会变成父div的边距,在这种情况下就是h2 的边距。这是演示它的演示jsfiddle.net/viphalongpro/yEWBM/10 如果我们为父级div 指定边框,则在这种情况下不会出现边距折叠。
  • 感谢两位回答我的问题,你们的答案最好放在一起 - 介绍,然后是更好的理论解释。
猜你喜欢
  • 2013-10-24
  • 2013-09-21
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多