温馨提示×

温馨提示×

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

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

什么是CSS条件注释

发布时间:2021-09-28 14:51:19 来源:亿速云 阅读:104 作者:iii 栏目:web开发

这篇文章主要讲解了“什么是CSS条件注释”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“什么是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]&ndash;>

感谢各位的阅读,以上就是“什么是CSS条件注释”的内容了,经过本文的学习后,相信大家对什么是CSS条件注释这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

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

css
AI