การใช้คุณสมบัติ CSS text-align เราสามารถตั้งค่าข้อความขององค์ประกอบในแนวนอน เราสามารถตั้งค่าให้ชิดซ้าย ขวา ชิดขอบหรือตรงกลางได้
ไวยากรณ์
ไวยากรณ์ของคุณสมบัติ CSS text-align มีดังนี้ -
Selector {
text-align: /*value*/
} ตัวอย่าง
ตัวอย่างต่อไปนี้แสดงให้เห็นถึงคุณสมบัติการจัดข้อความ CSS -
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: auto;
padding: 8px;
max-width: 200px;
border: thin solid;
}
p {
text-align: right;
}
div:nth-child(3) {
text-align: center;
}
div:last-child {
text-align: justify;
}
</style>
</head>
<body>
<h2>Student Examination Details</h2>
<div>
<div>Student John</div>
<div>
Student Tom
<p>Did not appeared for the exams.</p>
</div>
<div>Student Brad</div>
<div>Did not appeared for only the last exam.</div>
</div>
</body>
</html> ผลลัพธ์
สิ่งนี้ให้ผลลัพธ์ต่อไปนี้ -

ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<style>
td {
padding: 10px;
box-shadow: inset 0 0 21px yellow;
}
td:first-of-type {
text-align: right;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<table>
<tr>
<td>This is it!</td>
</tr>
<tr>
<td>Well, this is a demo text!</td>
</tr>
</table>
</body>
</html> ผลลัพธ์
สิ่งนี้ให้ผลลัพธ์ต่อไปนี้ -
