HTML 边距推送其他元素

发布于 2024-09-17 19:14:23 字数 984 浏览 7 评论 0原文

有人可以回答我吗,为什么当我为

代码:

<!DOCTYPE html>
<html lang="pt-br">
  <head>
    <title>Olá Mundo!</title>
    <style>
      /* CSS RESET HERE */ ( http://html5doctor.com/html-5-reset-stylesheet/ )
      body { margin:0; }
      #container { width:1000px; min-height:100%; height:auto; margin:0 auto; }
      #header { width:100%; height:160px; background-color:#FF0; }
      #logo { width:150px; height:150px; margin:10px 0 0 10px; background-color:#F0F; }
    </style>
  </head>
  <body>

    <div id="container">
      <div id="header">

        <div id="logo">
          <h1>Minha logo!</h1>
          <h2>meu slogan ...</h2>
        </div>

      </div>

  </body>  
</html>

could someone answer me, WHY when I set a margin-top to my <div id="logo">, all the others divs are pushed down. And why if a set float:left to my <div id="logo">, everything works fine.

Code:

<!DOCTYPE html>
<html lang="pt-br">
  <head>
    <title>Olá Mundo!</title>
    <style>
      /* CSS RESET HERE */ ( http://html5doctor.com/html-5-reset-stylesheet/ )
      body { margin:0; }
      #container { width:1000px; min-height:100%; height:auto; margin:0 auto; }
      #header { width:100%; height:160px; background-color:#FF0; }
      #logo { width:150px; height:150px; margin:10px 0 0 10px; background-color:#F0F; }
    </style>
  </head>
  <body>

    <div id="container">
      <div id="header">

        <div id="logo">
          <h1>Minha logo!</h1>
          <h2>meu slogan ...</h2>
        </div>

      </div>

  </body>  
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2024-09-24 19:14:23

这是由边距崩溃引起的。

正常文档流

p {
   margin: 1em 0;
}

它们之间只有 1em 的边距,而不是 2em(如果边距没有设置,这将是结果)不会崩溃)。

浮动修复

当您浮动

修复

在您的情况下,修复边距折叠的其他选项是向

It's caused by margin collapse.

Normal Document Flow

In the case where <div id="logo"> is not floated, its top margin is actually sticking out of the top of its containing element, which pushes everything down. The reason for this behavior (as the article above points out) is so that if you have a series of paragraphs with the following CSS:

p {
   margin: 1em 0;
}

They will only have 1em of margin between them, instead of 2em (which would be the result if margins didn't collapse).

Float Fix

When you float <div id="logo"> it takes it out of the normal document flow, which means its top margin no longer collapses with its parent's margin.

Fixes

Other options to fix margin collapse in your case would be to add 1px of top/bottom padding or a border to your <div id="header">.

落在眉间の轻吻 2024-09-24 19:14:23

你的 h1 有一个默认边距(在我电脑上的 safari 上,它是 0.67em)。这就是导致一切都被推倒的原因。

尝试:

h1{margin:0;}

会解决所有问题。

当您浮动时它起作用的原因是浮动会将徽标元素带出正常流程,因此它不会影响其父元素的定位。

另外,我总是使用重置 CSS 来避免这种情况。 YUI 的重置效果很好。

Your h1 has a default margin on them (on safari on my computer it's .67em). This is what's causing everything to be pushed down.

try:

h1{margin:0;}

Will fix everything.

The reason it works when you float is that floating it takes the logo element out of the normal flow, so it doesn't affect its parents' positioning.

Also, I ALWAYS use a reset css to avoid this. YUI's reset works very well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文