php - div样式高度不起作用

标签 php css html

我需要直接在 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/

相关文章:

css - jQuery Sortable Items Float Right 排序不流畅问题

html - Firefox 中宽度和边距的不同行为

html - 强制内联 block 导航栏和列表

javascript - 使用 jQuery 获取输入值。可以用,但是确认错误

php - Codeigniter 查询显示 num 行 1 但行返回空数组

php - PHP 中的静态变量

html - 使用 CSS 使这个主题适合浏览器窗口

php - 我的购物车产品添加和减去无法正常工作

php - MySQL 连接多个表

javascript - Jquery AJAX显示具有相同类的每个选择标签的onchange数据库记录