天天看点

CSS使用技巧

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

未来,本文将持续更新。

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>

首先,将大容器的定位为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%}
  img {width: 100%}

6. 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;

所以,

font-family: arial, helvetica, sans-serif; font-size: 13px; font-weight: normal; font-variant: small-caps; font-style: italic; line-height: 150%;

可以被写成:

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的优先性

基本规则是:

  行内样式 > 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,有两种方法可以解决这个问题:

方法一:

min-height: 500px; height: auto !important; height: 500px;

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

方法二:

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重置

17. 用图片充当列表标志

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

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

18. 透明

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

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,所以也可以利用它区分不同的浏览器。

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. 固定位置的页首

  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 { 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 } 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 } *+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; 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;

内阴影:

-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. 图片预加载

32. css重置

(完)

继续阅读