2014-10-26 79 views
1

我需要在html中直接设置样式的大小,但是我遇到了问题。 div容器大小只适用于在CSS类中设置高度的样式,否则容器将不会有任何高度(甚至可以在div标签中设置样式,而是尝试执行)。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具有与图像相同的宽度和高度。

+1

首先,当你高度后'插入分号发生什么:auto'? – 2014-10-26 22:10:32

+0

什么是HTML的样子,你有任何额外的CSS(如绝对定位...)的图像? – jeroen 2014-10-26 22:12:40

+0

你只是没有设置div的高度。只输出一些像素值,甚至没有div标签的任何属性(就像你做的那样)不会做任何事情。也许重新访问您选择的HTML/CSS指南? – hakre 2014-10-26 22:23:57

回答

1

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

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

您还需要重新输入PHP解析使用PHP变量(与<?php ?>标签如上)。

此外,您的CSS中有错误,因为您尚未在img规则的末尾放置分号(;)。

+0

对不起,我已经说过了。这些变量已经包含它:$ img_width ='width =“'。$ width。'”'; – 2014-10-26 22:25:59

+0

啊是的。使用'width ='和'height ='不会覆盖CSS,并折旧。 – worldofjr 2014-10-26 22:28:24

+0

style =“width/height”会起作用吗?或者它是一样的? – 2014-10-26 22:30:07