【发布时间】:2017-08-20 20:43:27
【问题描述】:
我在尝试将元素翻译 -50%、-50% 时遇到问题。
我的代码如下,但它在 Firefox 和 Chrome 中的执行方式不同。 Firefox 呈现了我想要实现的目标(类 .two 的元素的中心位置),但 Chrome 只是失去了它。
到目前为止,我发现它可能是 position: relative 的情况,但我需要它而不是 absolute 来计算元素相对于父元素的宽度和高度。
body, html {
height: 100%; width: 100%;
margin: 0; padding: 0;
}
.one {
background: red;
width: 100%;
height: 100%; /* for anothet purposes */
min-height: 30%;
max-height: 50%;
}
.two {
background: orange;
display: inline-block;
height: 80%;
width: auto; max-width: 90%;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
<div class="one">
<div class="two">ABC</div>
</div>
【问题讨论】:
-
将
relative更改为absolute -
@friedman 为什么将
height: 80%设置为two? -
谢谢你,Jer,但我需要它相对如上所述。
-
@kukkuz - 我只需要这个元素是 .one 高度的 80%。