flex-direction: row คืออะไร?
ค่า row เป็นค่าเริ่มต้น (default) ของ flex-direction ทำให้ flex items เรียงตัว แนวนอน (Horizontal) จาก ซ้ายไปขวา ตามลำดับใน HTML (สำหรับ LTR languages)
ลักษณะการทำงาน
- Main Axis: แนวนอน (Horizontal) - ซ้าย → ขวา
- Cross Axis: แนวตั้ง (Vertical) - บน → ล่าง
- justify-content: ควบคุมแนวนอน
- align-items: ควบคุมแนวตั้ง
ตัวอย่างการใช้งาน
<!DOCTYPE html>
<html>
<head>
<style>
.mycontainer {
display: flex;
flex-direction: row; /* เรียงแนวนอน ซ้าย→ขวา (default) */
background-color: #fff3e0;
padding: 10px;
gap: 10px;
}
.mycontainer > div {
background-color: white;
border: 2px solid #ff9800;
text-align: center;
line-height: 50px;
font-size: 20px;
width: 100px;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>ตัวอย่าง flex-direction: row</h1>
<p>Items เรียงจากซ้ายไปขวา: Q1, Q2, Q3, Q4, Q5, Q6</p>
<div class="mycontainer">
<div>Q1</div>
<div>Q2</div>
<div>Q3</div>
<div>Q4</div>
<div>Q5</div>
<div>Q6</div>
</div>
</body>
</html>
การใช้งานจริง: Navigation Bar
.navbar {
display: flex;
flex-direction: row; /* default - สามารถละได้ */
justify-content: space-between;
align-items: center;
padding: 16px 24px;
background: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.nav-brand {
font-weight: 700;
font-size: 1.25rem;
}
.nav-menu {
display: flex;
gap: 24px;
list-style: none;
}
.nav-actions {
display: flex;
gap: 12px;
}
การใช้งานจริง: Horizontal Card List
.card-list {
display: flex;
flex-direction: row;
flex-wrap: wrap; /* ขึ้นบรรทัดใหม่บนมือถือ */
gap: 24px;
padding: 24px;
}
.card {
flex: 1 1 300px; /* grow, shrink, basis */
max-width: 100%;
background: white;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
overflow: hidden;
}
RTL (Right-to-Left) Languages
/* สำหรับภาษาที่อ่านขวาไปซ้าย (อาหรับ, ฮีบรู) */
html[dir="rtl"] .container {
flex-direction: row-reverse; /* เรียงจากขวาไปซ้าย */
}
/* หรือใช้ logical properties */
.container {
flex-direction: row; /* ใช้ logical: inline-start → inline-end */
}
เปรียบเทียบ Row vs Column
| คุณสมบัติ | row (default) | column |
|---|---|---|
| Main Axis | Horizontal | Vertical |
| Cross Axis | Vertical | Horizontal |
| justify-content | จัดแนวนอน | จัดแนวตั้ง |
| align-items | จัดแนวตั้ง | จัดแนวนอน |
| เหมาะสำหรับ | Navbar, Toolbar, Inline items | Sidebar, Form, Stack layout |
คำแนะนำ
rowเป็นค่าเริ่มต้น สามารถละเว้นได้ถ้าไม่ต้องการเปลี่ยนทิศทาง- ใช้
flex-wrap: wrapร่วมกันสำหรับ Responsive (row wrap) - ใช้
gapสำหรับระยะห่างแทน margin - สำหรับ RTL ให้ใช้
dir="rtl"บน html tag แทนการเปลี่ยน flex-direction