CSS使用技巧

作者: 阮一峰

日期: 2010年3月31日

最近,我开始升级网志了。

在修改模板的过程中,需要重写CSS样式表。正好看到instantshift.com有一篇CSS常用技巧的总结文章,我就把它整理出来,供自己参考,也希望对大家有用。

未来,本文将持续更新。

1. 文字的水平居中

将一段文字置于容器的水平中点,只要设置text-align属性即可:

  text-align:center;

2. 容器的水平居中

先为该容器设置一个明确宽度,然后将margin的水平值设为auto即可。

  div#container {
    width:760px;
    margin:0 auto;
  }

3. 文字的垂直居中

单行文字的垂直居中,只要将行高与容器高设为相等即可。

比如,容器中有一行数字。

  <div id="container">1234567890</div>

然后CSS这样写:

  div#container {height: 35px; line-height: 35px;}

如果有n行文字,那么将行高设为容器高度的n分之一即可。

4. 容器的垂直居中

比如,有一大一小两个容器,请问如何将小容器垂直居中

  <div id="big">
    <div id="small">
    </div>
  </div>

首先,将大容器的定位为relative。

  div#big{
    position:relative;
    height:480px;
  }

然后,将小容器定位为absolute,再将它的左上角沿y轴下移50%,最后将它margin-top上移本身高度的50%即可。

  div#small {
    position: absolute;
    top: 50%;
    height: 240px;
    margin-top: -120px;
  }

使用同样的思路,也可以做出水平居中的效果。

5. 图片宽度的自适应

如何使得较大的图片,能够自动适应小容器的宽度?CSS可以这样写:

  img {max-width: 100%}

但是IE6不支持max-width,所以遇到IE6时,使用IE条件注释,将语句改写为:

  img {width: 100%}

6. 3D按钮

要使按钮具有3D效果,只要将它的左上部边框设为浅色,右下部边框设为深色即可。

  div#button {
    background: #888;
    border: 1px solid;
    border-color: #999 #777 #777 #999;
  }

7. font属性的快捷写法

font快捷写法的格式为:

  body {
    font: font-style font-variant font-weight font-size line-height font-family;
  }

所以,

  body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
    font-weight: normal;
    font-variant: small-caps;
    font-style: italic;
    line-height: 150%;
  }

可以被写成:

  body {
    font: italic small-caps normal 13px/150% Arial, Helvetica, sans-serif;
  }

8. link状态的设置顺序

link的四种状态,需要按照下面的前后顺序进行设置:

  a:link
  a:visited
  a:hover
  a:active

9. IE条件注释

你可以利用条件注释,设置只对IE产生作用的语句:

  <!--[if IE]>
    <link rel="stylesheet" type="text/css" href="ie-stylesheet.css" />
  < ![endif]-->

还可以区分各种不同的IE版本:

  <!--[if IE 6]> - targets IE6 only -->
  <!--[if gt IE 6]> - targets IE7 and above -->
  <!--[if lt IE 6]> - targets IE5.5 and below -->
  <!--[if gte IE 6]> - targets IE6 and above -->
  <!--[if lte IE 6]> - targets IE6 and below -->

10. IE6专用语句:方法一

由于IE6不把html视为文档的根元素,所以利用这一点,可以写出只有IE6才能读到的语句:

  /* the following rules apply only to IE6 */

  * html{
  }

  * html body{
  }

  * html .foo{
  }

IE7专用语句则要写成

  /* the following rules apply only to IE7 */

  *+html .foo{
  }

11. IE专用语句:方法二

除了IE6以外,所有浏览器都不能识别属性前的下划线。而除了IE7之外,所有浏览器都不能识别属性前的*号,因此可以写出只有这两个浏览器才能读到的语句:

  .element {
    background: red; /* modern browsers */
    *background: green; /* IE 7 and below */
    _background: blue; /* IE6 exclusively */
  }

12. CSS的优先性

如果同一个容器被多条CSS语句定义,那么哪一个定义优先呢?

基本规则是:

  行内样式 > id样式 > class样式 > 标签名样式

比如,有一个元素:

  <div id="ID" class="CLASS" style="color:black;"></div>

行内样式是最优先的,然后其他设置的优先性,从低到高依次为:

  div < .class < div.class < #id < div#id < #id.class < div#id.class

13. IE6的min-height

IE6不支持min-height,有两种方法可以解决这个问题:

