返回正常中文阅读

想对这篇译文“指手画脚”吗?

您的参与将有助于译者提高译文的质量;同时,大家一起对问题的讨论也是最佳的学习方式。还等什么?请现在就注册登录译言,开始眉批!
大错 小错 不顺 建议

CSS Hacks & Issues

CSS Hacks & Issues

Download this white paper in Adobe PDF format

1.   Introduction

This article includes 8 helpful solutions which we find essential when designing with CSS

2.   Browser-Specific selectors

These selectors are very useful when you want to change a style in one browser but not the others.

IE 6 and below

    * html {}

IE 7 and below

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

IE 7 only

    *:first-child+html {}

IE 7 and modern browsers only

    html>body {}

Modern browsers only (not IE 7)

    html>/**/body {}

Recent Opera versions 9 and below

    html:first-child {}

Safari

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

To use these selectors, place the code in front of the style. E.g.:

    #content-box {
            width: 300px;
            height: 150px;
            }

    * html #content-box {
            width: 250px;
            } /* overrides the above style and changes the width to 250px in IE 6 and below */

Source

3.   Transparent PNG’s in IE6

An IE6 bug which causes a great deal of hassle is that it doesn’t support transparent PNG images.

You will need to use a filter which overrides the CSS.

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

4.   Removing dotted links

Firefox produces a dotted outline that appears around links when you click on them.

Dotted-Links

This is simple to stop. Just add outline:none to the a tag

    a {
            outline: none;
            }

5.   Applying a width to inline elements

If you apply a width to an inline element it will only work inIE6. This is actually a fault of IE6 - inline elements shouldn't need to have a width assigned to them. However it is often useful for example if you wanted all labels in a form to be the same width.

All HTML elements are either a block or an inline element. Inline elements include span, a, strong and em. Block elements include div, p, h1, form and li.

You can’t change the width of an inline element. A way around this is to change the element from inline to block.

    span {
            width: 150px;
            display: block
            }

Applying display: block will turn the span into a block element, allowing you to change the width. One thing to note however is that block elements will always start on a new line, so you might have to use floats as well.

6.   Centering a fixed width website

To centre your website within the browser, add relative positioning to the outer div, then set the margin to auto.

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

center-content

7.   Image replacement technique

It is always best to use text rather than an image for headings. On the odd occasion when you must have an image it is best to use a background image with hidden text within a div. This is extremely useful for screen readers and SEO as  it is still using regular text, with all the benefits associated with this.

HTML:

<h1><span>Main heading one</span></h1>

CSS:

    h1 {
            background: url(heading-image.gif) no-repeat;
            }

           

    h1 span {
            position:absolute;{
            text-indent: -5000px;{
            }

As you can see we are using regular HTML code for the h1 tag and using CSS to replace the text with an image. Text-indent pushes the text 5000px’s to the left, making it invisible to the user.

Try turning off your CSS style on your website and see how the heading looks.

8.   Min-width

Another bug in IE is that it doesn’t support min-width. Min-width is extremely useful, especially for flexible templates that have a width of 100%, as it tells the browser to stop contracting. For all browsers apart from IE6 all you need ismin-width:Xpx;. e.g.:

    .container {
            min-width:300px;
            }

Getting this to work in IE6 takes some extra effort! To start you will need to create 2 divs, one embedded in the other.

    <div class=”container”>
            <div class=”holder”>Content</div>
            </div>

Then you will need the min-width which goes on the outer div.

    .container {
            min-width:300px;
            } 

Now this is where the IE hack comes into play. You will need to include the following code.

    * html .container {
            border-right: 300px solid #FFF;
            }

    * html .holder {
            display: inline-block;
            position: relative;
            margin-right: -300px;
            } 

As the browser window is resized the outer div width reduces to suit until it shrinks to the border width, at which point it will not shrink any further. The holder div follows suit and also stops shrinking. The outer div border width becomes the minimum width of the inner div.

Source

9.   Hide horizontal scrolling

To remove the horizontal scrolling bar, insert overflow-x: hidden; into the body

    body {
            overflow-x: hidden;
            }

This is useful if you decide to have an image or flash file which has a larger width than the browser.

CSS Hacks 和 问题解决

Author: Chris Thomas ;Translater: Robin

目录

  • 介绍
  • 针对浏览器的选择器
  • 让IE6支持PNG透明
  • 移除超链接的虚线
  • 给行内元素定义宽度
  • 让固定宽度的页面居中
  • 图片替换技术
  • 最小宽度
  • 隐藏水平滚动条

 

一. 介绍

这篇文章包括了8个非常有用的解决办法, 在进行css设计遇到问题时你就会用到它们.

二. 针对浏览器的选择器

这些选择器在你需要针对某款浏览器进行css设计时将非常有用.

IE6及其更低版本

* html {}

IE7及其更低版本

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

仅针对IE7

*:first-child+html {}

IE7和当代浏览器

html>body{}

仅当代浏览器(IE7不适用)

html>/**/body{}

Opera9及其更低版本

html:first-child {}

Safari

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

要使用这些选择器,请将它们放在样式之前. 例如:

#content-box {
width: 300px;
height: 150px;
}
* html
#content-box {
width: 250px;
} /* overrides the above style and changes the width to 250px in IE 6 and below */

