html - 创建动态重叠 div,它会根据内部内容改变高度

标签 html css css-position division overlapping

我想创建一个结构,其中有一个背景容器,该容器将包含一些图像并具有一些最小高度。在该容器的顶部将有另一个容器,其中将包含一些文本内容。现在如果文本内容增加,我的父容器的高度需要增加。

我已经使用 position:relative 和 absolute CSS 属性实现了这一点。内容 div 是绝对的,不允许父 div 的高度在内容增加后增加。

我能得到一些解决方案吗?我不想更改现有的实现,只需要在这一个中进行一些修复。

我为此创建了一个虚拟 fiddle :

感谢您的帮助..!!

.parent{
          position:relative;
          min-height:300px;
          border:1px solid;
}
.child{
          position:absolute;
          width:200px;
          height:auto;
          border:1px solid;
}
<div class="parent">
    <div class="child">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

https://jsfiddle.net/arpagrawal/kd144czz/1/

最佳答案

有几种解决方法

  1. 使用背景图片,不要使用绝对位置。看下面的片段

.parent {
  position: relative;
  min-height: 300px;
  border: 1px solid;
  background-image: url('https://via.placeholder.com/350x150');
  background-repeat: no-repeat;
  background-size: cover;
}

.child {
  width: 200px;
  height: auto;
  border: 1px solid;
}
<div class="parent">
    <div class="child">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

  1. 使用 jQuery 为父子赋予相同的高度并使用 img 标签

$(".parent").height($(".child").height())
.parent{
          position:relative;
          min-height:200px;
          border:1px solid;
}
.parent img {
	width:100%;
	height:100%;

}
.child{
          position:absolute;
          width:200px;
          height:auto;
          border:1px solid;
					top:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<img src="http://via.placeholder.com/350x150">
    <div class="child">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
				typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
				typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
				
    </div>
</div>

关于html - 创建动态重叠 div,它会根据内部内容改变高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44414230/

相关文章:

jquery - 以编程方式更改scrollTop后固定定位按钮上的移动Safari错误...?

javascript - 覆盖响应性 Bootstrap 3 @media print

使用外部 URL 的 jQuery 标签

表格列的 HTML 文本显示限制

html - CSS height auto 在相对 DIV 中不起作用

html - 在 CSS 中完美修复背景横幅

html - 文本溢出省略号

javascript - jQuery:删除结束标记

html - 无法在 Bootstrap Navbar 中看到下拉菜单

html - 位置 :fixed not working on chrome but works in firefox