一种用于横向和纵向照片的 CSS 样式



我正在显示以横向和纵向格式拍摄的图像。我想将它们样式设置为始终以 500px 显示照片的最长边。这可能吗?

有人发布了我使用的答案,然后出于某种原因将其删除。它工作得很好。

max-width: 500px;
max-height: 500px;

谢谢!

您可以设置背景大小:100% 100%;(这将使图像填充其父元素)然后,您可以将包含照片的div设置为宽度:500px;

PHP

list($width, $height, $type, $attr) = getimagesize("img/test.jpg");
if($width > $height){
    $newWidth = 500;
    $newHeight = (500 / $width) * $height;
}else{
    $newHeight = 500;
    $newWidth = (500 / $height) * $width;
}

.HTML

<img src="img/test.jpg" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>" alt="Test" />

最新更新