CSS条件注释怎么用

发布时间:2022-03-10 10:38:00 作者:小新
来源:亿速云 阅读:122

这篇文章给大家分享的是有关CSS条件注释怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

由于浏览器版本的不同,对CSS里某些元素的解释也不一样,针对浏览器版本不同而选择不同CSS的代码,
其实我们还可以利用条件注释的方法来达到类似的目的,什么是条件注释,在此简单介绍一下,无非就是一些if判断啦,呵呵,但这些判断不是在脚本里执行的,而是直接在html代码里执行的,下面来介绍一下使用方法吧。


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

这里XXX是一些特定的东东,在此列表几个出来,详细介绍各自的含义:


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

上面是几个常用的判断IE浏览器版本的语法,下面再来介绍一下相对比较少用的逻辑判断的参数:
有几个参数:lte,lt,gte,gt及!
各自的详细解释如下:
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 /

看到这里相信大家都已经明白了条件注释的用法了,OK,那来举个例子吧:


<!-- 默认先调用css.css样式表 -->
<link rel="stylesheet" type="text/css" xhref="css.css" />
<!--[if !IE]>
<!&ndash; 非IE下调用1.css样式表 &ndash;>
<link rel=”stylesheet” type=”text/css” xhref=”1.css” />
<![endif]&ndash;>
<!&ndash;[if lt IE 6]>
<!&ndash; 如果IE浏览器版本小于6,调用2.css样式表 &ndash;>
<link rel=”stylesheet” type=”text/css” xhref=”2.css” />
<![endif]&ndash;>

定义什么浏览器下显示什么内容。
这个dropmenu(下拉菜单)模型来自cssplay,使经过作者多次的研究和反复的测试才做出来的。我想那这个模型来实践一下条件注释的原理。
先看一个最简单的模型
下面是xhtm:

复制代码

代码如下:


<div class="menu"> 
<ul> 
<li><a class="drop" xhref="../menu/index.html">DEMOS 
<!--[if IE 7]><!&ndash;> 
</a> 
<!&ndash;<![endif]&ndash;> 
<!&ndash;IE7时显示</a>标签&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时显示</a>标签&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时候,在final_drop.css基础上补充一个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; 

/*定义鼠标滑过样式*/ 
.menu ul li:hover ul { 
display:block; 
position:absolute; 
top:3em; 
margin-top:1px; 
left:0; 
width:150px; 

在非IE下,看到鼠标滑过时候li包含的ul显示了,因为这些浏览器支持li:hover用法
IE下的css


.menu ul li a:hover { 
color:#fff; 
background:#bd8d5e; 

/*当鼠标滑过时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样式,无鼠标时间时候li包含的ul不显示
因为


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

感谢各位的阅读!关于“CSS条件注释怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 条件注释判断浏览器版本
  2. HTML条件注释的用法案例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css

上一篇:swiper小程序开发示例分析

下一篇:如何接入微信小程序消息服务

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》