使用技巧会让人变的越来越懒,没错,我就是想让你变懒。下面是我收集的CSS高级技巧,希望你懒出境界。

1. 黑白图像

这段代码会让你的彩色照片显示为黑白照片,是不是很酷?

 img.desaturate {
filter: grayscale(%);
-webkit-filter: grayscale(%);
-moz-filter: grayscale(%);
-ms-filter: grayscale(%);
-o-filter: grayscale(%);
}

2. 使用:not()在菜单上应用/取消应用边框

先给每一个菜单项添加边框

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

……然后再除去最后一个元素……

 // remove border /

 .nav li:last-child {
border-right: none;
}

……可以直接使用 :not() 伪类来应用元素:

 .nav li:not(:last-child) {
border-right: 1px solid #;
}

这样代码就干净,易读,易于理解了。

当然,如果你的新元素有兄弟元素的话,也可以使用通用的兄弟选择符(~):

 ..nav li:first-child ~ li {

   border-left: 1px solid #;
}

3. 页面顶部阴影

下面这个简单的 CSS3 代码片段可以给网页加上漂亮的顶部阴影效果:

 body:before {
content: "";
position: fixed;
top: -10px;
left: ;
width: %;
height: 10px; -webkit-box-shadow: 0px 0px 10px rgba(,,,.);
-moz-box-shadow: 0px 0px 10px rgba(,,,.);
box-shadow: 0px 0px 10px rgba(,,,.); z-index: ;
}

4. 给 body 添加行高

你不需要分别添加 line-height 到每个p,h标记等。只要添加到 body 即可:

 body {
line-height: ;
}

这样文本元素就可以很容易地从 body 继承。

5. 所有一切都垂直居中

要将所有元素垂直居中,太简单了:

 html, body {
height: %;
margin: ;
} body {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-flex;
display: flex;
}

看,是不是很简单。

注意:在IE11中要小心flexbox。

6. 逗号分隔的列表

让HTML列表项看上去像一个真正的,用逗号分隔的列表:

 ul > li:not(:last-child)::after {
content: ",";
}

对最后一个列表项使用 :not() 伪类。

7. 使用负的 nth-child 选择项目

在CSS中使用负的 nth-child 选择项目1到项目n。

 li {
display: none;
} /* select items 1 through 3 and display them */
li:nth-child(-n+) {
display: block;
}

8. 对图标使用 SVG

我们没有理由不对图标使用SVG:

 .logo {
background: url("logo.svg");
}

SVG对所有的分辨率类型都具有良好的扩展性,并支持所有浏览器都回归到IE9。这样可以避开.png、.jpg或.gif文件了。

9. 优化显示文本

有时,字体并不能在所有设备上都达到最佳的显示,所以可以让设备浏览器来帮助你:

html {
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
}

注:请负责任地使用 optimizeLegibility。此外,IE /Edge没有 text-rendering 支持。

10. 对纯 CSS 滑块使用 max-height

使用 max-height 和溢出隐藏来实现只有CSS的滑块:

 .slider ul {
max-height: ;
overlow: hidden;
} .slider:hover ul {
max-height: 1000px;
transition: .3s ease;
}

11. 继承 box-sizing

让 box-sizing 继承 html:

 html {
box-sizing: border-box;
} *, *:before, *:after {
box-sizing: inherit;
}

这样在插件或杠杆其他行为的其他组件中就能更容易地改变 box-sizing 了。

12. 表格单元格等宽

表格工作起来很麻烦,所以务必尽量使用 table-layout: fixed 来保持单元格的等宽:

 .calendar {
table-layout: fixed;
}

13. 用 Flexbox 摆脱外边距的各种 hack

当需要用到列分隔符时,通过flexbox的 space-between 属性,你就可以摆脱nth-,first-,和 last-child 的hack了:

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

现在,列表分隔符就会在均匀间隔的位置出现。

14. 使用属性选择器用于空链接

