<!doctype html> <html> <head> <meta charset="utf-8"> <title>图片在正方形容器内缩放至适合尺寸并水平居中和垂直居中</title> <style type="text/css"> * { margin: 0; padding: 0; line-height: 1; } .square-container { float: left; overflow: hidden; margin: 100px 0 0 100px; width: 250px; height: 250px; background-color: #c8c8c8; background-position: 50%; background-size: contain; background-repeat: no-repeat; } </style> </head> <body> <div class="square-container" style="background-image:url(1.jpg);"></div> <div class="square-container" style="background-image:url(2.jpg);"></div> <div class="square-container" style="background-image:url(3.jpg);"></div> </body> </html>
Copyright © 2024 码农人生. All Rights Reserved