<!-- html -->
<div id="parent">
<div id="child1">自适应自适应自适应自适应自适应自适应</div>
<div id="child2">自适应自适应自适应自适应自适应自适应</div>
</div>
<!-- css-->
<style>
/* 绝对定位 */
#child1 {
width: 100px;
height: 100px;
position: absolute;
left: 0;
top: 0;
background-color: lime;
}
#child2 {
margin-left: 100px;
background-color: red;
}
/* flex */
#parent {
display: flex;
}
#child1 {
flex-basis: 100px;
background-color: lime;
}
#child2 {
background-color: red;
}
</style>
<!-- html -->
<div class="parent">
<div class="child">div</div>
</div>
<!-- css-->
<style>
/* flex */
.parent {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 100px;
}
/* 绝对定位+transform */
.parent {
width: 100px;
height: 100px;
}
.child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
/* 绝对定位+margin */
.parent {
width: 100px;
height: 100px;
}
.child{
width: 40px;
height: 20px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -10px;
margin-left: -20px;
}
</style>