三. 让IE6支持PNG透明

一个IE6的Bug引起了大麻烦, 他不支持透明的PNG图片.

你需要使用一个css滤镜

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

四. 移除超链接的虚线(仅对FF有效)

FireFox下,当你点击一个超链接时会在外围出现一个虚线轮廓. 这很容易解决, 只需要在标签样式中加入 outline:none .

a{
outline: none;
}

五. 给行内元素定义宽度

如果你给一个行内元素定义宽度,那么它只是在IE6下有效. 所有的HTML元素要么是行内元素要么就好是块元素. 行内元素包括: <span>, <a>, <strong> 和 <em>. 块元素包括<div>, <p>, <h1>, <form>和<li> . 你不能定义行内元素的宽度, 为了解决这个问题你可以将行内元素转变为块元素.

span { width: 150px; display: block }

六. 让固定宽度的页面居中

为了让页面在浏览器居中显示, 需要相对定位外层div, 然后把margin设置为auto.

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

七. 图片替换技术

用文字总比用图片做标题好一些. 文字对屏幕阅读机和SEO都是非常友好的.

HTML:

<h1><span>Main heading one</span></h1>

CSS:

h1 { background: url(heading-image.gif) no-repeat; }
h1 span {
position:absolute;
text-indent: -5000px;
}
你可以看到我们对标题使用了标准的<h1>作为标签并且用css来将文本替换为图片. text-indent属性将文字推到了浏览器左边5000px处, 这样对于浏览者来说就看不见了.

关掉css,然后看看头部会是什么样子的.

八. 最小宽度

IE6另外一个bug就是它不支持 min-width 属性. min-width又是相当有用的, 特别是对于弹性模板来说, 它们有一个100%的宽度,min-width 可以告诉浏览器何时就不要再压缩宽度了.

除IE6以外所有的浏览器你只需要一个 min-width: Xpx; 例如:

.container {
min-width:300px;
}

为了让他在IE6下工作, 我们需要一些额外的工作. 开始的时候我们需要创建两个div, 一个包含另一个:

<div class="container">
<div class="holder">Content</div>
</div>

然后你需要定义外层div的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;
}

As the browser window is resized the outer div width reduces to suit until it shrinks to the border width, at which point it will not shrink any further. The holder div follows suit and also stops shrinking. The outer div border width becomes the minimum width of the inner div.

九. 隐藏水平滚动条

为了避免出现水平滚动条, 在body里加入 overflow-x:hidden .

body { overflow-x: hidden; }

当你决定使用一个比浏览器窗口大的图片或者flash时, 这个技巧将非常有用.


欢迎访问译言网。在这里,您可以。。。

阅读
发现
翻译