提升前端工程师的css3代码编写的专业性

1. 使用 :not() 为导航添加/取消边框

很多人会这样给导航添加边框,然后给最后一个取消掉:

/* add border */
.nav li {
border-right: 1px solid #666;
}

/* remove border */
.nav li:last-child {
border-right: none;
}
其实,用CSS的 :not() 可以简化为下面的代码:

.nav li:not(:last-child) {
border-right: 1px solid #666;
}
当然,你也可以使用 .nav li + li 甚至 .nav li:first-child ~ li,但是使用 :not() 可以使意图更加明确。

2. 给 body 添加 line-height 属性

你不需要为 <p>、<h*> 分别添加 line-height 属性,相反的,只需要添加到 body 上即可:

body {
line-height: 1;
}
这样,文本元素就可以很容易的从 body 继承该属性。

3. 垂直居中

这并不是什么魔法,你可以垂直居中任何元素:

html, body {
height: 100%;
margin: 0;
}

body {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-flex;
display: flex;
}
还需要其他的?水平居中、垂直居中,在任何时间、任何地方?可以看看CSS-Tricks的这篇文章。

注意:flexbox 在 IE11 下存在一些bug。

4. 使用逗号分割列表

使列表看起来像是用逗号分割的:

ul > li:not(:last-child)::after {
content: ",";
}
通过 :not() 伪类去掉最后一个元素后面的逗号。

5. 使用负的 nth-child 选取元素

使用负的 nth-child 在 1 到 n 之间选择元素:

li {
display: none;
}

/* 选择第1到3个元素并显示它们 */
li:nth-child(-n+3) {
display: block;
}
当然,如果你了解 :not() 的话,还可以这么做:

li:not(:nth-child(-n+3)) {
display: none;
}
是不是非常简单?

6. 使用 SVG 作 icon 图标

没什么理由不使用 SVG 作 icon 图标:

.logo {
background: url("logo.svg");
}
SVG 对于任何分辨率的缩放效果都很好,并且支持 IE9+所有浏览器,所以,放弃使用 .png、.jpg、.gif文件吧。

注:以下代码对于使用辅助设备上网的用户可以提升可访问性:

.no-svg .icon-only:after {
content: attr(aria-label);
}
7. 文本展示优化

有时候字体并不是对于所有设备都显示为最佳效果,所以使用浏览器来帮忙吧:

html {
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
}
8. 使用 max-height 实现纯CSS幻灯片

使用 max-height 与超出隐藏实现纯CSS的幻灯片:

.slider ul {
max-height: 0;
overlow: hidden;
}

.slider:hover ul {
max-height: 1000px;
transition: .3s ease; /* animate to max-height */
}
9. 继承 box-sizing

让 box-sizing 继承自 html :

html {
box-sizing: border-box;
}

*, *:before, *:after {
box-sizing: inherit;
}
这使得在插件或者其他组件中修改 box-sizing 属性变得更加容易。

10. 设置表格相同宽度

.calendar {
table-layout: fixed;
}
11. 使用 Flexbox 来避免 Margin Hacks

在做多列布局的时候,可以通过 Flexbox 的 space-between 属性来避免nth-、first-、 last-child 等 hacks:

.list {
display: flex;
justify-content: space-between;
}

.list .person {
flex-basis: 23%;
}
这样,列之间的空白就会被均匀的填满。

13. 对空链接使用属性选择器

当 <a> 中没有文本而 href 不为空的时候,显示其链接:

a[href^="http"]:empty::before {
content: attr(href);
}
浏览器支持

以上技巧支持大部分现代浏览器,如:Chrome、Firefox、Safari、Edge以及IE11。

 

兼容所有浏览器的阴影写法:

