我需要直接在 html 中设置 div 大小的样式,但我遇到了问题。 div 容器大小仅在我在 CSS 类中设置高度样式时才有效,否则容器将没有任何高度(甚至在 div 标签中设置样式,我正在尝试这样做)。
宽度和高度是从图像中获取的,我不知道还有什么其他方法可以做到这一点;
list($width, $height) = getimagesize('dir/to/img.jpg');
echo $width . $height; //WORKING
// resizing the img...
// the resizing script have max width and height
$img_width = 'width="' . $width . '"';
$img_height = 'height="' . $height . '"';
<div class="img_container" ' . $img_width . ' ' . $img_height . '>
<img src="'. $img_dir . '" class="img"/>
</div>
// the html output is ok but maybe it's not overwriting the css
这是我为容器使用的 css 类:
.img_container {
overflow: hidden;
position: relative;
box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.05);
display: block;
width: auto;
min-height: 50px;
max-height: auto;
}
.img {
height: auto;
display: block;
}
如果我从 .img_container
中删除高度,div 将没有任何高度(图像不显示,但 div 具有宽度和高度)。
所以我想要的是 div 具有与图像相同的宽度和高度。
最佳答案
内联样式应该覆盖(优先级高于)外部 CSS,但您没有正确设置宽度和高度。你应该使用 style
像这样的属性;
<div class="img_container" style="width: <?php echo $width; ?>px; height: <?php echo $height; ?>px;">
您还需要重新进入 PHP 解析以使用 PHP 变量(使用上面的 <?php ?>
标记)。
此外,您的 CSS 有一个错误,因为您没有在 ;
的末尾放置分号 ( img
)规则。
关于php - div样式高度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26578546/