带有边框/轮廓/边距/填充的奇怪错误

发布于 2024-11-15 20:57:36 字数 879 浏览 7 评论 0原文

HTML(3x 次):

<div class="scream">
  <div class="author">
    <img src="avatars/dagrevis.png" alt="" title="daGrevis" />
  </div>
  <div class="data">
    <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry...</span>
  </div>
  <div class="clearer"></div>
</div>

CSS:

.scream {
  background: #F4F4F4;
  width: 944px; height: 48px;
}

.scream .author {
  float: left;
}

.scream .data {
  float: left;
}

.clearer { clear: both; }

这就是它在我的浏览器上的外观:

我的情况。

如您所见,< code>height 设置为 48px。图像大小也是 48px。为什么我没有在单独的行中看到每个 div.scream ?例如,如果我将 height 设置为 50px,则一切正常!在我看来,这是因为某种边框/轮廓/边距/填充毁了我的生活。不幸的是,对于 FireBug 我没有看到类似的东西......

HTML (3x times):

<div class="scream">
  <div class="author">
    <img src="avatars/dagrevis.png" alt="" title="daGrevis" />
  </div>
  <div class="data">
    <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry...</span>
  </div>
  <div class="clearer"></div>
</div>

CSS:

.scream {
  background: #F4F4F4;
  width: 944px; height: 48px;
}

.scream .author {
  float: left;
}

.scream .data {
  float: left;
}

.clearer { clear: both; }

This is how it looks on my browser:

My situation.

As you can see, height is set to 48px. Images size is 48px as well. How it comes that I don't see each div.scream in separate line? If I set height to 50px, for example, all works! In my opinion, it's because there are some-kind of border/outline/margin/padding that ruin my life. Unfortunately, with FireBug I don't see anything like that...

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

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

发布评论

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

评论(2

猫九 2024-11-22 20:57:36

在此处输入图像描述

默认情况下,图像的底部边缘与线框的基线(灰线)对齐。基线下方的空间(也称为“下降空间”)是导致问题的原因。有关更多详细信息,请参阅 Eric Meyer 撰写的这篇文章

要修复此问题,请添加以下规则:

.scream .author img {
    display: block;
}

或者添加以下规则:

.scream .author img {
    vertical-align: bottom;
}

enter image description here

The bottom edge of an image is aligned with the baseline (the grey line) of the line box by default. The space below the baseline (also called "descender space") is what's causing your problem. See this article by Eric Meyer for more details.

To fix it, add this rule:

.scream .author img {
    display: block;
}

Or this one:

.scream .author img {
    vertical-align: bottom;
}
蒗幽 2024-11-22 20:57:36

如果您稍微使用一下 DOM 检查器,您会发现 .author 的高度为 52px 左右。额外的 4px 似乎来自 line-height。将 line-height 设置为 0:

.scream .author {
    float: left;
    line-height: 0;
}

修复布局:http://jsfiddle.net/ ambigitude/8KzdD/

或者,您也可以将图像向左浮动:

.scream .author {
    float: left;
}
.scream .author img {
    float: left;
}

这将从本地流中删除图像并使 line-height 变得无关紧要:http://jsfiddle.net/ambigously/8KzdD/1/

另一种选择是放弃清理

并在外部

上使用 overflow:hidden
.scream {
    background: #F4F4F4;
    width: 944px;
    height: 48px;
    overflow: hidden;
}

这将留下

高度为 52px,但效果在视觉上并不明显:

If you play around with a DOM inspector for a bit, you'll find that .author is coming out with a height of 52px or so. The extra 4px appears to be coming from the line-height. Setting line-height to 0:

.scream .author {
    float: left;
    line-height: 0;
}

Fixes the layout: http://jsfiddle.net/ambiguous/8KzdD/

Or, you can float the image to the left as well:

.scream .author {
    float: left;
}
.scream .author img {
    float: left;
}

That will remove the image from the local flow and make the line-height irrelevant: http://jsfiddle.net/ambiguous/8KzdD/1/

Yet another option is to ditch the clearing <div> and use overflow:hidden on the outer <div>:

.scream {
    background: #F4F4F4;
    width: 944px;
    height: 48px;
    overflow: hidden;
}

This will leave the <div class="author"> with their 52px height but the effect won't be visually noticeable: http://jsfiddle.net/ambiguous/8KzdD/2/

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