.shadow {
width:200px;
height:200px;
background-color:#000;
-moz-box-shadow: 3px 3px 4px #444;
-webkit-box-shadow: 3px 3px 4px #444;
box-shadow: 3px 3px 4px #444;
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#444444')";
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#444444');

  

 css3圆角的制作

各浏览器的兼容写法:

-moz-border-radius: 20px;

-webkit-border-radius: 20px;

-o-border-radius: 20px;

老版本的IE浏览器的兼容写法:

如果你的IE比较老,不支持border-radius,有很多其它的技术可以弥补这个缺陷,我遇到过的最好的一个解决方法是使用一个很小的JavaScript程序包,叫做CurvyCorners。这个CurvyCorners用javaScript动态的生成很多div标记,用这些div标记来绘出圆角效果,甚至支持消除锯齿功能。

CurvyCorners的用法很简单。第一步是在页面中引入CurvyCorners.js脚本:

<script src="curvy.corners.trunk.js"></script>
CurvyCorners会在DOM元素里寻找具有border-radius属性的元素,然后依次给它们制作出圆角效果。不需要辅助图片。你甚至可以设置指定元素的弧度半径:
var settings = {
    tl: { radius: 12 },
    tr: { radius: 12 },
    bl: { radius: 12 },
    br: { radius: 12 },
    antiAlias: true
};
/* moooo */
$$('.round').each(function(rd) {
  curvyCorners(settings,rd);
});

我强烈建议你应该指定需要使用圆角的元素,因为让脚本搜索整个页面来寻找需要圆角处理的元素是一个很耗CPU的过程,而且这个过程是每个页面加载时都会执行。

 

线性渐变色兼容所有浏览器

 

 

#example1	{
	/* 底色 */
	width:200px;
	height:200px;
	background-color: #063053;
	/* chrome 2+, safari 4+; multiple color stops */
	background-image:-webkit-gradient(linear, left bottom, left top, color-stop(0.32, #063053), color-stop(0.66, #395873), color-stop(0.83, #5c7c99));
	/* chrome 10+, safari 5.1+ */
	background-image: -webkit-linear-gradient(#063053, #395873, #5c7c99);
	/* firefox; multiple color stops */
	background-image: -moz-linear-gradient(top,#063053, #395873, #5c7c99);
	/* ie 6+ */
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#063053', endColorstr='#395873');
	/* ie8 + */
	-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#063053', endColorstr='#395873')";
	/* ie10 */
	background-image: -ms-linear-gradient(#063053, #395873, #5c7c99);
	/* opera 11.1 */
	background-image: -o-linear-gradient(#063053, #395873, #5c7c99);
	/* 标准写法 */
	background-image: linear-gradient(#063053, #395873, #5c7c99);
}

 

  

CSS颜色渐变(Gradients)的技巧和提示

CSS颜色渐变(Gradients)技术很有价值,但有时很难实现。有时你已经实现了想要的渐变,而浏览器的支持也会成为一个问题。下面是一些使用CSS颜色渐变(Gradients)的建议:

1、想让你的CSS颜色渐变(Gradients)表现一定的透明度?使用 rgba 颜色。
2、使用背景色垫底,这样防止当浏览器不支持时不注意没有任何颜色。
3、火狐浏览器和谷歌浏览器都支持repeating-linear-gradient 和 repeating-radial-gradient,用法是:

#example7 {
	background-image: -moz-repeating-linear-gradient(top left -45deg, green, red 5px, white 5px, #ccc 10px);
	background-image: -webkit-repeating-linear-gradient(-45deg, green, red 5px, white 5px, #ccc 10px);
}

 

css3属性-webkit-font-smoothing
CSS3里面加入了一个“-webkit-font-smoothing”属性。

这个属性可以使页面上的字体抗锯齿,使用后字体看起来会更清晰舒服。

它有三个属性:

none ------ 对低像素的文本比较好

subpixel-antialiased ------默认值

antialiased ------抗锯齿很好

例子:

body{

-webkit-font-smoothing: antialiased;

}

posted @ 2016-04-11 13:08  我不是工程师  阅读(260)  评论(0编辑  收藏  举报