方法一:

  .element {
    min-height: 500px;
    height: auto !important;
    height: 500px;
  }

共有三条CSS语句,第一句是针对其他浏览器设置最小高度,第三句是针对IE设置最小高度,第二句则是让其他浏览器覆盖第三句的设置。

方法二:

  .element {
    min-height: 500px
    _height: 500px
  }

_height只有IE6能读取。

14. font-size基准

浏览器的缺省字体大小是16px,你可以先将基准字体大小设为10px:

  body {font-size:62.5%;}

后面统一采用em作为字体单位,2.4em就表示24px。

  h1 {font-size: 2.4 em}

15. Text-transform和Font Variant

Text-transform用于将所有字母变成小写字母、大写字母或首字母大写:

  p {text-transform: uppercase}
  p {text-transform: lowercase}
  p {text-transform: capitalize}

Font Variant用于将字体变成小型的大写字母(即与小写字母等高的大写字母)。

  p {font-variant: small-caps}

16. CSS重置

CSS重置用于取消浏览器的内置样式,请参考YUIEric Meyer的样式表。

17. 用图片充当列表标志

默认情况下,浏览器使用一个黑圆圈作为列表标志,可以用图片取代它:

  ul {list-style: none}

  ul li {
    background-image: url("path-to-your-image");
    background-repeat: none;
    background-position: 0 0.5em;
  }

18. 透明

将一个容器设为透明,可以使用下面的代码:

  .element {
    filter:alpha(opacity=50);
    -moz-opacity:0.5;
    -khtml-opacity: 0.5;
    opacity: 0.5;
  }

在这四行CSS语句中,第一行是IE专用的,第二行用于Firefox,第三行用于webkit核心的浏览器,第四行用于Opera。

19. CSS三角形

如何使用CSS生成一个三角形?

先编写一个空元素

  <div class="triangle"></div>

然后,将它四个边框中的三个边框设为透明,剩下一个设为可见,就可以生成三角形效果:

  .triangle {
    border-color: transparent transparent green transparent;
    border-style: solid;
    border-width: 0px 300px 300px 300px;
    height: 0px;
    width: 0px;
  }

20. 禁止自动换行

如果你希望文字在一行中显示完成,不要自动换行,CSS命令如下:

  h1 { white-space:nowrap; }

21. 用图片替换文字

有时我们需要在标题栏中使用图片,但是又必须保证搜索引擎能够读到标题,CSS语句可以这样写:

  h1 {
    text-indent:-9999px;
    background:url("h1-image.jpg") no-repeat;
    width:200px;
    height:50px;
  }

22. 获得焦点的表单元素

当一个表单元素获得焦点时,可以将其突出显示:

  input:focus { border: 2px solid green; }

23. !important规则

多条CSS语句互相冲突时,具有!important的语句将覆盖其他语句。由于IE不支持!important,所以也可以利用它区分不同的浏览器。

  h1 {
    color: red !important;
    color: blue;
  }

上面这段语句的结果是,其他浏览器都显示红色标题,只有IE显示蓝色标题。

24. CSS提示框

当鼠标移动到链接上方,会自动出现一个提示框。

  <a class="tooltip" href="#">链接文字 <span>提示文字</span></a>

CSS这样写:

  a.tooltip {position: relative}
  a.tooltip span {display:none; padding:5px; width:200px;}
  a:hover {background:#fff;} /*background-color is a must for IE6*/
  a.tooltip:hover span{display:inline; position:absolute;}

25. 固定位置的页首

当页面滚动时,有时需要页首在位置固定不变,CSS语句可以这样写,效果参见http://limpid.nl/lab/css/fixed/header

  body{ margin:0;padding:100px 0 0 0;}

  div#header{
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:<length>;
  }

  @media screen{
    body>div#header{position: fixed;}
  }

  * html body{overflow:hidden;}

  * html div#content{height:100%;overflow:auto;}

IE6的另一种写法(用于固定位置的页脚):

  * html #footer {
    position:absolute;
    top:expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+'px');
}

26. 在IE6中设置PNG图片的透明效果

  .classname {

    background: url(image.png);

    _background: none;

    _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader
        (src='image.png', sizingMethod='crop');

  }

