【问题标题】:Can't move img in display block?无法在显示块中移动 img?
【发布时间】:2014-09-26 06:31:06
【问题描述】:

这是我的 HTML:

<div id="mitte"><a href="http://www.web.com"><img id="img_mittig" src="tienda.png" /></a></div>

这是我的 CSS:

#img_mittig{ display: block; margin-left: auto;margin-right: auto;}

img 位于右中,但是当我尝试使用top: 200px; 向下移动图像时,它不起作用。为什么?

【问题讨论】:

  • 你能把它放在 JSFiddle 里吗?我认为问题出在其他一些 CSS 代码中
  • 确定:jsfiddle.net/SoSoDef/4twL6;谢谢
  • top 声明什么都不做,除非您将 position 指定为 relativefixedabsolute

标签: html css image center


【解决方案1】:

top:200px 仅适用于 position:absoluteposition:relative;position:fixed

使用margin-top:200px;可以实现您想要实现的目标

【讨论】:

  • position: fixed(事实上,除了static的默认值之外,几乎是任何值)。
【解决方案2】:

您需要在元素上添加position 才能使用top
比如:

#img_mittig { position: relative; top: 200px; }

【讨论】:

    【解决方案3】:

    您不能使用“上、左、右、下”移动没有定位的块元素。使用边距和填充。例如margin-top: 200px; 如果你想通过设置top: 200px来移动img,那么为img设置position: relative

    【讨论】:

      猜你喜欢
      • 2018-03-11
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多