div风格高度不起作用

时间:2014-10-26 22:08:20

标签: php css html5

我需要直接在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具有与图像相同的宽度和高度。

2 个答案:

答案 0 :(得分:2)

内联样式应覆盖(优先级高于)外部CSS,但您无法正确设置宽度和高度。您应该使用style这样的属性;

<div class="img_container" style="width: <?php echo $width; ?>px; height: <?php echo $height; ?>px;">

您还需要重新进入PHP解析以使用PHP变量(如上所述使用<?php ?>标记)。

此外,您的CSS中有错误,因为您没有在;规则的末尾添加分号(img)。

答案 1 :(得分:0)

我发现身高:自动为我获取div中的内容。