27. 各类浏览器的专用语句

  /* IE6 and below */
  * html #uno { color: red }

  /* IE7 */
  *:first-child+html #dos { color: red }

  /* IE7, FF, Saf, Opera */
  html>body #tres { color: red }

  /* IE8, FF, Saf, Opera (Everything but IE 6,7) */
  html>/**/body #cuatro { color: red }

  /* Opera 9.27 and below, safari 2 */
  html:first-child #cinco { color: red }

  /* Safari 2-3 */
  html[xmlns*=""] body:last-child #seis { color: red }

  /* safari 3+, chrome 1+, opera9+, ff 3.5+ */
  body:nth-of-type(1) #siete { color: red }

  /* safari 3+, chrome 1+, opera9+, ff 3.5+ */
  body:first-of-type #ocho { color: red }

  /* saf3+, chrome1+ */
  @media screen and (-webkit-min-device-pixel-ratio:0) {
    #diez { color: red }
  }

  /* iPhone / mobile webkit */
  @media screen and (max-device-width: 480px) {
    #veintiseis { color: red }
  }

  /* Safari 2 - 3.1 */
  html[xmlns*=""]:root #trece { color: red }

  /* Safari 2 - 3.1, Opera 9.25 */
  *|html[xmlns*=""] #catorce { color: red }

  /* Everything but IE6-8 */
  :root *> #quince { color: red }

  /* IE7 */
  *+html #dieciocho { color: red }

  /* Firefox only. 1+ */
  #veinticuatro, x:-moz-any-link { color: red }

  /* Firefox 3.0+ */
  #veinticinco, x:-moz-any-link, x:default { color: red }

  /***** Attribute Hacks ******/

  /* IE6 */
  #once { _color: blue }

  /* IE6, IE7 */
  #doce { *color: blue; /* or #color: blue */ }

  /* Everything but IE6 */
  #diecisiete { color/**/: blue }

  /* IE6, IE7, IE8 */
  #diecinueve { color: blue\9; }

  /* IE7, IE8 */
  #veinte { color/*\**/: blue\9; }

  /* IE6, IE7 -- acts as an !important */
  #veintesiete { color: blue !ie; } /* string after ! can be anything */

28. 容器的水平和垂直居中

HTML代码如下:

  <figure class='logo'>

    <span></span>

    <img class='photo'/>

  </figure>

CSS代码如下:

  .logo {
    display: block;
    text-align: center;
    display: block;
    text-align: center;
    vertical-align: middle;
    border: 4px solid #dddddd;
    padding: 4px;
    height: 74px;
    width: 74px; }

  .logo * {
    display: inline-block;
    height: 100%;
    vertical-align: middle; }

  .logo .photo {
    height: auto;
    width: auto;
    max-width: 100%;
    max-height: 100%; }

29. CSS阴影

外阴影:

  .shadow {
    -moz-box-shadow: 5px 5px 5px #ccc;
    -webkit-box-shadow: 5px 5px 5px #ccc;
    box-shadow: 5px 5px 5px #ccc;
  }

内阴影:

  .shadow {
    -moz-box-shadow:inset 0 0 10px #000000;
    -webkit-box-shadow:inset 0 0 10px #000000;
    box-shadow:inset 0 0 10px #000000;
  }

30. 取消IE文本框的滚动条

  textarea { overflow: auto; }

31. 图片预加载

请参考3 Ways to Preload Images with CSS, JavaScript, or Ajax

32. CSS重置

请参考Should You Reset Your CSS?

(完)

留言(36条)

先回后看,既然持续更新,当然要常期关注。

正要学CSS,这些技巧对我很实用。呵

一些技巧要求HTML文档是标准模式才行
也就是在文档头加上DOCTYPE注释

我还是先去学习些css的基础知识吧。。。

“如果有n行文字,那么将行高设为容器高度的n分之一即可。”
这个貌似是不可行的,行高会应用到每一行,而不是整段的文字。

另外opacity的那个,webkit核心的应该是-webkit-
而现在firefox,opera和webkit核心的safari、chrome都可以直接用opacity:50%;不需要加私有属性的标记。

对我有用,谢谢!
很多是以前就会的,今天又复习一遍。呵呵

  拜托,现在可是多事之秋,这个时候升级网志不是给自己添乱吗?有时间的话多写几篇文章,抓一下社会热点,等Google和Godaddy的话题过去再升级网志不行吗?

本人将持续关注

学习中,很有用,css很神奇,持续关注~~~

晕,看样子不管什么都得对IE6重新写一遍特殊对待。IE6,害人不浅啊。
另外问一下博主,如果我想更改一下模板中段间距应该改哪里呢?我Google了一下,没有找到满意的答案

