使某些 HTML 元素表现为“块”;与内联父级内的内联元素混合时的情况
我试图让一个 div 的显示属性设置为内联(或内联块,如果我想要边距),以便在 IE 中正确运行(在大多数其他浏览器中也是如此)。 我的情况是这样的 - 想象一个工作区,其中项目容器包含以水平方式布局的内联项目。这些项目可以容纳文本或图像等内容,但也可以是复合类型,例如“分数”,其分子和分母本身就是项目容器,包含更多水平项目。
例如,我可能有 HTML:
<div class='item-container'>
<div id='statictext' class='item'>x = </div>
<div id='fraction' class='item'>
<div id='numerator' class='item-container'>...</div>
<hr/>
<div id='denominator' class='item-container'>...</div>
</div>
</div>
显然,我不希望项目或项目容器具有固定的宽度或高度,因为它们可以包含嵌套内容,这将增加所需的空间量(例如,想象另一个项目中的一个分数)分数),类似地,如果我希望静态文本“项目”的宽度足够大以包含一行文本(即内联)。
我认为的问题是,很难避免将块元素放入内联“item”/“item-container”元素中,例如分数中的
,或者如果我想说“项目”顶部的菜单栏,在计算出项目其余内容的宽度后使用整个宽度。 我知道将实际的块项放入内联项中是无效的语法,尽管将块元素的显示属性设置为 inline 或 inline-block 至少可以在 Firefox/Chrome 中正常运行。但可惜,在 IE 中不行。
有足够的修复吗?
编辑:我实际上对“item”和“item-container”使用了 inline-block (带有适当的 IE hack),以使其在 Firefox 等中正常工作,但 IE 仍然将它们视为内联,然后将其转换为块,因为它的子级之一是块。
I'm trying to get an div with its display property set to inline (or inline-block if I want a margin) to behave correctly in IE (it does in most others).
My situation is this - imagine a workspace in which an item container contains inline items laid out in a horizontal fashion. These items can hold things like text, or images, but also be composite types, say for example a 'fraction', whose numerator and denominator are themselves item containers, containing more horizontal items.
So for example, I might have the HTML:
<div class='item-container'>
<div id='statictext' class='item'>x = </div>
<div id='fraction' class='item'>
<div id='numerator' class='item-container'>...</div>
<hr/>
<div id='denominator' class='item-container'>...</div>
</div>
</div>
Clearly, I don't want fixed width or height for an item or item-container, because they can contain nested content which will increase the amount of space needed (e.g. imagine an fraction inside another fraction), and similarly if I want the width of a static text 'item' to be just big enough to contain the text on one line, i.e. inline.
The problem I think is that it's hard to avoid putting block elements inside my inline 'item'/'item-container' elements, for example the <hr>
in the fraction, or if I want say a menu bar at the top of an 'item' that uses the whole width after the width of the rest of the item's contents has been calculated.
I know it's invalid syntax to put an actual block item inside an inline one, although setting the block element's display attribute to inline or inline-block makes things behave correctly in Firefox/Chrome at least. But alas, not in IE.
Is there an adequate fix?
EDIT: I actually used inline-block (with the appropriate IE hack) for 'item' and 'item-container' to get it to work spiffingly in Firefox et al, but IE still treats them as inline, which then subsequently gets converted into block because one of its children is a block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
。您可以使用text-decoration: underline
或使用底部边框或使用图像(例如,拉伸到所需宽度的单像素图像)来绘制线条。然后您就可以使用内联元素。Don’t use
<hr>
. You can draw a line usingtext-decoration: underline
or using a bottom border or using an image (say, a one-pixel image stretched to the desired width). Then you can work with inline elements.