css技巧(长期更新)

css技巧(长期更新)

总结一下css 的一些技巧。


  1. 文本水平居中
1
2
3
p {
text-align: center;
}
  1. 容器水平居中

先给容器定宽,再margin: 0 auto

1
2
3
4
div {
width: 100px;
margin: 0 auto;
}
  1. 文本垂直居中

直接将line-height设置为容器高度即可。

1
2
3
4
5
6
7
<div>文本</div>
<style>
div {
height: 18px;
line-height: 18px;
}
</style>

如果容器内有n行文本,则设置line-heightheight/n即可。

  1. 容器垂直居中

一个大容器包含一个小容器,让小容器垂直居中。

首先设置大容器高度以及将大容器position: relative,然后小容器定高度以及position: absolutetop设置为50%,margin-top设置为负值的小容器高度的一半。

下面使用calc()简化代码。

calc()函数写法如下,符号两边各空一个空格。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<div class="big">
<div class="small">
</div>
</div>

<style>
.big {
height: 100px;
position: relative;
}
.small {
height: 20px;
position: absolute;
top: calc(50% - 10px);
}
/*或者*/
.small {
height: 20px;
position: absolute;
top: 50%;
margin-top: -10px;
}
</style>
  1. 图片自适应容器
1
2
3
4
5
6
7
8
img {
max-width: 100%;
}

/*ie-6写法*/
img {
width: 100%;
}
  1. 3D按钮

设置左上为浅色,右下为深色。

1
2
3
4
button {
border: 1px solid;
border-color: #999 #777 #777 #999;
}
  1. font简写,注意font-family属性值引号

分开写:

1
2
3
4
5
6
7
8
p {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
font-variant: small-caps;
font-style: italic;
line-height: 150%;
}

写一起:

1
2
3
p {
font: 12px/150% bold small-caps italic Arial, Helvetica, sans-serif;
}
  1. a标签的伪类
1
2
3
4
a:link {}
a:visited {}
a:hover {}
a:active {}
  1. IE条件注释

使用条件注释,引入只对IE生效的语句,比如这里引入只对IE生效的ie.css样式

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

区分IE版本:

