>웹 프론트엔드 >HTML 튜토리얼 >CSS에서 !important의 역할

CSS에서 !important의 역할

WBOY
WBOY원래의
2016-12-05 13:26:282013검색

지정된 스타일 규칙의 적용 우선순위를 높입니다.

  • IE6 이하 브라우저에는 상대적으로 명시적인 지원 문제가 있으며, !important는 동일한 규칙 세트에서 적용되지 않습니다. 다음 코드를 살펴보세요:

    샘플 코드:

    div { color: #f00 !important; color: #000; }

    위 코드에서 IE6 이하 브라우저의 div 텍스트 색상은 #000이며, !important는 다른 브라우저의 div 텍스트 색상이 #f00이라는 규칙을 재정의하지 않습니다.

  • IE6 이하 브라우저에서 !important를 적용하려면 다음 코드를 사용할 수 있습니다.

    샘플 코드:

    div { color: #f00 !important; } div { color: #000; }

    위 코드에서 IE6 이하 브라우저의 div 텍스트 색상은 다른 브라우저와 동일하며 둘 다 #f00

 IE6 및 이전 브라우저에서는 !important가 동일한 규칙 세트에 적용되지 않습니다.

예:

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><!</span><span style="color: #ff00ff">DOCTYPE html</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff"><</span><span style="color: #800000">html</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 3</span>   <span style="color: #0000ff"><</span><span style="color: #800000">head</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 4</span>     <span style="color: #0000ff"><</span><span style="color: #800000">title</span><span style="color: #0000ff">></span>important的使用<span style="color: #0000ff"></</span><span style="color: #800000">title</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 5</span>     <span style="color: #0000ff"><</span><span style="color: #800000">meta </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="content-type"</span><span style="color: #ff0000"> content</span><span style="color: #0000ff">="text/html; charset=UTF-8"</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span> <span style="color: #0000ff"><</span><span style="color: #800000">style</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 7</span> <span style="background-color: #f5f5f5; color: #800000">.button </span><span style="background-color: #f5f5f5; color: #000000">{</span>
<span style="color: #008080"> 8</span> <span style="background-color: #f5f5f5; color: #ff0000">    position</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> relative</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080"> 9</span> <span style="background-color: #f5f5f5; color: #ff0000">    background-color</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> #4CAF50</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">10</span> <span style="background-color: #f5f5f5; color: #ff0000">    border-radius</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff">8px</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">11</span> <span style="background-color: #f5f5f5; color: #ff0000">    font-size</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 28px</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">12</span> <span style="background-color: #f5f5f5; color: #ff0000">    color</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> #FFFFFF</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">13</span> <span style="background-color: #f5f5f5; color: #ff0000">    padding</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 20px</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">14</span> <span style="background-color: #f5f5f5; color: #ff0000">    width</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 200px</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">15</span> <span style="background-color: #f5f5f5; color: #ff0000">    text-align</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> center</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">16</span> <span style="background-color: #f5f5f5; color: #ff0000">    transition-duration</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 1.5s</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">17</span> <span style="background-color: #f5f5f5; color: #ff0000">    overflow</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> hidden</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">18</span> <span style="background-color: #f5f5f5; color: #ff0000">    cursor</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> pointer</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">19</span>   <span style="background-color: #f5f5f5; color: #000000">}</span>
<span style="color: #008080">20</span> <span style="background-color: #f5f5f5; color: #800000">  .button:hover</span><span style="background-color: #f5f5f5; color: #000000">{</span>
<span style="color: #008080">21</span> <span style="background-color: #f5f5f5; color: #ff0000">    box-shadow</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff">0 4px 8px 0 rgba(0,0,0,0.1),0 6px 20px 0 rgba(0,0,0,0.299)</span><span style="background-color: #f5f5f5; color: #000000">;</span>  
<span style="color: #008080">22</span>   <span style="background-color: #f5f5f5; color: #000000">}</span>
<span style="color: #008080">23</span> 
<span style="color: #008080">24</span> <span style="background-color: #f5f5f5; color: #800000">  .button:after </span><span style="background-color: #f5f5f5; color: #000000">{</span>
<span style="color: #008080">25</span> <span style="background-color: #f5f5f5; color: #ff0000">    content</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> ""</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">26</span> <span style="background-color: #f5f5f5; color: #ff0000">    background</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> #90EE90</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">27</span> <span style="background-color: #f5f5f5; color: #ff0000">    display</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> block</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">28</span> <span style="background-color: #f5f5f5; color: #ff0000">    position</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> absolute</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">29</span> <span style="background-color: #f5f5f5; color: #ff0000">    padding-top</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 300%</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">30</span> <span style="background-color: #f5f5f5; color: #ff0000">    padding-left</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 350%</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">31</span> <span style="background-color: #f5f5f5; color: #ff0000">    margin-left</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> -20px!important</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">32</span> <span style="background-color: #f5f5f5; color: #ff0000">    margin-top</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> -120%</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">33</span> <span style="background-color: #f5f5f5; color: #ff0000">    opacity</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 0</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">34</span> <span style="background-color: #f5f5f5; color: #ff0000">    transition</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff">1s</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">35</span> <span style="background-color: #f5f5f5; color: #000000">}</span>
<span style="color: #008080">36</span> 
<span style="color: #008080">37</span> <span style="background-color: #f5f5f5; color: #800000">.button:active:after </span><span style="background-color: #f5f5f5; color: #000000">{</span>
<span style="color: #008080">38</span> <span style="background-color: #f5f5f5; color: #ff0000">    padding</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 0</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">39</span> <span style="background-color: #f5f5f5; color: #ff0000">    margin</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 0</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">40</span> <span style="background-color: #f5f5f5; color: #ff0000">    opacity</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff"> 1</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">41</span> <span style="background-color: #f5f5f5; color: #ff0000">    transition</span><span style="background-color: #f5f5f5; color: #000000">:</span><span style="background-color: #f5f5f5; color: #0000ff">0s</span><span style="background-color: #f5f5f5; color: #000000">;</span>
<span style="color: #008080">42</span> <span style="background-color: #f5f5f5; color: #000000">}</span>
<span style="color: #008080">43</span> <span style="color: #0000ff"></</span><span style="color: #800000">style</span><span style="color: #0000ff">></span>
<span style="color: #008080">44</span> <span style="color: #0000ff"></</span><span style="color: #800000">head</span><span style="color: #0000ff">></span>
<span style="color: #008080">45</span> <span style="color: #0000ff"><</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>
<span style="color: #008080">46</span> 
<span style="color: #008080">47</span> <span style="color: #0000ff"><</span><span style="color: #800000">button </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="button"</span><span style="color: #0000ff">></span>Click Me<span style="color: #0000ff"></</span><span style="color: #800000">button</span><span style="color: #0000ff">></span>
<span style="color: #008080">48</span> <p>第31行的margin-left设置了!important,因此后面的第39行是不能更改margin-left的值。
<span style="color: #008080">50</span> <span style="color: #0000ff"></</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>
<span style="color: #008080">51</span> <span style="color: #0000ff"></</span><span style="color: #800000">html</span><span style="color: #0000ff">></span>

참고기사:

http://www.runoob.com/css3/css3-buttons.html

http://www.css88.com/book/css/rules/!important.htm

본 글은 해당 블로거의 원본 글입니다. 재인쇄가 필요하신 경우 출처를 밝혀주세요.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:HTML 공통 태그다음 기사:HTML 공통 태그