内部出现未知空白没有填充或边距

发布于 2024-09-29 12:10:52 字数 1329 浏览 2 评论 0原文

我在构建的设计中遇到了一个奇怪的空白问题。

我创建了一个

来包含投票元素 - 它包含一个赞成按钮、反对按钮和投票总数,每个按钮都位于自己的
元素中,并且使用 作为按钮。

来源:

<div class="votebox">
  <div class="vote"><img src="upvote.png" /></div>
  <div class="votetotal">15</div>
  <div class="vote"><img src="downvote.png" /></div>
</div>

在我的 CSS 中的迷你重置中,

元素都被定义为在没有边距或填充的情况下显示,并且 FireBug 确认了这些特定的元素没有边距或填充,但我看到 元素的底部与其各自包含 ` 元素的底部之间添加了空格。

我添加了以下 CSS 来显示每个元素周围的边框:

.votebox * {
  border: 1px #000 solid;
}

这就是它在 Firefox 3.6 中的显示方式(是的,这些是 StackOverflow 投票图像。我暂时使用它们作为占位符):


现在,这个问题的明显答案是简单地设置“投票”类以具有明确的图像高度(我会这样做,甚至可能选择 CSS 精灵而不是 s),但我更感兴趣的是了解为什么这些元素以这种方式显示(毕竟这应该是一个自学项目)。

有人能为我解释一下吗?


编辑:Steve H 向我指出,我应该使用轮廓而不是边框​​来显示元素的外边缘。我已经进行了更改,并且还分隔了 CSS 中的元素,以便它们各自显示为不同的颜色。

新的轮廓如下所示:


正如你所看到的,这个问题与我想象的有点不同。看起来图像下方有一些空白,但底部图像似乎稍微渲染在其包含的

之外,这一事实使情况变得更加复杂。这对我来说似乎很奇怪。

I'm having an odd issue with whitespace in a design I'm building.

I created a <div> to contain voting elements - it holds an upvote button, downvote button, and vote total, each inside their own <div> element, and using <img> for the buttons.

Source:

<div class="votebox">
  <div class="vote"><img src="upvote.png" /></div>
  <div class="votetotal">15</div>
  <div class="vote"><img src="downvote.png" /></div>
</div>

In the mini-reset in my CSS, both <div> and <img> elements are defined to display without margins or padding, and FireBug confirms these specific elements have no margins or padding, but I'm seeing whitespace being added between the bottoms of the <img> elements and the bottom of their respective containing ` elements.

I added the following CSS to display a border around each element:

.votebox * {
  border: 1px #000 solid;
}

and this is how it displayed in Firefox 3.6 (yes, those are StackOverflow vote images.. I'm using them as placeholders for the moment):


Now, the obvious answer to this problem is to simply set the "vote" class to have an explicit height of the images (and I will do this, possibly even opting for CSS sprites over <img>s), but I'm much more interested in learning why these elements are displaying in this manner (this is supposed to be a self-teaching project after all).

Can anyone shed some light on this for me?


Edit: Steve H has pointed out to me that I should be using outline, rather than border, to show the outer edges of elements. I've made that change, and also separated the elements in CSS so they each display as a different colour.

The new outline looks like this:


As you can see, the issue is a bit different than I thought. It looks like there is some whitespace below the image, but this is compounded by the fact that the bottom image seems to be rendered slightly outside its containing <div>. This seems weird to me.

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

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

发布评论

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

评论(3

临走之时 2024-10-06 12:10:52

HTML 中的图像是内联元素,默认情况下放置在字体基线上,因此您看到的可能是下行部分的空间。解决这个问题的常用方法是将它们设置为 display: blockvertical-align: Bottom

编辑:顺便说一句,您可以像任何其他元素一样使用 CSS 格式化图像,因此您很可能可以在图像周围删除额外的 div

Images in HTML are inline elements and are by default placed on the font base line, so what you are seeing is probably the space for the descenders. The usual way around this is either setting them to display: block or vertical-align: bottom.

EDIT: BTW, you can format images with CSS just like any other element, so you can mostly likely drop the extra divs around the images.

虚拟世界 2024-10-06 12:10:52

您使用严格的文档类型,所以

.votebox img{display:block;}

应该可以解决问题

Your using a strict doctype, so

.votebox img{display:block;}

Should do the trick

无声静候 2024-10-06 12:10:52

取决于您使用的文档类型。如果您使用 Transitional ( )它将按照您想要的方式显示。

过渡示例:http://jsbin.com/icesi

严格示例:http://jsbin.com/icesi/2

以下是可能帮助您理解问题的相关声明

这是另一个例子。你可能有
发现设置 font-size:medium
导致不同的字体大小
探索者与导航者。出现这种情况
因为互联网的方式
Explorer for Windows 团队解释
CSS 规范的意图。
为了与
Windows 版本,Internet Explorer for
Macintosh 模仿了它的行为
4.x系列。如果你把IE5/Mac
进入 bugwards 兼容模式,它
将继续处理 font-size:
与 IE4.x/Win 一样的介质。 严格
但是,它会充当导航器
是的,这实际上是正确的
根据 W3C 的解释。

来源:http://oreilly.com/pub/a/javascript/synd/2001/08/28/doctype.html?page=2

Depends on the doctype you use. If you use Transitional ( <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ) it will be shown in the way you want.

example with transitional: http://jsbin.com/icesi

example with strict: http://jsbin.com/icesi/2

A related statement that may help you understand the issue is the following

Here's another example. You may have
found that setting font-size: medium
results in different font sizes in
Explorer versus Navigator. This occurs
because of the way the Internet
Explorer for Windows team interpreted
the intent of the CSS specification.
In order to stay consistent with the
Windows version, Internet Explorer for
the Macintosh emulated its behavior in
the 4.x series. If you put IE5/Mac
into bugwards compatibility mode, it
will continue to treat font-size:
medium as IE4.x/Win does. In strict
mode however, it will act as Navigator
does, which is actually the correct
interpretation according to the W3C.

Source: http://oreilly.com/pub/a/javascript/synd/2001/08/28/doctype.html?page=2

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