【发布时间】:2018-02-15 23:46:31
【问题描述】:
这是另一个关于在 div 中垂直居中的问题,但我尝试了许多其他答案中讨论的解决方案,但均无济于事。
Here's an example of the code to play with: https://codesandbox.io/s/z2qzxwk99x
箭头图标在视口中垂直居中,而不是查看器包装器 div。因此,如果您使页面非常窄,它会完全脱离图像,而不是保持垂直居中。
.viewer-wrapper {
background-color: #1b8dbb;
position: relative;
}
.arrow-wrap {
position: absolute;
max-height: 100%;
line-height: 95vh;
background-color: #4cae4c;
margin: auto;
opacity: .9;
left: 0px
}
.arrow-icon {
background-color: orangered;
}
.comic-page {
object-fit: contain;
max-height: 95vh;
width: 100%;
}
<div className="viewer-wrapper">
<div className="arrow-wrap">
<LeftArrow className="arrow-icon" size={75} />
</div>
<img className="comic-page"
src="http://assets-production.rovio.com/s3fs-public/hatchlings_0.jpg"
about="This is an image"
/>
</div>
【问题讨论】:
-
问题似乎根源于箭头图标的 position:absolute 一直到视口来计算它的相对位置,而不是它最近的定位祖先。我可以通过将其拉到 .arrow-wrap div 之外来解决此问题,并将其直接父级设为 .viewer-wrapper。任何想法为什么它没有正确找到它的相对位置?