浮动:仅在 IE 中右 div 出现在下一行

发布于 2024-10-03 19:13:50 字数 733 浏览 0 评论 0原文

好的,在开始编写 Web 应用程序之前,我正在开发 UI 原型。我的设计大部分是在 Firefox 中完成的,(当然)当我在 IE 中测试它时,存在很多渲染问题。其中一个问题是,如果我有一个包含一些文本的 div 和另一个设置为 float:right 的 div,则该嵌套 div 将显示在下一行,位于其父 div 下方。这是最简单形式的问题标记...

<div style="background-color:red;">
    Text
    <div style="background-color:yellow; float:right;">Right</div>
</div>

我在互联网上搜索解决方案,我发现唯一可以在 IE 中工作的相关解决方案是将浮动 div 放在其父级的开头,如下所示

<div style="background-color:red;">
    <div style="background-color:yellow; float:right;">Right</div>
    Text
</div>

...实际上,嵌套的 div 有一个类,而我的 CSS 浮动了该类。但是,如果我最终制作另一个针对移动设备的样式表并且我不再希望内部 div 浮动,会发生什么情况?那么 HTML 中的内容本身就会乱序,只是为了适应 IE 中的 CSS 问题。有更好的方法来解决这个问题吗?

Ok, so I'm working on a prototype of my UI before I start coding the webapp. I got the design mostly done while working in Firefox and (of course) when I tested it in IE, there were a lot of rendering issues. One of those issues is that if I have a div that contains some text and another div that's set to float:right, that nested div shows up on the next line, below its parent div. This is the problem markup in its simplest form...

<div style="background-color:red;">
    Text
    <div style="background-color:yellow; float:right;">Right</div>
</div>

I scoured the internet for solutions and the only working relevant solution I found that makes this work in IE is to place the floating div at the beginning of its parent like this...

<div style="background-color:red;">
    <div style="background-color:yellow; float:right;">Right</div>
    Text
</div>

In reality, the nested div has a class and my CSS is floating that class. But what happens if I eventually make another stylesheet to target mobile devices and I no longer want that inner div to be floated? Then the content itself would be out of order in HTML, just for the sake of accommodating a CSS issue in IE. Is there a better way to solve this?

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

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

发布评论

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

评论(4

梦明 2024-10-10 19:13:50

我的一位同事最近遇到了一个非常相似的问题。我建议简单地使用定位而不是浮动。我相信你可以在这里做同样的事情:

<div style="background-color:red; position:relative;">
    Text
    <div style="background-color:yellow; position:absolute; right:0; top:0;">Right</div>
</div>

我不知道你是否需要使用浮动。使用定位方法将导致定位元素在正常流程中不占用空间,但保持正确的源顺序并在视觉上完成我认为您想要做的事情。

A colleague of mine recently had a very similar problem. I recommended simply using positioning rather than floating. I believe you could do the same here:

<div style="background-color:red; position:relative;">
    Text
    <div style="background-color:yellow; position:absolute; right:0; top:0;">Right</div>
</div>

I don't know if you have a requirement to use floats or not. Using the positioning method will cause the positioned element to not take up space in normal flow, but otherwise keep the correct source order and visually accomplish what I think you want to do.

又爬满兰若 2024-10-10 19:13:50

在内部 div 上设置宽度值并使其显示:inline-block。 Div 是块元素,占用父元素 100% 的宽度,这就是 IE 将其放在下一行的原因。

Set a width value on your inner div and make it display: inline-block. Div's are block elements that take 100% width of the parent, that's why IE puts it on the next line.

亚希 2024-10-10 19:13:50

我不确定这对你来说是否可行,但是将外部 div 中的文本放在它自己的 div 中似乎可以解决问题

<div style="background-color:red;">
    <div style="float: left;">Text</div>
    <div style="background-color:yellow; float:right;">Right</div>
</div>

I am not sure if it is a possibility for you, but putting the text within the outer div in a div of its own seems to solve the problem

<div style="background-color:red;">
    <div style="float: left;">Text</div>
    <div style="background-color:yellow; float:right;">Right</div>
</div>
め可乐爱微笑 2024-10-10 19:13:50

我刚刚在 IE7 中遇到了这个问题 - 就我而言,要清除浮动的项目无论如何都是全宽的。我只是将其设置为“float: none;clear: left”,它似乎有效。

I just hit this problem in IE7 - in my case, the item that was going to clear the float was going to be full width anyway. I just set that to "float: none;clear: left" and it seems to work.

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