html - 子div增长时容器div不增长?
问题描述
当内容大小发生变化时,如何使容器适合内容的大小?请注意,当添加更多内容时,背景填充不会扩展。
var text = document.getElementById("content");
text.innerText = document.body.innerText + document.body.innerText + document.body.innerText;;
* {
margin: 0;
padding: 0;
box-sizing: border-box;
border: none;
}
#view1 {
position: absolute;
background: #E5E5E5;
width: 671px;
height: 272px;
background-color: rgba(232,232,232,1);
overflow: visible;
}
#FlexGroup {
position: absolute;
width: 589px;
left: 41px;
top: 88px;
display: flex;
flex-direction: column;
align-items: stretch;
justify-content: space-between;
flex-wrap: nowrap;
overflow: visible;
gap: 30px;
}
#content {
position: relative;
align-self: auto;
overflow: visible;
width: 590px;
text-align: left;
font-family: Malayalam Sangam MN;
font-style: normal;
font-weight: normal;
font-size: 20px;
color: rgba(34,44,65,1);
}
<div id="view1">
<div id="FlexGroup">
<div id="content">
<span>How to make container size to content. How to make container size to content. How to make container size to content. How to make container size to content. How to make container size to content. How to make container size to content. How to make container size to content. How to make container size to content. How to make</span>
</div>
</div>
</div>
解决方案
如果您在子元素中设置,父元素不知道其子position: absolute
元素。因此,父height
元素不会随着子元素的增长而增长。要使其工作,您需要position: relative
在子元素中设置
var text = document.getElementById("content");
text.innerText = document.body.innerText + document.body.innerText + document.body.innerText+ document.body.innerText;
* {
margin: 0;
padding: 0;
box-sizing: border-box;
border: none;
}
#view1 {
position: absolute;
background: #E5E5E5;
width: 671px;
height: auto;
background-color: rgba(232,232,232,1);
overflow: visible;
border: 1px solid red;
}
#FlexGroup {
position: relative;
width: 589px;
/* height:auto; */
display: flex;
flex-direction: column;
align-items: stretch;
justify-content: space-between;
flex-wrap: nowrap;
overflow: visible;
gap: 30px;
}
#content {
position: relative;
align-self: auto;
overflow: visible;
width: 590px;
text-align: left;
font-family: Malayalam Sangam MN;
font-style: normal;
font-weight: normal;
font-size: 20px;
color: rgba(34,44,65,1);
}
<div id="view1">
<div id="FlexGroup">
<div id="content">
<span>How to make container size to content. How to make container size to content. How to make containntto content. How to make container size to content. How to make</span>
</div>
</div>
</div>
推荐阅读
- c - C中的类型转换是香蕉?
- gradle - 在 Gradle 中,可以从 Groovy 方法中获取调用项目或任务吗?
- python - 未定义的变量“资源”
- c# - 阻止对特定域 CefSharp 的请求
- reactjs - 使用 rowEvents react-bootstrap-table 修改一行单元格的样式
- openiddict - 除了使用 OpenIddict 的 OpenId 之外,还允许 Basic 授权
- sas - 在 SAS Enterprise Guide 8.1 上打开 SAS 表后文件树消失
- javascript - 如何在 postgresql & express 中的其他占位符变量中插入日期范围?
- ubuntu - “无法初始化 NVML:驱动程序/库版本不匹配”- Ubuntu 服务器是否自动更新?
- bootstrap-4 - 如何仅将中心导航栏与 bootstrap 4.5.3 对齐?