html - 如何在大 div 中居中更多 div
问题描述
我知道有很多教程可以帮助您将 div 居中放在另一个 div 中......但我的问题更大,到目前为止我还没有找到任何有用的解决方案。
一切都应该水平居中对齐。
并且紫色div有一些外部内容,所以它的高度不固定,我需要它留在红色div内(如果内容太多它显然可以滚动,但滚动应该是灰色div,不像红框中的那样)。
我试图玩定位,但绝对和相对的东西它一直在工作,直到我尝试放置孩子的孩子。
任何人都可以帮助我吗?
如果你没有心情为我写代码,你甚至可以给我一个有用的教程,因为我找到了很多关于父子定位的帮助,但对于父子孙的情况却没有......
非常感谢!
解决方案
我假设你正在寻找这样的东西?
https://codepen.io/akshaybhat301094/pen/MWprQvO
*{
box-sizing: border-box;
}
html, body {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 80%;
height: 80%;
border: 5px solid gray;
display: flex;
justify-content: center;
align-items: center;
}
.main {
width: 80%;
height: 80%;
border: 5px solid red;
display: flex;
justify-content: flex-end;
align-items: center;
flex-direction: column;
gap: 10px;
}
.logo {
width: 60%;
height: 80px;
border: 5px solid green;
}
.grid {
width: 80%;
height: 80px;
border: 5px solid blue;
}
.expandable {
width: 60%;
max-height: 120px;
min-height: 100px;
border: 5px solid purple;
margin-bottom: 10px;
}
<div class="container">
<div class="main">
<div class="logo"></div>
<div class="grid"></div>
<div class="expandable"></div>
</div>
</div>
推荐阅读
- php - Laravel Eloquent ID 作为数组键
- android - 解析存储在 ionic 3 中的常量中的 JSON 字符串
- java - JavaFX - 加载下一个场景时保留工具栏
- c++ - 如何将 C 类型“char *”存储到 C++ 向量中?
- selenium - 如何在 Appium 中使用现有的应用程序会话
- python - 没有名为 graphframes 的模块 Jupyter Notebook
- angular - 使用新值复制和更新对象并存储到数组
- gemfire - 无法为嵌入式 Apache Geode 缓存服务器操作在 Spring 上下文中定义的区域
- php - cart.php 只为最后一个产品返回想要的值
- javascript - Cytsocape.js 无法创建不存在目标的边缘