简单点的哈
<!DOCTYPE html> <html> <head> <style> html, body { height: 100%; margin: 0; display: flex; align-items: center; justify-content: center; } .container { max-width: 100%; max-height: 100%; text-align: center; } .container img { max-width: 100%; max-height: 100%; } </style> </head> <body> <div class="container"> <img src="your-image.jpg" alt="Your Image"> </div> </body> </html>
<!DOCTYPE html>
<html>
<head>
<style>
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 100%;
height: auto;
}
body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-color: #f3f3f3;
</style>
</head>
<body>
<img src="your_image_path.jpg" alt="Your Image" class="center">
</body>
</html>
感觉跟面试似的,居中元素的几种方式及使用限制
上面有些复杂,直接
// 图片自适应铺满: img { position: fixed; width: 100vw; height: 100vh; inset: 0; z-index: 0; object-fit: cover; } // 图片不铺满,只需改为: object-fit: contain;
搞定了 谢谢大家
这事找g屁提就行了