---
title: "CSS网页设计解决方案(Hacks & Issues)"
author: "water84222"
date: "2009-03-11T17:27:09Z"
tags: []
slug: "css-hacks-issues"
draft: false
lastmod: "2009-03-11T17:27:09Z"
id: 347505
url: "https://onlyLove.iteye.com/blog/347505"
---

1、说明本文阐述了8条我们发现的在用CSS设计中有用的解决方案。

2、浏览器特定的选择器

    英文地址：  http://www.solidstategroup.com/page/1592

    当你想在一个浏览器里改变样式而不像在其他浏览器中改变时，这些选择器很有用。

    IE6以下

   \*html{}

    IE 7 以下

\*:first-child+html {} * html {}

    只对IE 7

\*:first-child+html {}

    只对IE 7 和现代浏览器

html&gt;body {}

    只对现代浏览器(非IE 7)

html&gt;/\*\*/body {}

    最新的Opera 9以下版本

html:first-child {}

Safari

html\[xmlns\*=”"] body:last-child {}

    要使用这些选择器，请在样式前写下这些代码。例如：

#content-box {  
　　width: 300px;  
　　height: 150px;  
}  
\* html #content-box {  
　　width: 250px;  
}  
/* 重写上面的代码并且把宽度改为250px  
在IE6以下版本中适用。 \*/

    3、在IE6中使用透明PNG图片

    IE6的一个很难处理的BUG就是它不支持透明PNG图片。

    你可能需要用一个重写的CSS的滤镜来解决这个问题：

\*html #image-style {  
　　background-image: none;  
　　filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=”filename.png”, sizingMethod=”scale”);  
}

    4、去掉连接虚线框

    当你点击链接时，Firefox会在链接周围产生一个虚线外框。  
　　点击在新窗口中浏览此图片  
    这个很好解决，只需在a标签中添加outline:none就可以了。

a{  
　　outline:none;  
}

    5、对inline元素应用宽度。

    如果你对一个inline元素使用宽度，它将只在IE6下起作用。

    所以的HTML标签要么是Block的要么就是inline的。inline属性的标签有&lt;span&gt;&lt;a&gt;&lt; strong&gt;和&lt;em&gt;Block标签包括&lt;div&gt;&lt;p&gt;&lt;h1&gt;&lt;form&gt; 和&lt;li&gt;

    你不能控制inilne标签的宽带，不过有一个方法是把标签属性从inline改为Block。

span{  
　　width:150px;  
　　display:block;  
}

    应用display:block能够把span标签变成block标签，从而控制它的宽度。

    6、使一个固定宽度的网站居中。

    为了让你的网站在浏览器中居中，可以为最外层Div添加position:relative属性，然后将margin设为auto。

#wrapper {  
　　margin: auto;  
　　position: relative;  
}

    7、图片替换技术

    对于头部来说，永远是最好用文字而不是图片。在你必须要用图片的某个特殊地方最好使用隐藏文字的层的背景图片。这对于屏幕阅读和SEO非常有用，尽管依然使用很普通的文字，这可以联想到所有的优点。

HTML：

&lt;h1&gt;&lt;span&gt;Main heading one&lt;/span&gt;&lt;/h1&gt;

CSS:

h1 {  
　　background: url(heading-image.gif) no-repeat;  
}  
h1 span {  
　　position:absolute;  
　　text-indent: -5000px;  
}

    正如你所见，我们对H1标签使用普通的HTML代码，用CSS来将图片替代文字。Text-indent把文字放到左边5000像素处，从而用户看不到它们

    8、最小宽度

    IE的另外一个Bug就是它不支持min-width属性。min-width确实非常有用，特别是对于100%宽度的可变模板来说，因为他告诉浏览器停止收缩。

    对于除IE6以外的所有浏览器来说你只需min-width:xpx;例如：

.container {  
　　min-width:300px;  
}

    要让这些在IE6下起作用的话你要添加额外的努力！你需要创建两个DIV，一个包含着另一个。

&lt;div class=”container”&gt;  
&lt;div class=”holder”&gt;Content&lt;/div&gt;  
&lt;/div&gt;

    然后你需要设置外面层的min-width：

.container {  
　　min-width:300px;  
}

    现在又要IE hack起作用了，你需要写下以下代码：

\* html .container {  
　　border-right: 300px solid #FFF;  
}  
\* html .holder {  
　　display: inline-block;  
　　position: relative;  
　　margin-right: -300px;  
}

    当浏览器窗口调整外层宽度来适应直到它缩小到border的宽度时，这个时候它就不能够在缩小了。而holder层也会停止收缩。外层的边框宽度变成了内层的最小宽度。

    9、隐藏水平滚动

    要去除水平滚动条，可以在body中插入overflow-x:hidden属性。

body {  
　　overflow-x: hidden;  
}