css pie.htc使用总结

pie.htc可以让ie支持CSS3的一些特性,但并不总是有效,使用时经常会碰到下面的一些问题

1. z-index相关问题
IE下这些css3效果实现是借助于VML,由VML绘制圆角或是投影效果的容器元素,然后这个容器元素作为目标元素的后兄弟节点插入,如果目标元素position:absolute 或是 position:relative,则这个css3-Container元素将会设置与之一样的z-index值,在DOM tree中,同级的元素总是后面的覆盖前面的,所以这样就实现了覆盖,又避免了可能有其他元素正好插入其中。

所以,问题来了,如果目前元素的position属性为static,也就是默认属性,则z-index属性是没有用的,无覆盖可言,所以此时IE浏览器下CSS3的渲染是不会成功的。要解决也很简单,设置目标元素position:relative或是设置祖先元素position:relative并赋予一个z-index值(不可为-1)

2. 相当路径的问题
IE浏览器的behavior 属性是相对于HTML文档而言的,与CSS其他的属性不一样,不是相对于CSS文档而言的。这使得使用pie.htc文件不怎么方面。如果绝对路径于根目录,则CSS文件不方便移动;如果相对路径与HTML文档,则pie.htc文件在不同HTML页面见的重用性大大降低。同时,诸如border-image后面的URL属性路径也不好处理。

3. 缩写的问题
使用PIE实现IE下的CSS3渲染(其他方法也是一样),只能使用缩写的形式,例如圆角效果,我们可以设置border-top-left-radius表示左上圆角,但是PIE确实不支持这种写法的,只能是老老实实的缩写。

4. 提供正确的Content-Type
要想让IE浏览器支持htc文件,需要一个有着”text/x-component” 字样的content-type 头部,否则,会忽视behavior。绝大数web服务器提供了正确的content-type,但是还有一部分则有问题。例如的我的空间域名商就没有”text/x-component” 字样的content-type,可能是出于安全的考虑。

如果您发现在您的机子上PIE方法无效,也就是htc文件这里指pie.htc文件无效,检查您的服务器配置,可能其需要更新到最新的content-type。例如对于Apache,您可以在.htaccess文件中左如下处理:

在django中使用嗯PIE.htc的方式如下:
配置url.py:

[python] view plain copy
 
  1. from django.conf.urls.defaults import *  
  2. from django.views.generic.simple import redirect_to  
  3. from django.core.servers.basehttp import FileWrapper  
  4. from django.http import HttpResponse  
  5. import os  
  6. import settings  
  7.   
  8. # Uncomment the next two lines to enable the admin:  
  9. from django.contrib import admin  
  10. admin.autodiscover()  
  11.   
  12. def pie_with_headers(request):  
  13.     filename = settings.STATIC_PATH + '/scripts/css/PIE.htc'  
  14.     wrapper = FileWrapper(open(filename))  
  15.     response = HttpResponse(wrapper, content_type='text/x-component')  
  16.     response['Content-Length'] = os.path.getsize(filename)  
  17.     return response  
  18.   
  19. urlpatterns = patterns('',  
  20.   
  21.     # Uncomment the next line to enable the admin:  
  22.     (r'^admin/service/appinfo/(?P<id>d+)/delete','service.admin_views.app_delete'),  
  23.     (r'^admin/service/appinfo/(?P<id>d+)','service.admin_views.app_edit'),  
  24.     (r'^admin/service/appinfo/add','service.admin_views.app_edit'),  
  25.     (r'^admin/', include(admin.site.urls)),  
  26.     (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root':'/opt/dev/website/kakaservice/media', 'show_indexes': True}),  
  27.     (r'^content/(?P<path>.*)$', 'django.views.static.serve', {'document_root':'/opt/dev/website/kakaservice/templates/content', 'show_indexes': True}),  
  28.     (r'^scripts/(?P<path>.*)$', 'django.views.static.serve', {'document_root':'/opt/dev/website/kakaservice/templates/scripts', 'show_indexes': True}),  
  29.     (r'^PIE.htc',pie_with_headers,{}),  
  30.     (r'^$', 'service.views.index'),  
  31. )  



然后在相应的css文件中使用(使用时需要注意相对路径问题,最好通过django server的access信息来判断请求的路径):

.page .content .right .item .icon{
    140px;
    height: 141px;
    padding: 5px;
    background-color: #ffffff;
    margin: 15px auto;
    font-size: 0;
    position: relative;
    -webkit-border-radius: 25px 25px 25px 25px;
    -moz-border-radius: 25px 25px 25px 25px;
    border-radius: 25px 25px 25px 25px;
    behavior: url(../PIE.htc);
}

原文地址:https://www.cnblogs.com/JerryWang24/p/7381490.html