gpt4 book ai didi

css - 如何使用前后伪元素使图像垂直和水平居中

转载 作者:行者123 更新时间:2023-11-28 17:36:35 25 4
gpt4 key购买 nike

今天我参加了一个工作面试,被问到以下问题:

How can you center an image of unknown height and width both vertically and horizontally without the usage of tables (e.g. display:table-cell)?

有人提示我可以使用 beforeafter 伪元素来做到这一点,但我没有找到解决方案。能请大家帮忙吗?

最佳答案

可能是最简单的方法:

DEMO

img {
position: relative;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}

关于css - 如何使用前后伪元素使图像垂直和水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24994598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com