当a元素没有文本值,但 href 属性有链接的时候显示链接:

 a[href^="http"]:empty::before {
content: attr(href);
}

相当方便。

15. 检测鼠标双击

HTML:

 <div class="test3">
<span><input type="text" value=" " readonly="true" />
<a href="http://renpingjun.com">Double click me</a></span>
</div>

CSS:

 .test3 span {
position: relative;
}
.test3 span a {
position: relative;
z-index: ;
}
.test3 span a:hover, .test3 span a:active {
z-index: ;
}
.test3 span input {
background: transparent;
border: ;
cursor: pointer;
position: absolute;
top: -1px;
left: ;
width: %; /* Hacky */
height: %; /* Hacky */
z-index: ;
}
.test3 span input:focus {
background: transparent;
border: ;
z-index: ;
}

16. CSS 写出三角形

 /* create an arrow that points up */
div.arrow-up {
width:0px;
height:0px;
border-left:5px solid transparent; /* left arrow slant */
border-right:5px solid transparent; /* right arrow slant */
border-bottom:5px solid #2f2f2f; /* bottom, add background color here */
font-size:0px;
line-height:0px;
} /* create an arrow that points down */
div.arrow-down {
width:0px;
height:0px;
border-left:5px solid transparent;
border-right:5px solid transparent;
border-top:5px solid #2f2f2f;
font-size:0px;
line-height:0px;
} /* create an arrow that points left */
div.arrow-left {
width:0px;
height:0px;
border-bottom:5px solid transparent; /* left arrow slant */
border-top:5px solid transparent; /* right arrow slant */
border-right:5px solid #2f2f2f; /* bottom, add background color here */
font-size:0px;
line-height:0px;
} /* create an arrow that points right */
div.arrow-right {
width:0px;
height:0px;
border-bottom:5px solid transparent; /* left arrow slant */
border-top:5px solid transparent; /* right arrow slant */
border-left:5px solid #2f2f2f; /* bottom, add background color here */
font-size:0px;
line-height:0px;
}

17. CSS3 calc() 的使用

calc() 用法类似于函数,能够给元素设置动态的值:

 /* basic calc */
.simpleBlock {
width: calc(% - 100px);
} /* calc in calc */
.complexBlock {
width: calc(% - % / );
padding: 5px calc(% - 2px);
margin-left: calc(% + 10px);
}

18. 文本渐变

文本渐变效果很流行,使用 CSS3 能够很简单就实现:

 h2[data-text] {
position: relative;
}
h2[data-text]::after {
content: attr(data-text);
z-index: ;
color: #e3e3e3;
position: absolute;
top: ;
left: ;
-webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(,,,)), color-stop(%, rgba(,,,)), to(rgba(,,,)));}

办公资源网址导航 https://www.wode007.com

19. 禁用鼠标事件

CSS3 新增的 pointer-events 让你能够禁用元素的鼠标事件,例如,一个连接如果设置了下面的样式就无法点击了。

 .disabled { pointer-events: none; }

20. 模糊文本

简单但很漂亮的文本模糊效果,简单又好看!

 .blur {
color: transparent;
text-shadow: 5px rgba(,,,0.5);
}

