css:神秘的“边距”

发布于 2024-11-19 18:03:04 字数 1727 浏览 2 评论 0原文

每当我有两个水平并排的元素并指定了右和/或左填充和/或边距时,元素之间通常会在我指定的内容之上和之上存在空间。我希望有人能告诉我如何消除这个空间(没有像负边距这样的粗俗东西)。

请注意:我并不是在寻找替代的多列 CSS 布局技术。我知道那里有很多这样的问题,这个问题不仅仅是列布局问题。

以下是工作示例页面的标记和样式。 这是该页面的部分屏幕截图,其中显示了使用 Firebug 选择的左侧元素。所讨论的神秘空间位于右侧,并标有红色星号。没有包含重置样式,但我插入了 Eric Meyers 的重置,但它没有解决问题。

    <div id="side-a"> 
           <p>
        Lorem ipsum ....
        </p> 
    </div> 
    <div id="side-b"> 
           <p>
        Nunc dapibus....
        </p> 
    </div>

    <div id="website-footer"> 
           <ul id="legal-information"> 
                  <li>Copyright 2011</li> 
                  <li><a href="#">Privacy Policy</a></li> 
           </ul> 
    </div>

    div#side-a,
    div#side-b {
        display: inline-block;
        width: 200px;
        padding: 17px 17px 0;
    }

    div#side-a {
           vertical-align: top;
    }

    div#side-b {
           background: #999;
    }

    ul {
           padding-bottom: 17px;
           list-style: none outside none;
    }

    ul li {
           line-height: 17px;
           margin-left: 17px;
    }

    div#website-footer ul#legal-information {
           float: left;
    }

    div#website-footer ul#legal-information li {
           border-left: 1px solid #29443C;
           display: inline;
           margin: 17px 0;
           padding-left: 8px;
    }

    div#website-footer ul#legal-information li:first-child {
           border-left: medium none;
           padding: 0 8px 0 0;
    }

Whenever I have two elements side by side horizontally with right and/or left padding and/or margin specified, there is often space between the elements over and above what I've specified. I'm hoping someone can tell me how to eliminate that space (without something crufty like a negative margin).

Please note: I am not looking for alternative multi-column CSS layout techniques. I know there are loads of them out there and this issue is bigger than just a column layout issue.

Below is the markup and styles for a working example page. Here's a partial screenshot of that page that shows left elements selected with Firebug. The mysterious space in question is to the right and is marked with a red asterisk. There are no reset styles included but I've plugged in Eric Meyers' reset and it didn't solve the problem.

    <div id="side-a"> 
           <p>
        Lorem ipsum ....
        </p> 
    </div> 
    <div id="side-b"> 
           <p>
        Nunc dapibus....
        </p> 
    </div>

    <div id="website-footer"> 
           <ul id="legal-information"> 
                  <li>Copyright 2011</li> 
                  <li><a href="#">Privacy Policy</a></li> 
           </ul> 
    </div>

    div#side-a,
    div#side-b {
        display: inline-block;
        width: 200px;
        padding: 17px 17px 0;
    }

    div#side-a {
           vertical-align: top;
    }

    div#side-b {
           background: #999;
    }

    ul {
           padding-bottom: 17px;
           list-style: none outside none;
    }

    ul li {
           line-height: 17px;
           margin-left: 17px;
    }

    div#website-footer ul#legal-information {
           float: left;
    }

    div#website-footer ul#legal-information li {
           border-left: 1px solid #29443C;
           display: inline;
           margin: 17px 0;
           padding-left: 8px;
    }

    div#website-footer ul#legal-information li:first-child {
           border-left: medium none;
           padding: 0 8px 0 0;
    }

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

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

发布评论

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

评论(2

七色彩虹 2024-11-26 18:03:04

由于inline-block,这很自然。简单的解决方案是删除空格。

http://work.arounds.org/ issues/6/unwanted-white-space- Between-inline-block-elements/

还有其他基于 css 的解决方法,例如在正文上设置字体大小为 0,但据我所知,它们并不一致/可靠。但我可能是错的。

It's natural because of inline-block. Simple solution is to kill whitespace.

http://work.arounds.org/issue/6/unwanted-white-space-between-inline-block-elements/

There are other css based workarounds such as setting a font size of 0 on the body, but AFAIK they aren't as consistent/reliable. I could be wrong though.

听,心雨的声音 2024-11-26 18:03:04

我更改了此 Css:

div#side-a,
div#side-b {
  float: left
  width: 200px;
  padding: 17px 17px 0;
}

并在页脚中添加了 Css:

#website-footer {
    clear: both;
}

这解决了问题。

当我希望元素彼此相邻时,我通常会浮动它们。

I changed this Css:

div#side-a,
div#side-b {
  float: left
  width: 200px;
  padding: 17px 17px 0;
}

And added in Css for the footer:

#website-footer {
    clear: both;
}

And this fixed the issue.

I usually float the elements when I want them next to each other.

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