html - 使用 Chrome/Safari 时定位的 img 不会移动

标签 html css positioning

我有一个绝对位置 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>

最佳答案

添加样式 position: relative到图像的包装元素。 您可能希望将此样式添加到父项 <li> .

例如

<li style="width:200; position: relative;"">Item 1
    <span>
        <img src="favicon.ico" style="position:absolute; right: 5px;" />
    </span>
</li>

注意我已经更正了你的right=5px;right: 5px;

关于html - 使用 Chrome/Safari 时定位的 img 不会移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6416266/

相关文章:

css - 为什么 <div> 重叠?

jquery - 最简单的jquery有div "position:fixed"到右下角(总是..就像向下滚动)?

javascript - 从屏幕外滑动一个 div,然后再打开 toggle

html - 如何消除TD中输入框右侧的空白

html - 当屏幕宽度减半时,Bootstrap 将我的内容向下推送

jquery onclick 下拉菜单

javascript - 图像的可选区域

javascript - 当鼠标快速通过元素时,onMouseEnter 不起作用

html - 将 url 图片放在黑色背景颜色 html/css 之上

javascript - 如何找到元素的位置并将其应用于不同的元素?