【问题标题】:center img within div在 div 中居中 img
【发布时间】:2018-10-10 17:37:37
【问题描述】:

我在 sencha extjs 应用程序中有一个容器,里面有一个 img div,位于容器的左侧。我希望它居中,这样旋转就可以了(因为我已经将图像拟合到最长的尺寸)。

<div class="x-component ih-image-container x-box-item x-component-default"
    style="width: 516px; overflow: auto !important; right: auto; left: 0px; margin: 0px; top: 30px; height: 429px;" 
    id="component-1990">

    <img class="ih-image" 
        style="max-width: 100%; max-height: 100%; border-left: 1px solid rgba(0, 0, 0, 0.1); border-right: 1px solid rgba(0, 0, 0, 0.1); border-bottom: 1px solid rgba(0, 0, 0, 0.1); transition: all 0.25s ease-in-out 0s; transform: rotate(0deg) scale(1); width: auto;" 
        src="/viewer/image/urlabridged" data-rotate="0" data-scale="1">
</div>

我已经尝试添加这些都没有效果,来自类似问题的 SO

垂直对齐:中间;
文本对齐:居中;
显示:块

可能包含这些的其他 extjs 容器正在发生变化。

【问题讨论】:

  • 添加浮动:左;文本对齐:居中;到 div

标签: javascript html extjs


【解决方案1】:

你试过margin: auto;吗? 您也可以将 flex 用于外部组件:

display: flex;
justify-content: center;

另外,如果你的外部组件有固定大小和position: relative,那么你可以使用

position: absolute;
left: 50%;
transform: translateX(-50%);

【讨论】:

  • translateX/Y 看起来很有希望。轮换并没有很好地发挥作用,但它比以前好多了,所以我会接受它作为我能够提出的有限问题的答案。谢谢。
  • @KennyOstrom 玩transform-origin 可能会帮助您解决旋转问题?
  • 添加
    元素效果更好,所以这对 +1 很有帮助,但我需要为每次旋转提供不同的翻译,而且它不能很好地推广到形状奇特的图像。
  • @KennyOstrom
    标签已弃用,不应使用:w3schools.com/tags/tag_center.asp
  • 它使用中间
    替换
【解决方案2】:

为什么不用 HTML &lt;center&gt; 标签?

.rotate90 {
    -webkit-transform: rotate(30deg);
    -moz-transform: rotate(30deg);
    -o-transform: rotate(30deg);
    -ms-transform: rotate(30deg);
    transform: rotate(30deg);
}
<div style="width: 516px; overflow: auto !important; right: auto; left: 0px; margin: 0px; top: 30px; height: 429px; background-color:red;">
<center><img src="https://www.gravatar.com/avatar/ad942ab5a7f58014ab966ae26cb91f87?s=48&d=identicon&r=PG&f=1" class="rotate90"/></center>
</div>

【讨论】:

  • Works ... 从技术上讲
    已被弃用,但这也适用于中间
    你有中心。奇怪的是它不适用于实际包含的 div。
猜你喜欢
  • 1970-01-01
  • 2013-10-04
  • 2012-08-07
  • 2011-06-24
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
相关资源
最近更新 更多