溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

什么是CSS條件注釋

發(fā)布時(shí)間:2021-09-28 14:51:19 來源:億速云 閱讀:109 作者:iii 欄目:web開發(fā)

這篇文章主要講解了“什么是CSS條件注釋”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“什么是CSS條件注釋”吧!

由于瀏覽器版本的不同,對CSS里某些元素的解釋也不一樣,針對瀏覽器版本不同而選擇不同CSS的代碼,
其實(shí)我們還可以利用條件注釋的方法來達(dá)到類似的目的,什么是條件注釋,在此簡單介紹一下,無非就是一些if判斷啦,呵呵,但這些判斷不是在腳本里執(zhí)行的,而是直接在html代碼里執(zhí)行的,下面來介紹一下使用方法吧。

代碼如下:

<!--[if XXX]>
這里是正常的html代碼
<![endif]&ndash;>

這里XXX是一些特定的東東,在此列表幾個(gè)出來,詳細(xì)介紹各自的含義:

代碼如下:

<!&ndash;[if IE]> / 如果瀏覽器是IE /
<!&ndash;[if IE 5]> / 如果瀏覽器是IE 5 的版本 /
<!&ndash;[if IE 6]> / 如果瀏覽器是IE 6 的版本 /
<!&ndash;[if IE 7]> / 如果瀏覽器是IE 7 的版本 /

上面是幾個(gè)常用的判斷IE瀏覽器版本的語法,下面再來介紹一下相對比較少用的邏輯判斷的參數(shù):
有幾個(gè)參數(shù):lte,lt,gte,gt及!
各自的詳細(xì)解釋如下:
lte:就是Less than or equal to的簡寫,也就是小于或等于的意思。
lt :就是Less than的簡寫,也就是小于的意思。
gte:就是Greater than or equal to的簡寫,也就是大于或等于的意思。
gt :就是Greater than的簡寫,也就是大于的意思。

例句:

代碼如下:

<!--[if gt IE 5.5]> / 如果IE版本大于5.5 /
<!&ndash;[if lte IE 6]> / 如果IE版本小于等于6 /
<!&ndash;[if !IE]> / 如果瀏覽器不是IE /

看到這里相信大家都已經(jīng)明白了條件注釋的用法了,OK,那來舉個(gè)例子吧:

代碼如下:

<!-- 默認(rèn)先調(diào)用css.css樣式表 -->
<link rel="stylesheet" type="text/css" xhref="css.css" />
<!--[if !IE]>
<!&ndash; 非IE下調(diào)用1.css樣式表 &ndash;>
<link rel=”stylesheet” type=”text/css” xhref=”1.css” />
<![endif]&ndash;>
<!&ndash;[if lt IE 6]>
<!&ndash; 如果IE瀏覽器版本小于6,調(diào)用2.css樣式表 &ndash;>
<link rel=”stylesheet” type=”text/css” xhref=”2.css” />
<![endif]&ndash;>

定義什么瀏覽器下顯示什么內(nèi)容。
這個(gè)dropmenu(下拉菜單)模型來自cssplay,使經(jīng)過作者多次的研究和反復(fù)的測試才做出來的。我想那這個(gè)模型來實(shí)踐一下條件注釋的原理。
先看一個(gè)最簡單的模型
下面是xhtm:

代碼如下:

<div class="menu">  
<ul>  
<li><a class="drop" xhref="../menu/index.html">DEMOS  
<!--[if IE 7]><!&ndash;>  
</a>  
<!&ndash;<![endif]&ndash;>  
<!&ndash;IE7時(shí)顯示</a>標(biāo)簽&ndash;>  
<table><tr><td>  
<ul>  
<li><a xhref=”../menu/zero_dollars.html” title=”The zero dollar ads page”>zero dollars advertising page</a></li>  
<li><a xhref=”../menu/embed.html” title=”Wrapping text around images”>wrapping text around images</a></li>  
<li><a xhref=”../menu/form.html” title=”Styling forms”>styled form</a></li>  
<li><a xhref=”../menu/nodots.html” title=”Removing active/focus borders”>active focus</a></li>  
<li><a class=”drop” xhref=”../menu/hover_click.html” title=”Hover/click with no active/focus borders”>hover/click with no borders</li>  
<li class=”upone”><a xhref=”../menu/shadow_boxing.html” title=”Multi-position drop shadow”>shadow boxing</a></li>  
<li><a xhref=”../menu/old_master.html” title=”Image Map for detailed information”>image map for detailed information</a></li>  
<li><a xhref=”../menu/bodies.html” title=”fun with background images”>fun with background images</a></li>  
<li><a xhref=”../menu/fade_scroll.html” title=”fade-out scrolling”>fade scrolling</a></li>  
<li><a xhref=”../menu/em_images.html” title=”em size images compared”>em image sizes compared</a></li>  
</ul>  
</td></tr></table>  
<!&ndash;[if lte IE 6]>  
</a>  
<![endif]&ndash;>  
</li>  
<!&ndash;IE6時(shí)顯示</a>標(biāo)簽&ndash;>  
</ul>  
</div>  

CSS

代碼如下:

<link rel="stylesheet" media="all" type="text/css" xhref="final_drop.css" />  
<!--[if lte IE 6]>  
<link rel=”stylesheet” media=”all” type=”text/css” xhref=”final_drop_ie.css” />  
<![endif]&ndash;>  

采用雙樣式,給ie和非ie分別定義樣式,如果IE時(shí)候,在final_drop.css基礎(chǔ)上補(bǔ)充一個(gè)final_drop_ie.css
先看看非ie下的css是怎樣定義的

代碼如下:

.menu ul li ul {  
display: none;  
}  
/* specific to non IE browsers */  
.menu ul li:hover a {  
color:#fff;  
background:#bd8d5e;  
}  
/*定義鼠標(biāo)滑過樣式*/  
.menu ul li:hover ul {  
display:block;  
position:absolute;  
top:3em;  
margin-top:1px;  
left:0;  
width:150px;  
}  

在非IE下,看到鼠標(biāo)滑過時(shí)候li包含的ul顯示了,因?yàn)檫@些瀏覽器支持li:hover用法
IE下的css

代碼如下:

.menu ul li a:hover {  
color:#fff;  
background:#bd8d5e;  
}  
/*當(dāng)鼠標(biāo)滑過時(shí)li包含的ul顯示*/  
.menu ul li a:hover ul {  
display:block;  
position:absolute;  
top:3em;  
left:0;  
background:#fff;  
margin-top:0;  
marg\in-top:1px;  
}  

繼承上面的final_drop.css樣式,無鼠標(biāo)時(shí)間時(shí)候li包含的ul不顯示
因?yàn)?/p>

代碼如下:

<!--[if lte IE 6]>
</a>
<![endif]&ndash;>

感謝各位的閱讀,以上就是“什么是CSS條件注釋”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對什么是CSS條件注釋這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI