Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将DIV width手风琴设置为图像大小_Php_Html_Background_Size - Fatal编程技术网

Php 如何将DIV width手风琴设置为图像大小

Php 如何将DIV width手风琴设置为图像大小,php,html,background,size,Php,Html,Background,Size,我需要设置一个DIV宽度,这个高度是根据我将要用作背景的图像的宽度固定的。显然,图像比DIV大 让我试着解释一下: HTML 现在,背景图像hh1.jpg肯定比DIV大。我可以使用cover属性,但我想通过php动态设置DIV的宽度,以便它始终显示整个背景图像 假设我有一个180px的固定高度,使用一个1000wx1800h的图像,得到的div应该是100wx180hpx。如果我使用1200wx3600hpx,div应该是60wx180hpx,以此类推。你可以选择最小宽度,这总是足够的。 在相对

我需要设置一个DIV宽度,这个高度是根据我将要用作背景的图像的宽度固定的。显然,图像比DIV大

让我试着解释一下:

HTML

现在,背景图像hh1.jpg肯定比DIV大。我可以使用cover属性,但我想通过php动态设置DIV的宽度,以便它始终显示整个背景图像


假设我有一个180px的固定高度,使用一个1000wx1800h的图像,得到的div应该是100wx180hpx。如果我使用1200wx3600hpx,div应该是60wx180hpx,以此类推。

你可以选择最小宽度,这总是足够的。 在相对定位的容器中使用绝对位置,以便元素不会干扰文档流

<div class="cont"><div class="bg"></div></div>
.cont{position:relative;}.bg(position:absolute; background: ....)

但是为什么要使用PHP呢?你不能用JavaScript吗?嗯,我正在尝试为WP创建一个简短的代码,并且更喜欢用PHP做一个简单的数学运算。
.book {
  height:180px;
  background: url("hh1.jpg");
  width: ???
}
<div class="cont"><div class="bg"></div></div>
.cont{position:relative;}.bg(position:absolute; background: ....)