html - 绝对div父级内的css div子级
问题描述
对css有点新手问题。
我有一个居中的 div 父级(屏幕中心),position:absolute
现在我想在父级 div 中创建子级 div。他们应该有:
- 父母的全高
- 不同的宽度(应该加起来父的宽度)
- 也许填充,但我应该能够添加自己
很像不同大小的列。
到目前为止我所做的(仅限父 div,因为我尝试过的一切都真的搞砸了:():
.parent {
position: absolute;
width: 80%;
height: 75%;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
background: #000000;
}
<div class="parent">
</div>
解决方案
一个建议。不要position:absolute
用于布局。仅在必要时。排列元素的方法有很多,而不仅仅是位置:绝对。
不太确定你“搞砸了什么”,但你可以使用 flexbox -> add display:flex
on parent 并根据需要在其中安排你的子 div
在此处阅读有关flexbox和其他教程的更多信息
.parent {
position: absolute;
width: 80%;
height: 75%;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
background: #000000;
display: flex;
flex-wrap: wrap;
}
.child {
box-sizing: border-box;
}
.child1 {
background: red;
flex: 0 0 30%;
padding: 15px;
}
.child2 {
background: green;
flex: 0 0 50%;
}
.child3 {
background: blue;
flex-grow: 1;
}
<div class="parent">
<div class="child child1">text with padding</div>
<div class="child child2">no padding</div>
<div class="child child3">no padding</div>
</div>
推荐阅读
- javascript - 如何在保持一个元素可见的同时在两个部分之间切换?
- c++ - 输出不是我想要的 C++
- javascript - 如何删除类中的 eventListener?
- python - Pycharm不显示数据框
- graphql - 是否可以修改 graphql 输入模型以使用复合键而不是 id 来引用对象?
- xamarin - Xamarin Forms - 根据 Image 是否存在在控件上设置 IsVisible
- angular - Angular 4 中的 SetValue 控制 ngui-auto-complet
- swift - 如何快速将麦克风和 InApp Audio CMSampleBuffer 发送到 webRTC?
- x509certificate - 有没有人有一个在 Spring Cloud Gateway/Spring WebFlux 中实现 x509 相互身份验证的简单示例?
- android - UI 测试谷歌地图标记图标