Ghi đè css quan trọng bằng javascript

Trong bài viết này, chúng ta sẽ tìm hiểu cách chúng ta có thể ghi đè kiểu nội tuyến bằng CSS bên ngoài. Nói chung, chúng tôi sử dụng CSS nội tuyến để ghi đè tất cả các kiểu khác. Trong một số trường hợp, chúng ta phải làm ngược lại. Chúng tôi phải ghi đè CSS nội tuyến đến từ các nguồn nước ngoài và không thể xóa được

Tiếp cận. Để ghi đè CSS nội tuyến,. từ khóa quan trọng được sử dụng. Điều này làm cho thuộc tính CSS đứng trước tất cả các thuộc tính CSS khác cho phần tử đó

từ khóa đã sử dụng

  • quan trọng. Từ khóa này có thể được sử dụng với thuộc tính CSS trong CSS nội tuyến, nội bộ hoặc bên ngoài. Điều này chỉ định rằng thuộc tính mà nó được sử dụng sẽ được ưu tiên cao nhất cho phần tử đó

Ví dụ dưới đây minh họa cách tiếp cận trên

ví dụ 1. Đoạn mã dưới đây minh họa cách CSS bên ngoài thay đổi màu sắc của tiêu đề bằng cách sử dụng. từ khóa quan trọng

HTML




a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
1

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
3
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
4
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
6
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
8

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2____20____17

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
23
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
24
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
26
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
23
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
31
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
33
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
34
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
36
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
23
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
41
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
43
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
34
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
46____147
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
51
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
52
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
54
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
55
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
57
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
61
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
46____164

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
66
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
61
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
66
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
0
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
8

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2____174
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
78
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
79
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
81
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
46____184

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
66____178
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
89

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
22
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
79
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
25
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
46____128

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
66______122
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
89

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
21
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
2
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
06
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
338
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
340
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
79
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
343
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7

Dấu phân cách

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
15 theo sau là từ khóa
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
16 đánh dấu tuyên bố là quan trọng. Cờ
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 thay đổi các quy tắc chọn khai báo bên trong tầng. Một khai báo không quan trọng được gọi là bình thường

Để đánh dấu một tuyên bố là quan trọng, hãy thêm cờ quan trọng [

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17] sau giá trị trong tuyên bố. Mặc dù khoảng trắng được cho phép giữa dấu phân cách và từ khóa, cờ thường được viết là
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 mà không có bất kỳ khoảng trắng nào

selector {
  property: value; /* normal declaration */
  property: value !important; /* important declaration [preferred] */
  property: value ! important; /* important declaration [not preferred] */
}

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 xuất hiện sau giá trị của khai báo cặp giá trị thuộc tính, trước ít nhất một khoảng trắng. Cờ quan trọng phải là mã thông báo cuối cùng trong khai báo. Nói cách khác, có thể có khoảng trắng và nhận xét giữa cờ và dấu chấm phẩy kết thúc của tuyên bố, ngoài ra không có gì khác

Tác động lên dòng thác

Khi nói đến các khai báo quan trọng, thứ tự tầng gốc và tầng được đảo ngược. Nếu không có cờ quan trọng, các khai báo trong biểu định kiểu của tác giả sẽ ghi đè các khai báo trong biểu định kiểu của người dùng, khai báo này sẽ ghi đè các khai báo trong biểu định kiểu mặc định của tác nhân người dùng

Khi một tuyên bố là quan trọng, thứ tự ưu tiên được đảo ngược. Các khai báo được đánh dấu là quan trọng trong biểu định kiểu tác nhân người dùng sẽ ghi đè tất cả các khai báo quan trọng trong biểu định kiểu tác nhân người dùng. Tương tự, tất cả các khai báo quan trọng trong biểu định kiểu của người dùng sẽ ghi đè lên tất cả các khai báo quan trọng trong biểu định kiểu của tác giả. Cuối cùng, tất cả các khai báo quan trọng được ưu tiên hơn tất cả các hoạt ảnh

Ghi chú. Tất cả các khai báo quan trọng được ưu tiên hơn tất cả các hoạt ảnh.

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 không hợp lệ trong khai báo hoạt hình @keyframes

