Typography คือการจัดการตัวอักษรให้เหมาะกับการอ่านและบุคลิกของเว็บไซต์ เช่น font, ขนาด, น้ำหนัก, ระยะบรรทัด และการจัดแนว ข้อความที่อ่านง่ายช่วยให้ผู้ใช้เข้าใจเนื้อหาได้ดีขึ้น
font-familyfont-family กำหนดชนิดฟอนต์ ควรกำหนด font stack เพื่อให้ browser มีตัวเลือกสำรอง
body {
font-family: "Noto Sans Thai", Arial, sans-serif;
}
Web-safe fonts เช่น Arial, Georgia, Times New Roman, Verdana มักมีในเครื่องผู้ใช้จำนวนมาก
<link><link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@400;700&display=swap" rel="stylesheet">
@import@import url("https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@400;700&display=swap");
โดยทั่วไป <link> มักเหมาะกว่าในด้าน performance
font-sizeหน่วยที่ใช้บ่อย:
px: ค่าคงที่em: อิงจาก font-size ของ parentrem: อิงจาก root elementแนะนำใช้ rem สำหรับ accessibility เพราะผู้ใช้ปรับขนาดตัวอักษรระดับ browser ได้ง่ายกว่า
body {
font-size: 1rem;
}
h1 {
font-size: 2.25rem;
}
line-heightline-height ควบคุมระยะห่างระหว่างบรรทัด ค่าแนะนำสำหรับเนื้อหาอ่านยาวคือ 1.5 ถึง 1.8
p {
line-height: 1.7;
}
.title {
font-weight: 700;
font-style: normal;
text-decoration: none;
text-transform: none;
}
font-weight ใช้ได้ตั้งแต่ 100 ถึง 900 หาก font รองรับ
.label {
letter-spacing: 0.04em;
word-spacing: 0.1em;
text-align: center;
}
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
property ที่เกี่ยวข้อง:
letter-spacingword-spacingtext-aligntext-overflowwhite-spaceออกแบบ typography scale สำหรับหน้า course landing page โดยกำหนดขนาด h1, h2, p, .caption และ .button
<link> และ @import สำหรับ Google Fonts ต่างกันอย่างไรrem มีข้อดีอย่างไรline-height ที่เหมาะกับการอ่านเนื้อหายาวควรอยู่ประมาณใดtext-overflow: ellipsis ต้องใช้ร่วมกับ property ใดบ้าง