text-align一般只对ie有效的。

IE6真应该从机器上删除了,太久远了。

引用zhiwei的发言:

如果我想更改一下模板中段间距应该改哪里呢?我Google了一下,没有找到满意的答案

如果是一段文本中的行高,可以在p标签上用line-height来设定;
如果是多段文本之间的距离,你可以在p标签上设置margin属性。

关于“容器的水平居中”问题,野草遇到了不少问题,参见:
《如何将一个div区块水平居中?》
http://yeahcao.blog.hexun.com/42891545_d.html

《分享:在和讯博客中利用div将头部广告位居中排版的方法》
http://yeahcao.blog.hexun.com/43417832_d.html

主要问题就是:IE的兼容问题……

!important的描述有错误,ie7.8还是支持的。

其中透明的 CSS大不必使用那么多,只需要filter:alpha(opacity=50);和opacity:0.5;即可;
 .element {
    filter:alpha(opacity=50);
    opacity: 0.5;
  }
我在ie6、7、8和firefox、safari、opera和chrome上测试过的。
:)

您好。在第十条里面提到了:

由于IE6不把html视为文档的根元素,所以利用这一点,可以写出只有IE6才能读到的语句:

  /* the following rules apply only to IE6 */

  * html{
  }

  * html body{
  }

  * html .foo{
  }

但是紧接着在第11条里又提到了:

而除了IE7之外,所有浏览器都不能识别属性前的*号

第10条说专门针对IE6的例子加了*,而第11条里说只有IE7才能识别*,这不是矛盾了吗?

引用Zz的发言:

第10条说专门针对IE6的例子加了*,而第11条里说只有IE7才能识别*,这不是矛盾了吗?

没有矛盾啊。一个是针对元素选择器,另一个是针对属性值。

哈哈,牛叉的技巧

还蛮好的!

第5条,如果是IE7+,firefox, img {max-width: 100%} 可以保证如果图片没有超过容器,可以显示正常图片大小,如果超过容器,以容器为准。
但是IE6中 img {width: 100%} ,不过图片的大小如果,都是以容器为准,都会成100%的。
这个max-width没有在IE6中实现吧?

太感谢了,学到了很多东西!

生成三角形的方法在gg chrome 和Oprea中无效




triangle





.triangle {
    border-color: transparent transparent green transparent;
    border-style: solid;
    border-width: 100px 300px 300px 300px;
    height: 100px;
    width: 100px;
  }



不信大家试试上面的例子!

CSS的优先性 不完全 应该加上伪类。伪类基本最低。css的优先级应该是按照权重来计算的。只能说通常情况下按照楼主的优先级是正确的。

24.方法是不是多余了?直接写个title就会有提示文字啊,而且里面提到的方法在chorme firefox里面的效果是完全不同的,不兼容?

第十九个,生成三角形这个,为甚么测试的时候没有成功?chrome IE firefox都不可以,没有显示任何内容

很好的文章,很好的博客,是我学习的榜样

生成三角形的那个 可以啊 直接复制过来 有缩进问题吧,自己排一下就可以了

Only wanna tell that this is invaluable , Thanks for taking your time to write this.

background-repeat: none; ??

ie不支持!important?

4. 容器的垂直居中
我试验了下,将top设置成25%这样,是不是更加简单有效呢

div {
display: flex;
align-items: center; /*垂直方向*/
justify-content: center; /*水平方向*/
}

这个是我接触flex之后,经常用到的语法,用来居中;
阮老师的课程虽然发布在2010年,但是到现在17年了,这些用法还是很精辟又实用呢

引用惠瑛的发言:

div {
display: flex;
align-items: center; /*垂直方向*/
justify-content: center; /*水平方向*/
}

这个是我接触flex之后,经常用到的语法,用来居中;
阮老师的课程虽然发布在2010年,但是到现在17年了,这些用法还是很精辟又实用呢

flex布局需要选择主轴方向,属性为flex-direction,值为row/column,justify-content不能说指水平方向。

第17条错误,那是设置了列表的背景非列表标志,应用:list-style-image:url("your-image-path");望早入改正,以免向我这样的新手错误学习,还好我看一条就实践一条。

引用Ming的发言:

生成三角形的那个 可以啊 直接复制过来 有缩进问题吧,自己排一下就可以了

弄了半天说怎么没想过,果然缩进问题,好神奇啊

我要发表看法

«-必填

«-必填,不公开

«-我信任你,不会填写广告链接