html - 定位最接近绝对定位 div 的图像

标签 html css cross-browser

我有以下html

<div id="someid">
    <img src="" />
    <div class="someclass"></div>
</div>
<div id="other">
</div>

和下面的CSS

#someid{position: relative;}
.someclass{position: absolute; left: 100px; width: 100px; height: 100px; background-color: red;}
img{floa/t: left; background-color: blue; width: 100px; height: 100px;}
#other{width: 100px; height: 100px; background-color: green; position: relative;}

我不需要图像向左浮动。 Demo

绿色背景的 div 应该在下面。这个演示在 chrome 中完全符合我的要求,但在 Mozilla Firefox 中却不行。

最佳答案

这在没有 float:left; 的情况下实际上是可能的,但我不确定您是否真的需要一个绝对定位的 div

注意:没有绝对div

#someid {
    position: relative;
}
.someclass {
    position: relative;
    display:inline-block;
    width: 100px;
    height: 100px;
    background-color: red;
}
img {
    /*float: left;*/
    display:inline-block;
    background-color: blue;
    width: 100px;
    height: 100px;
}
#other {
    width: 100px;
    height: 100px;
    background-color: green;
    position: relative;
}

火狐浏览器

它们只是忽略宽度高度,因此您需要将显示更改为 block 级元素,然后它们才会被应用。

Unable to set width/height to an img when DOCTYPE is set (Firefox)

DEMO

编辑:使用绝对定位的 div

DEMO

关于html - 定位最接近绝对定位 div 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17593166/

相关文章:

html - 在网页上使用直接 ASCII 字体有什么问题吗?

html - react 引导 CSS : I want my Container element to fill more of the entire page horizontally

html - 旋转并保留 3d 不起作用

html - 忽略 CSS 样式类

PHP - 表单错误处理问题

java - 带有 GWT Sprite 的视网膜图像?

javascript - 如何创建具有百分比高度的固定、可滚动的 div?

css - 如何防止大字体在 Chrome (OSX) 中看起来呈锯齿状?

testing - 谷歌浏览器如何影响开发网络应用程序的产品所有者?

jQueryUI 对话框位置在 Chrome 和 Safari 中损坏