2014-10-26 77 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