Đảo ngược thứ tự ưu tiên cho các khai báo quan trọng đảm bảo người dùng có nhu cầu đặc biệt, chẳng hạn như phối màu được cá nhân hóa hoặc phông chữ lớn, có thể ghi đè kiểu tác giả khi cần bằng cách đánh dấu một số khai báo trong biểu định kiểu của người dùng là quan trọng. Nó cũng đảm bảo các tiện ích mở rộng độc hại không thể ghi đè các kiểu tác nhân người dùng quan trọng, có thể phá vỡ chức năng hoặc tác động tiêu cực đến bảo mật

Có bất cứ điều gì được ưu tiên hơn các tuyên bố quan trọng? . Chuyển đổi CSS là một cách để kiểm soát tốc độ thay đổi thuộc tính từ giá trị này sang giá trị khác. Trong khi chuyển đổi từ giá trị này sang giá trị khác, một thuộc tính sẽ không khớp với một khai báo quan trọng cụ thể

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}

Trong ví dụ này, thuộc tính

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
4 và
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
5 sẽ chuyển sang trạng thái lơ lửng trong hai giây. Mặc dù trạng thái mặc định là khai báo bình thường và trạng thái di chuột là khai báo
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17, quá trình chuyển đổi vẫn xảy ra

tầng lớp

Trong mỗi một trong ba nguồn gốc của biểu định kiểu - tác giả, người dùng và tác nhân người dùng - các khai báo thông thường trong các kiểu không có lớp sẽ ghi đè các khai báo kiểu có lớp, với lớp được khai báo cuối cùng có quyền ưu tiên hơn các lớp được khai báo trước nó. Các khai báo quan trọng đảo ngược thứ tự ưu tiên. các khai báo quan trọng ở lớp đầu tiên được ưu tiên hơn các khai báo quan trọng ở lớp tiếp theo, v.v. Ngoài ra, tất cả các khai báo quan trọng được ưu tiên hơn các khai báo quan trọng được thực hiện bên ngoài bất kỳ lớp nào

kiểu nội tuyến

Kiểu nội tuyến là kiểu được xác định bằng thuộc tính

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
7. Chúng cũng có thể là bình thường hoặc quan trọng. Các kiểu thông thường nội tuyến được ưu tiên hơn tất cả các khai báo thông thường, bất kể nguồn gốc. Các kiểu quan trọng nội tuyến được ưu tiên hơn tất cả các kiểu tác giả quan trọng khác, bất kể lớp nào, nhưng các kiểu quan trọng từ biểu định kiểu của người dùng hoặc tác nhân người dùng và chuyển tiếp sẽ ghi đè chúng

quan trọng và cụ thể

Mặc dù

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 không phải là một phần của việc xác định tính đặc hiệu, nhưng nó có liên quan. Các khai báo quan trọng sẽ ghi đè tất cả các khai báo khác từ cùng một lớp gốc và tầng

#myElement#myElement#myElement .myClass.myClass p:hover {
  color: blue;
}

p {
  color: red !important;
}

Ví dụ này hiển thị trường hợp chỉ định quá mức bộ chọn. Cho dù mức độ cụ thể của bộ chọn phù hợp với một khai báo thông thường cao đến mức nào, thì một khai báo quan trọng từ cùng một nguồn và tầng tầng sẽ luôn được ưu tiên. Trong trường hợp này, đoạn văn sẽ luôn có màu đỏ

Khi hai khai báo quan trọng từ cùng một nguồn gốc và lớp áp dụng cho cùng một phần tử, các trình duyệt sẽ chọn và sử dụng khai báo có tính đặc hiệu cao nhất

#myElement p {
  color: green !important;
}

p {
  color: purple !important;
}

Trong trường hợp này, tính đặc hiệu của bộ chọn có vấn đề. Chỉ khi các bộ chọn có cùng độ đặc hiệu thì thứ tự nguồn mới quan trọng

Tác động đến các thuộc tính tốc ký

Khai báo một thuộc tính tốc ký với

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 đặt tất cả các thuộc tính phụ là quan trọng. Hai khối kiểu bộ chọn sau đây là tương đương

p {
  background: blue !important;
}

p {
  background-image: none !important;
  background-position: 0 0 !important;
  background-size: auto auto !important;
  background-repeat: repeat !important;
  background-origin: padding-box !important;
  background-clip: border-box !important;
  background-attachment: scroll !important;
  background-color: blue !important;
}

