เราสามารถใช้เปอร์เซ็นต์และ em ร่วมกันเพื่อระบุขนาดฟอนต์ขององค์ประกอบเพื่อให้เข้ากันได้ดีกับฟอนต์ ซึ่งช่วยให้เรามีข้อความที่เหมือนกันในเบราว์เซอร์ต่างๆ
ไวยากรณ์
ไวยากรณ์ของคุณสมบัติขนาดแบบอักษร CSS มีดังต่อไปนี้ -
Selector {
font-size: /*value*/
} ตัวอย่าง
ตัวอย่างต่อไปนี้แสดงให้เห็นว่าคุณสมบัติขนาดแบบอักษร CSS สามารถตั้งค่าด้วยคำหลักได้อย่างไร -
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 80%;
}
p {
font-size: 2em;
}
span {
font-size: 4em;
font-style: italic;
}
</style>
</head>
<body>
<p>Reading <span>source code</span> written by others gives you opportunity to criticize the
mistakes done in writing that code</p>
</body>
</html> ผลลัพธ์
สิ่งนี้ให้ผลลัพธ์ต่อไปนี้ -

ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 120%;
}
p {
font-size: 2em;
}
</style>
</head>
<body>
<h3>Demo Heading</h3>
This is example text.
<div>
Examples are easier to understand with practical implementation
<p>This is another text just to display different ways to set font size in CSS.</p>
</div>
</body>
</html> ผลลัพธ์
สิ่งนี้ให้ผลลัพธ์ต่อไปนี้ -
