使用 Chrome/Safari 时,定位的 img 不会移动
我有一个绝对位置 img (我想在 li 中右对齐)。我正在使用绝对定位。此方法在 IE8 和 FF3.6 中运行良好,但在 Safari 和 Chrome 中效果不佳。在后者中,当 div 折叠时,img 保持固定。
我编写了一个小脚本来演示我的问题:
<html>
<head>
<script>
function hide()
{
var el = document.getElementById('collapsable');
//el.style.visibility = "hidden";
el.style.display = "none";
} // hide
function show()
{
var el = document.getElementById('collapsable');
//el.style.visibility = "visible";
el.style.display = "inline";
} // show
</script>
</head>
<body>
<input type="button" value="Collapse" onClick='hide();' >
<input type="button" value="Expand" onClick='show();' >
<div id="collapsable" >
<div>The quick fox jumped over the lazy brown dog</div>
<div>The quick fox jumped over the lazy brown dog</div>
<div>The quick fox jumped over the lazy brown dog</div>
</div>
<ul>
<li style="width:200">Item 1<span>< img src="favicon.ico" style="position:absolute; right=5px;" ></span></li>
<li style="width:200">Item Two</li>
<li style="width:200">Third Item</li>
</ul>
</body>
</html>
I have an absolute position img (I want to right justify it in the li). I am using absolute positioning. This method works fine in IE8 and FF3.6 but not as I would like in Safari and Chrome. In the later, the img remains fixed when the div is collapsed.
I have made up a little script to demonstrate my issue:
<html>
<head>
<script>
function hide()
{
var el = document.getElementById('collapsable');
//el.style.visibility = "hidden";
el.style.display = "none";
} // hide
function show()
{
var el = document.getElementById('collapsable');
//el.style.visibility = "visible";
el.style.display = "inline";
} // show
</script>
</head>
<body>
<input type="button" value="Collapse" onClick='hide();' >
<input type="button" value="Expand" onClick='show();' >
<div id="collapsable" >
<div>The quick fox jumped over the lazy brown dog</div>
<div>The quick fox jumped over the lazy brown dog</div>
<div>The quick fox jumped over the lazy brown dog</div>
</div>
<ul>
<li style="width:200">Item 1<span>< img src="favicon.ico" style="position:absolute; right=5px;" ></span></li>
<li style="width:200">Item Two</li>
<li style="width:200">Third Item</li>
</ul>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将样式
position:relative
添加到图像的包装元素。您可能希望将此样式添加到父
中。
例如,
请注意,我已将您的
right=5px;
更正为right: 5px;
Add style
position: relative
to a wrapper element of the image.You would probably want to add this style to the parent
<li>
.e.g.
Notice I have corrected your
right=5px;
toright: 5px;
将
right=5px
更改为right:5px
。编辑
错误抱歉..给出一个位置:相对于保存图像跨度的 li
change
right=5px
toright:5px
.EDIT
err sorry.. give a position:relative to the li that holds the image span