สำหรับการจัดตำแหน่งกึ่งกลางแนวนอนและแนวตั้งด้วย Flexbox ให้ใช้คุณสมบัติ align-ites ใน CSS3 ตั้งค่าคุณสมบัติ align-items ให้อยู่ตรงกลาง
ต่อไปนี้เป็นรหัสสำหรับการจัดตำแหน่งกึ่งกลางแนวนอนและแนวตั้งด้วย flexbox -
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.h-container {
display: flex;
width: 300px;
height: 300px;
background-color: lightblue;
justify-content: center;
align-items: center;
border: 2px solid black;
}
.h-1 {
font-size: 30px;
width: 150px;
height: 150px;
background-color: red;
color: white;
border: 2px solid rgb(58, 2, 83);
}
</style>
</head>
<body>
<h2>Horizontal and vertical center alignment using flexbox</h2>
<div class="h-container">
<div class="h-1">Centered Div</div>
</div>
</body>
</html> ผลลัพธ์
รหัสข้างต้นจะสร้างผลลัพธ์ต่อไปนี้ -