20 个 CSS高级样式技巧汇总的更多相关文章

  1. 20个 CSS 快速提升技巧

    作者:web秀 http://www.javanx.cn/20190321/css-skill/ 本文涵盖了20个css技巧,可以解决许多工作中常见的问题. 1.使用CSS重置(reset) css重 ...

  2. CSS 高级布局技巧

    随着 IE8 逐渐退出舞台,很多高级的 CSS 特性都已被浏览器原生支持,再不学下就要过时了. 用 :empty 区分空元素 兼容性:不支持 IE8 /*假如我们有以上列表:*/ <div cl ...

  3. CSS 技巧汇总

    CSS 选择符优先级 !important 声明>内联样式(style)>id 选择符(#id)>类选择符(.class)=伪类选择符(:hover )=属性选择符([attr] ) ...

  4. 移动平台3G手机网站前端开发布局技巧汇总

    移动平台3G手机网站前端开发布局技巧汇总 作者:前端开发-武方博   发布:2011-05-10 09:11   分类:移动开发   阅读:120,618 views   7条评论     您或许正在 ...

  5. 移动平台WEB前端开发技巧汇总(转)

    最近我很关注移动前端的知识,但做为一个UI设计师和web前端工作人员没有这个工作环境接触,做为门外汉,网上系统的知识也了了,一直有种雾里看花的感觉,见到本文,我自己是奉为经典.所以我分享之后又专门打笔 ...

  6. 总结与学习DIV+CSS网页布局技巧

    以前用表格布局时设置网页居中非常方便,把表格对齐方式设置为居中就行了,就这么简单,现在呢,用DIV+CSS样式表控制,好像不是那么容易了,其实也很简单,只不过方式不同而已. <style> ...

  7. SQL高级查询技巧

    SQL高级查询技巧   1.UNION,EXCEPT,INTERSECT运算符 A,UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重 ...

  8. CSS 黑魔法小技巧,让你少写不必要的JS,代码更优雅

    首页   登录注册         CSS 黑魔法小技巧,让你少写不必要的JS,代码更优雅 阅读 8113 收藏 927 2017-09-26 原文链接:github.com 腾讯云容器服务CSS,立 ...

  9. DIV+CSS一些小小的技巧

    DIV+CSS网页布局技巧实例1:设置网页整体居中的代码 以前用表格布局时设置网页居中非常方便,把表格对齐方式设置为居中就行了,就这么简单,现在呢,用DIV+CSS样式表控制,好像不是那么容易了,其实 ...

随机推荐

  1. Java实现 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…

    import java.util.Scanner; public class Main{ private static Scanner cin; public static void main(Str ...

  2. java实现最大连续和问题

    /* 10 5 -3 12 -31 15 22 -7 6 -8 -9 10 .... 暴力:O(n^3) 分治:[ mid ) 三种情况求最大 基线法: O(n) 2个数组: 从左到本位:出现的最大累 ...

  3. Java实现 蓝桥杯 历届试题 横向打印二叉树

    问题描述 二叉树可以用于排序.其原理很简单:对于一个排序二叉树添加新节点时,先与根节点比较,若小则交给左子树继续处理,否则交给右子树. 当遇到空子树时,则把该节点放入那个位置. 比如,10 8 5 7 ...

  4. java实现第八届蓝桥杯生命游戏

    生命游戏 题目描述 康威生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 这个游戏在一个无限大的2D网格上进行. 初始时,每个小方格中居住着一个活着或死了的细胞. 下一时刻每个细胞的 ...

  5. 【CSS】滚动条样式

    /*定义滚动条宽高及背景,宽高分别对应横竖滚动条的尺寸*/ .scrollbar::-webkit-scrollbar{ width: 16px; height: 16px; background-c ...

  6. R调用python模块

    明明已经安装了sctransfer,但仍然显示没有该模块 Error in py_module_import(module, convert = convert) : ModuleNotFoundEr ...

  7. python中的类型

    python中的类型分为四种 1.整形 2.浮点型 3.字符串 4.对象(除了前三种,其他的都是对象) 比如函数也是对象 def fun(): print(123) type(fun) // < ...

  8. 一个简单的CSS登录页

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 使用 LIKE 的模糊查询

    字符串匹配的语法格式如下: <表达式1> [NOT] LIKE <表达式2> 字符串匹配是一种模式匹配,使用运算符 LIKE 设置过滤条件,过滤条件使用通配符进行匹配运算,而不 ...

  10. Linux系统使用Nmon监控及分析系统性能

    一.下载nmon(1)查看sever的内核版本:     命令:lsb_release -a执行结果:LSB Version:    :base-4.0-amd64:base-4.0-noarch:c ...