1
2
3
4
5
  <!--[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 -->
  1. IE6专用语法

ie6视html为根元素,故而以下代码仅对ie6生效。

1
2
3
* html {}
* html body {}
* html .class{}
  1. IE专用语法

只有ie6可以识别属性前面的下划线

只有ie7可以识别属性前面的通配符

1
2
3
4
5
.class {
background: red;
*background: green;
_background: blue;
}

然而并没有什么卵用。

  1. 选择器优先级
1
行内样式 > ID > class > 标签
  1. IE6的min-height

IE6不支持min-height

解决方法一:

顺序不能乱!

1
2
3
4
5
.class {
min-height: 100px;
height: auto !important;
height: 100px;
}

解决方法二:

使用了IE6才能识别的_height属性

1
2
3
4
.class {
min-height: 100px;
_height: 100px;
}
  1. font-size统一

浏览器默认16px。

可以先设置基准文字大小为10px:

1
2
3
4
body {
font-size: 62.5%;
/*基准字体大小设置为了10px*/
}

后面一律使用em来设置字体:

2.4em就是24px

1
2
3
h1 {
font-size: 2.4em;
}
  1. 改变字母大小写
1
2
3
4
5
6
7
8
9
10
11
12
/*字母设置为大写*/
p {
text-transform: uppercase;
}
/*字母设置为小写*/
p {
text-transform: lowercase;
}
/*字母设置为首字母大写*/
p {
text-transform: capitalize;
}
1
2
3
4
/*字母设置与小写字母等高的大写形式*/
p {
font-variant: small-caps;
}
  1. reset.css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* http://meyerweb.com/eric/tools/css/reset/ 
v2.0 | 20110126
License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
  1. 修改list图标样式

默认:

1
2
3
ul {
list-style-type: disc;
}

自定义图标:

1
2
3
ul {
list-style: url('triangle.png');
}

或者:

1
2
3
4
5
6
ul {
list-style: none;
}
ul li {
background: url('pic.png') no-repeat 0 0.5em;
}
  1. css中的透明
1
2
3
div {
opacity: 0.5;
}
  1. css三角形

原理:容器长宽设为0,设置border-width后,对四个border分别着色,可以看到四个不同颜色的等腰直角三角形组成了一个正方形(由border构成)。

容器长宽设置为0,设置border-width border-style: solid,将需要的三角形着色,其他三角形设为透明border-color: transparent

1
2
3
4
5
6
7
8
9
10
11
<div class="triangle"></div>

<style>
.triangle {
width: 0;
height: 0;
border-style: solid;
border-width: 4px;
border-color: transparent transparent red transparent;
}
</style>

以上就得到了一个底边长8px红色的相左的等腰直角三角形。

详细css三角形见博客单独文章。

  1. 禁止文本自动换行

让文本在一行显示

1
2
3
p {
white-space: nowrap;
}
  1. 给标题设置图标

不想使用文本,想使用图标替换h1达到更好的视觉效果。但是又不能丧失SEO。

方法:

  1. 为容器设置宽高。
  2. 设置一个负的大的文本缩进将h1移出视野。
  3. background设置替换的图片。
1
2
3
4
5
6
h1 {
width: 200px;
height: 50px;
text-indent: -9999px;
background: url('icon.jpg') no-repeat;
}
  1. :focus

为可以获取焦点的元素设置获取焦点后的样式。

1
2
3
input:focus {
border: 1px yellow solid;
}
  1. !important
1
2
3
4
p {
color: red !important;
color: green;
}

规则color: red胜出。

值得一提的是IE不支持!important规则,故而可以用来区分IE和非IE浏览器。

  1. 提示框
1
<a class="tips" href="#">https://google.com/<span>提示文本</span></a>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a.tips {
position: relative;
}
a.tips span {
display: none;
padding: 5px;
width: 200px;
}
a:hover {
background: #fff;
}
a.tips:hover span {
display: inline;
position: absolute;
}
  1. 固定header位置

header标签固定在页面顶部

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;}
  1. 浏览器兼容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* 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 */
  1. 容器水平和垂直居中
1
2
3
4
<div>
<span></span>
<img>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
div {
width: 100px;
height: 200px;
display: block;
text-align: center;
vertical-align: middle;
}
div * {
display: inline-block;
height: 100%;
vertical-align: middle;
}
div img {
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
}
  1. 阴影

text-shadow

box-shadow

外阴影:

1
2
3
4
5
6
div {
-moz-box-shadow: 5px 5px 5px #ccc;
-webkit-box-shadow: 5px 5px 5px #ccc;
box-shadow: 5px 5px 5px #ccc;
/*不带厂商前缀的放在最后*/
}

内阴影:

1
2
3
4
5
div {
-moz-box-shadow: inset 0 0 10px #000;
-webkit-box-shadow: inset 0 0 10px #000;
box-shadow: inset 0 0 10px #000;
}
  1. 取消IE文本框滚动条
1
textarea { overflow: auto; }
  1. 图片预加载

纯css预加载:

1
2
3
4
5
6
7
8
body:after {
content: "";
display: block;
position: absolute;
background: url("../image/manage/help/help_item2_01.png?v=201707241359") no-repeat -10000px -1000px,url("../image/manage/help/help_item2_02.png?v=201707241359") no-repeat -10000px -1000px,url("../image/manage/help/help_item2_03.png?v=201707241359") no-repeat -10000px -1000px,url("../image/manage/help/help_item2_04.png?v=201707241359") no-repeat -10000px -1000px,url("../image/manage/help/help_item2_05.png?v=201707241359") no-repeat -10000px -1000px,url("../image/manage/help/help_item2_06.png?v=201707241359") no-repeat -10000px -1000px,url("../image/manage/help/help_item2_07.png?v=201707241359") no-repeat -10000px -1000px,url("../image/manage/help/help_item2_01.png?v=201707241359") no-repeat -10000px -1000px;
width: 0;
height: 0
}

评论