เพื่อรวมกฎ stylesheet ให้ใช้แอตทริบิวต์ <style> หรือ style attribute หรือ external stylesheet ด้วย <link>
แอตทริบิวต์ <link> ใช้รวมไฟล์ external style sheet ในเอกสาร HTML
External style sheet คือไฟล์ข้อความแยกต่างหากมีนามสกุล .css กำหนด Style rules ทั้งหมดในไฟล์นี้ แล้วรวมไฟล์นี้ในเอกสาร HTML ใดๆ ด้วย <link>
Syntax การรวม external CSS file:
<head> <link href="..." media="..." /> </head>
แอตทริบิวต์ของ <link> element:
| แอตทริบิวต์ | ค่า | คำอธิบาย |
|---|---|---|
| Type | text/css | ระบุภาษา style sheet เป็น content-type (MIME type) จำเป็น |
| Href | URL | ระบุไฟล์ style sheet ที่มี Style rules จำเป็น |
| media | screen, tty, tv, projection, handheld, print, braille, aural, all | ระบุอุปกรณ์ที่จะแสดงเอกสาร ค่าเริ่มต้นคือ all ไม่บังคับ |
สมมติ style เป็น new.css:
h1, h2 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}ตอนนี้คุณสามารถรวมไฟล์ new.css ในเอกสาร HTML ใดๆ ได้ดังนี้:
<head> <link rel="stylesheet" href="mystyle.css" media="all" /> </head>