Ví dụ này cho thấy một trong nhiều lý do tránh cờ quan trọng thường được khuyến nghị

Tác động đến thuộc tính tùy chỉnh

Khi cờ

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 được thêm vào khai báo giá trị thuộc tính tùy chỉnh, nó làm cho việc gán giá trị trở nên quan trọng. Cờ
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 sau đó bị tước khỏi giá trị thuộc tính tùy chỉnh. Cờ
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 không được chuyển như một phần của giá trị thuộc tính tùy chỉnh cho hàm
#myElement#myElement#myElement .myClass.myClass p:hover {
  color: blue;
}

p {
  color: red !important;
}
3

:root {
  --myColor: red !important;
  --myColor: blue;
}
p {
  color: var[--myColor];
  }
blockquote {
   color: var[--myColor];
   color: purple;
}

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
0

Trong ví dụ này, đoạn văn sẽ có màu đỏ, không phải màu xanh, vì việc gán giá trị thuộc tính tùy chỉnh rất quan trọng. Blockquote sẽ có màu tím, vì khai báo bình thường màu tím xuất hiện sau khai báo bình thường màu đỏ

Thực hành tốt nhất

Tránh sử dụng

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 để ghi đè tính đặc hiệu. Khi cố tình tạo các khai báo quan trọng cho các yêu cầu giao diện người dùng, hãy nhận xét trong mã CSS của bạn để giải thích cho người bảo trì lý do tại sao họ không nên ghi đè tính năng đó

Ngay cả khi làm việc để ghi đè các kiểu có độ đặc hiệu cao không thuộc quyền kiểm soát của bạn, chẳng hạn như các kiểu trong plugin của bên thứ 3 được khai báo bằng bộ chọn id, bạn không cần sử dụng

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17. Thay vào đó, hãy cân nhắc nhập tập lệnh biểu định kiểu của bên thứ 3 vào một lớp có tên hoặc ẩn danh làm lớp xếp tầng đầu tiên của bạn, thay vì sử dụng
a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17. Miễn là các kiểu bên ngoài không bao gồm các khai báo quan trọng, các kiểu của bạn sẽ được ưu tiên hơn các kiểu widget, bất kể tính đặc hiệu

Nếu bạn cần ghi đè biểu định kiểu dáng bên ngoài có chứa các khai báo quan trọng, hãy tạo một tầng tầng chứa các giá trị thay thế cần thiết và khai báo lớp đó trước

khả năng tiếp cận

Các kiểu quan trọng từ biểu định kiểu người dùng được ưu tiên hơn các khai báo quan trọng của biểu định kiểu tác giả, nghĩa là việc thêm cờ

a {
  color: red !important;
  background-color: yellow;
  transition: all 2s linear;
}
a:hover {
  color: blue !important;
  background-color: orange !important;
}
17 vào kiểu của trang web sẽ không ngăn người dùng cá nhân có yêu cầu đặc biệt, chẳng hạn như phông chữ lớn, có thể ghi đè kiểu của bạn bằng cách thêm các kiểu quan trọng

Làm cách nào để thay đổi CSS quan trọng bằng JavaScript?

Để đặt kiểu nội tuyến CSS làm kiểu. quan trọng trong javascript, bạn phải sử dụng phần tử. phương thức setAttribute[] .

Bạn có thể ghi đè một CSS quan trọng không?

Có hai cách bạn có thể ghi đè lên một. thẻ quan trọng trong CSS. Bạn có thể thêm một quy tắc CSS khác bằng. thẻ quan trọng và sử dụng bộ chọn có độ đặc hiệu cao hơn .

Làm cách nào để ghi đè thuộc tính CSS trong JavaScript?

Trong JavaScript, bạn có thể sử dụng phương thức setAttribute[] để xác định thuộc tính cho một phần tử . Sử dụng phương pháp tương tự, bạn có thể ghi đè lên. tính chất quan trọng của một phần tử.

Bạn có thể thao tác CSS bằng JavaScript không?

Giờ đây, JavaScript là một ngôn ngữ mạnh mẽ, do đó, chúng ta không chỉ có thể thao tác với các phần tử HTML bằng nó mà còn chúng ta còn có thể sử dụng nó để thao tác với các thuộc tính CSS của bất kỳ trang web nào.

Chủ Đề