Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.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
Html 带背景图像的空浮动div上的强制高度_Html_Css - Fatal编程技术网

Html 带背景图像的空浮动div上的强制高度

Html 带背景图像的空浮动div上的强制高度,html,css,Html,Css,我有一个有两个浮动div的容器。右侧div为空,只需显示居中的背景图像。但是,右侧div没有高度(即使我将其设置为高度:100%;),这会阻止显示背景图像 如何强制右div的高度等于父容器?请记住父容器的高度是动态的 HTML: Fiddle:您需要为.wrapper元素设置一个高度,该元素是.bg img的父元素。百分比高度在父元素中始终需要参考高度。如果您仅使用高度的百分比值,则每个祖先(最多为body和html(或最多为具有固定高度的元素)都必须设置为百分比值,通常为100% 在你的小提琴

我有一个有两个浮动div的容器。右侧div为空,只需显示居中的背景图像。但是,右侧div没有高度(即使我将其设置为
高度:100%;
),这会阻止显示背景图像

如何强制右div的高度等于父容器?请记住父容器的高度是动态的

HTML:


Fiddle:

您需要为
.wrapper
元素设置一个
高度
,该元素是
.bg img
的父元素。百分比高度在父元素中始终需要参考高度。如果您仅使用高度的百分比值,则每个祖先(最多为
body
html
(或最多为具有固定高度的元素)都必须设置为百分比值,通常为100%


在你的小提琴中,这看起来是这样的:

你需要为
.wrapper
元素设置一个
高度,它是
.bg img
的父元素。百分比高度在父元素中始终需要参考高度。如果您仅使用高度的百分比值,则每个祖先(最多为
body
html
(或最多为具有固定高度的元素)都必须设置为百分比值,通常为100%


在您的小提琴中,这看起来是这样的:

您可以使用Flexbox来操作:

.wrapper{
溢出:隐藏;
显示:flex;/*以内联方式显示flex项(子项)*/
}
.fl{
/*浮动:左*/
/*宽度:50%*/
flex:1;/*增长并占据父级宽度的50%*/
}
.bg img{
背景:url('https://news.nationalgeographic.com/content/dam/news/photos/000/755/75552.ngsversion.1422285553360.adapt.1900.1.jpg“)无重复中心;
背景大小:50px 50px;/*看起来很小,因为您是这样设置的*/
/*高度:100%;不需要/默认情况下,两个div的高度相同,高度由“最高”弹性项(.f1)的高度决定*/
}

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。两人或两人在一个无教区的房间里互相指责。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡。

您可以使用Flexbox进行操作:

.wrapper{
溢出:隐藏;
显示:flex;/*以内联方式显示flex项(子项)*/
}
.fl{
/*浮动:左*/
/*宽度:50%*/
flex:1;/*增长并占据父级宽度的50%*/
}
.bg img{
背景:url('https://news.nationalgeographic.com/content/dam/news/photos/000/755/75552.ngsversion.1422285553360.adapt.1900.1.jpg“)无重复中心;
背景大小:50px 50px;/*看起来很小,因为您是这样设置的*/
/*高度:100%;不需要/默认情况下,两个div的高度相同,高度由“最高”弹性项(.f1)的高度决定*/
}

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。两人或两人在一个无教区的房间里互相指责。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡。

只需使用父包装容器上的flexbox即可。 如果希望背景大小相对于包装器进行更改(也可以使用
cover
contain
),则将背景大小设置为100%,它将根据父容器的高度动态调整div

在此处使用
contain
更合适,因为它不会让您的图像被裁剪并动态更改其尺寸

.wrapper{
溢出:隐藏;
显示器:flex;
}
.fl{
宽度:50%;
}
.bg img{
背景:url('https://news.nationalgeographic.com/content/dam/news/photos/000/755/75552.ngsversion.1422285553360.adapt.1900.1.jpg“)无重复中心;
背景尺寸:包含;
}

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。二人世界
这是一种令人厌恶的行为,因为它是一种不平等的行为。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡。

.
只需使用父包装容器上的flexbox即可。 如果希望背景大小相对于包装器进行更改(也可以使用
cover
contain
),则将背景大小设置为100%,它将根据父容器的高度动态调整div

在此处使用
contain
更合适,因为它不会让您的图像被裁剪并动态更改其尺寸

.wrapper{
溢出:隐藏;
显示器:flex;
}
.fl{
宽度:50%;
}
.bg img{
背景:url('https://news.nationalgeographic.com/content/dam/news/photos/000/755/75552.ngsversion.1422285553360.adapt.1900.1.jpg“)无重复中心;
背景尺寸:包含;
}

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。二人世界
在一个充满活力的世界里
<div class="wrapper">
  <div class="fl">
     Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>

  <!--Background image should appear below, in the empty div-->
  <div class="fl bg-img"></div>
</div>
.wrapper {
  overflow: hidden;
}

.fl {
  float: left;
  width: 50%;
}

.bg-img {
  background: url('https://news.nationalgeographic.com/content/dam/news/photos/000/755/75552.ngsversion.1422285553360.adapt.1900.1.jpg') no-repeat center center;
  background-size: 50px 50px;
  height: 100%;
}