使用技巧会让人变的越来越懒,没错,我就是想让你变懒。下面是我收集的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实现输入信用卡号码

    /* 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心, 因为并不是一个随便的信用卡号码都是合法的,它必须通过 Luhn 算法来验证通过. 该校验的过程: 1.从卡号最后一 ...

  2. java实现第三届蓝桥杯数量周期

    数量周期 [结果填空](满分9分) 复杂现象背后的推动力,可能是极其简单的原理.科学的目标之一就是发现纷繁复杂的自然现象背后的简单法则.爱因斯坦的相对论是这方面的典范例证. 很早的时候,生物学家观察某 ...

  3. IDEA连接远程服务器Docker部署Spring Boot项目

    开始前的准备工作 拥有一台云服务器,我的是腾讯云服务器(CentOS7) 腾讯云服务器安装Docker,我的版本是Docker 19.03.9,关于安装和配置镜像加速器可以查阅我的另一篇博文:http ...

  4. iOS-pthread && NSThread && iOS9网络适配

    几个概念: 进程:"正在运行"应用程序(app)就是一个进程,它至少包含一个线程:            进程的作用:为应用程序开辟内存空间: 线程:CPU调度的最小单元:     ...

  5. Azure AD(四)知识补充-服务主体

    一,引言 又到了新的一周了,也到了我新的分享的时间了,还记得上一周立得Flag,其中 “保证每周输出一篇文章” ,让我特别“在意”(这里用词不太恰当).主要是我的一个大学舍友,他突然问了我一个关于写博 ...

  6. PE文件介绍 (1)

    PE文件介绍 PE文件主要是windows操作系统下使用的可执行文件格式,PE文件是指32位的可执行文件也叫做PE32,64位可执行文件叫做PE+或者PE32+ PE文件格式 种类 主扩展名 可执行类 ...

  7. isinstance用法

    ''' 作用:来判断一个对象是否是一个已知的类型. 其第一个参数(object)为对象,第二个参数(type)为类型名(int...)或类型名的一个列表((int,list,float)是一个列表). ...

  8. 装cnpm

    npm install -g cnpm --registry=https://registry.npm.taobao.org 然后配置环境变量

  9. Android学习笔记显示和隐藏ActionBar

    要在应用中使用ActionBar 需要android:minSdkVersion:11以上,现在基本都可以用了 创建Android项目如果不做特殊设置默认都带有ActionBar 如果不想要Actio ...

  10. Android开发学习笔记DDMS的使用

    打开DDMS DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务. DDMS里面包含了:Device(设备) F ...