[2] 하위 요소가 이미지인 경우 테이블 -cell은 사용할 수 없지만 상위 요소는 높이 대신 행 높이를 사용하고 글꼴 크기는 0으로 설정됩니다. 하위 요소 자체는 Vertical-align:middle
<style>
.parent{
display:table-cell;
vertical-align: middle;
}
.child{
display: table;
margin: 0 auto;
}
</style>
<p class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<p class="child" style="background-color: lightblue;">测试文字</p>
</p>
아이디어 4: 절대 사용
【1】절대 위치 요소의 박스 모델 기능을 사용하고 오프셋 속성 을 기준으로 margin:auto
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 0;
left: 0;
rightright: 0;
bottombottom: 0;
height: 50px;
width: 80px;
margin: auto;
}
</style>
를 결정된 값으로 설정합니다. 🎜>
<p class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<p class="child" style="background-color: lightblue;">测试文字</p>
</p>
【2】절대 위치에 있는 요소의 오프셋 속성과 번역()
함수 자체 오프셋을 사용하여 수준에 도달 세로 센터링 효과 [참고] IE9 브라우저는
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
rrree
를 지원하지 않습니다.[3] 너비와 높이가 아래의 하위 요소는 알려져 있습니다. 음수 여백 값을 사용하여 수평 및 수직 중앙 정렬 효과를 얻을 수 있습니다.
<p class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<p class="child" style="background-color: lightblue;">测试文字</p>
</p>
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
width: 80px;
height: 60px;
margin-left: -40px;
margin-top: -30px;
}
</style>
아이디어 5: flex 사용 [참고] IE9 브라우저는
[1] margin:auto
<p class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<p class="child" style="background-color: lightblue;">测试文字</p>
</p>
rrree
[2] 확장 가능한 프로젝트의 경우 확장 가능한 컨테이너에서 주축 정렬 justify-content 및 교차 축 정렬 align-items를 사용합니다.
<style>
.parent{
display: flex;
}
.child{
margin: auto;
}
</style>
<p class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<p class="child" style="background-color: lightblue;">测试文字</p>
</p>
위 기사에 대한 간략한 분석 CSS로 수평 및 수직 센터링을 동시에 달성하기 위한 5가지 아이디어는 모두 편집자가 공유한 내용이므로 참고가 되셨으면 좋겠습니다. PHP 중국어 웹사이트.