inline-styles
ドキュメント内の優先度が最も高いので、たとえば、div
要素の色をに変更したいが、プロパティをに設定しblue
たinline style
としcolor
ますred
<div style="font-size: 18px; color: red;">
Hello World, How Can I Change The Color To Blue?
</div>
div {
color: blue;
/* This Won't Work, As Inline Styles Have Color Red And As
Inline Styles Have Highest Priority, We Cannot Over Ride
The Color Using An Element Selector */
}
それで、jQuery / Javascriptを使用する必要がありますか?-答えはNOです
ここでは、element-attr
CSSセレクターを使用できます!important
。これ!important
は重要です。そうしないと、インラインスタイルを上書きしません。
<div style="font-size: 30px; color: red;">
This is a test to see whether the inline styles can be over ridden with CSS?
</div>
div[style] {
font-size: 12px !important;
color: blue !important;
}
デモ
注:!important
ここではONLYを使用div[style]
できますdiv
が、セレクターを使用
して特定のstyle
属性を選択しています