预览css
.out { position: relative; } .in { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
解读:因而可知主要是由top、left的百分比和translate百分比实现的,那咱们接下来就有必要讲一下二者百分比的原理了:code
X(内X轴偏移量) = width(外) * left百分比(内)
Y(内Y轴偏移量) = height(外) * top百分比(内)
预览orm
X(内X轴偏移量) = width(内) * translateX百分比(内)
Y(内Y轴偏移量) = height(内) * translateY百分比(内)
预览get
从上述的原理解读中能够看出最终的位置的计算公式是:it
X(内X轴偏移量) = width(外) left百分比(内) + width(内) translateX百分比(内)
Y(内Y轴偏移量) = height(外) top百分比(内) + height(内) translateY百分比(内)
即:io
X(内X轴偏移量) = 300 50% + 100 -50% = 100
Y(内X轴偏移量) = 300 50% + 100 -50% = 100