简体   繁体   English

CSS保证金热门问题

[英]CSS Margin Top Issues

Not sure why I am having such a hard time with this. 不知道为什么我这么难过。 Trying to add a 10px top margin to a 试图将10px的上边距添加到a

that is inside of a div. 那是在div里面。 But that does not add the gutter I want and just pushes the containing div down 10px. 但这并没有增加我想要的阴沟,只是将包含的div推下10px。

Styles 样式

#item{width:738px; height:168px; background-image:url(../images/item_bg.png);margin:0px auto;}
#description{width:314px; height:55px;}
#description p{font:12px arial; color:#666666;margin:10px 0 0 30px;}

HTML/PHP HTML / PHP

echo "<div id='item'>
           <div id='description'><p>{$row['description']}</p></div> 
     </div>";

If I use padding it works fine but I want to know why margin-top isn't working? 如果我使用填充它工作正常但我想知道为什么margin-top不起作用?

Try overflow:hidden in the surrounding div . 尝试overflow:hidden在周围的div

The reason why padding is working and margin is not might be the following: 填充工作原因和保证金不是以下原因:

Padding will introduce a gutter between the content and its surrounding element, whereas margin will introduce the gutter between the surrounding element and the nearest "solid element". Padding将在内容及其周围元素之间引入沟槽,而margin将引入周围元素与最近的“实体元素”之间的沟槽。

This is a bit confusing because we usually infer that margin will introduce a gutter between the surrounding elements and it's parent, but that's not true unless the parent element is "solid". 这有点令人困惑,因为我们通常推断margin将在周围元素和它的父元素之间引入一个阴沟,但除非父元素是“实体”,否则这不是真的。 The trick is to turn the parent element into something "solid" using an overflow: hidden 诀窍是使用overflow: hidden将父元素转换​​为“实体”

Quick example: 快速举例:

<style>
  #parent{
    position: relative;
    left: 100px;
    top: 100px;
    width: 100px;
    height: 100px;
    background: #DDD;
    /* Try adding here overflow:hidden */
  }
  #surrounding-element{
    margin-top: 50px;
    background: #AAA;
  }

</style>

<div id="parent">
  <div id="surrounding-element">content</div>
</div>

Looks fine here http://jsfiddle.net/huhu/hRH6k/ and the margin worked for 10px. 看起来很好http://jsfiddle.net/huhu/hRH6k/ ,保证金适用于10px。

Try margin:10px 0 0 30px !important ; 尝试margin:10px 0 0 30px !important ;

它的工作正常可能是你有另一个错误。你的代码工作正常。

看一下这篇文章 ,了解折边率的解释

如果你试图将div中的内容压低10px,你不能使用margin属性或margin-top,你需要使用填充。

You're experiencing Margin-collapsing which is part of the CSS Box model specification . 您正在经历Margin-collapsing,这是CSS Box模型规范的一部分 You probably just want to put some padding on the parent element, rather than a margin on the child. 您可能只想在父元素上添加一些填充,而不是在子元素上添加边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM