Giới thiệu
Thuộc tính fontStyle trong HTML DOM Style dùng để thiết lập hoặc lấy kiểu font của văn bản. Nó cho phép chỉ định kiểu normal, italic hoặc oblique cho văn bản của một phần tử.
Cú pháp
Thiết lập fontStyle:
object.style.fontStyle = "normal|italic|oblique|initial|inherit"
Giá trị
| Giá trị | Mô tả |
|---|---|
| normal | Font bình thường (mặc định). |
| italic | Font nghiêng (italic). |
| oblique | Font nghiêng nhân tạo (oblique) - thường giống italic nếu font không có phiên bản oblique riêng. |
| initial | Đặt về giá trị mặc định. |
| inherit | Kế thừa từ phần tử cha. |
Ví dụ
Ví dụ thay đổi font-style thành italic cho hai div.
<!DOCTYPE html> <html> <head> <style>
#demo2, #demo1 {
font-family: 'times new roman';
font-size: 25px;
}
</style> <script>
function changeFontStyle() {
document.getElementById("demo1").style.fontStyle = "italic";
document.getElementById("demo2").style.fontStyle = "italic";
document.getElementById("Sample").innerHTML = "Font style đã thay đổi cho các đoạn văn trên";
}
</script> </head> <body>
<div id="demo1">This is demo text 1</div>
<div id="demo2">This is demo text 2</div>
<p>Nhấn nút để thay đổi font style của các div</p>
<button onclick="changeFontStyle()">Thay đổi Font Style</button>
<p id="Sample"></p>
</body> </html>
Kết quả

Sau khi nhấn nút Thay đổi Font Style:
