20 个 CSS高级样式技巧汇总
使用技巧会让人变的越来越懒,没错,我就是想让你变懒。下面是我收集的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高级样式技巧汇总的更多相关文章
- 20个 CSS 快速提升技巧
作者:web秀 http://www.javanx.cn/20190321/css-skill/ 本文涵盖了20个css技巧,可以解决许多工作中常见的问题. 1.使用CSS重置(reset) css重 ...
- CSS 高级布局技巧
随着 IE8 逐渐退出舞台,很多高级的 CSS 特性都已被浏览器原生支持,再不学下就要过时了. 用 :empty 区分空元素 兼容性:不支持 IE8 /*假如我们有以上列表:*/ <div cl ...
- CSS 技巧汇总
CSS 选择符优先级 !important 声明>内联样式(style)>id 选择符(#id)>类选择符(.class)=伪类选择符(:hover )=属性选择符([attr] ) ...
- 移动平台3G手机网站前端开发布局技巧汇总
移动平台3G手机网站前端开发布局技巧汇总 作者:前端开发-武方博 发布:2011-05-10 09:11 分类:移动开发 阅读:120,618 views 7条评论 您或许正在 ...
- 移动平台WEB前端开发技巧汇总(转)
最近我很关注移动前端的知识,但做为一个UI设计师和web前端工作人员没有这个工作环境接触,做为门外汉,网上系统的知识也了了,一直有种雾里看花的感觉,见到本文,我自己是奉为经典.所以我分享之后又专门打笔 ...
- 总结与学习DIV+CSS网页布局技巧
以前用表格布局时设置网页居中非常方便,把表格对齐方式设置为居中就行了,就这么简单,现在呢,用DIV+CSS样式表控制,好像不是那么容易了,其实也很简单,只不过方式不同而已. <style> ...
- SQL高级查询技巧
SQL高级查询技巧 1.UNION,EXCEPT,INTERSECT运算符 A,UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重 ...
- CSS 黑魔法小技巧,让你少写不必要的JS,代码更优雅
首页 登录注册 CSS 黑魔法小技巧,让你少写不必要的JS,代码更优雅 阅读 8113 收藏 927 2017-09-26 原文链接:github.com 腾讯云容器服务CSS,立 ...
- DIV+CSS一些小小的技巧
DIV+CSS网页布局技巧实例1:设置网页整体居中的代码 以前用表格布局时设置网页居中非常方便,把表格对齐方式设置为居中就行了,就这么简单,现在呢,用DIV+CSS样式表控制,好像不是那么容易了,其实 ...
随机推荐
- js动态绑定class(当前父级div下的子元素有没有这个class,有的话移除,没有的话添加)
<div class="layui-inline" id=‘’ onclick="changeType(id)"> ...
- webpack从什么都不懂到入门
前言 这篇文章是自己在整理webpack相关的东西时候突发奇想,想总结自己所学知识,也希望能够帮助想学习webpack的同学们,都是入门级别的,大佬请出门右转. 本文的webpack基于webpack ...
- 实验三 Linux系统用户管理及VIM配置
项目 内容 这个作业属于哪个课程 班级课程的主页链接 这个作业的要求在哪里 作业要求链接接地址 学号-姓名 17041428-朱槐健 作业学习目标 1.学习Linux系统用户管理 2.学习vim使用 ...
- 如何在centos7安装dnf软件包
想在自己的笔记本CentOS7上安装dnf玩玩儿,但是根据百度出来的方法没有成功. yum install epel-release -y yum install dnf 现在将解决办法转载过来,如下 ...
- 利用Jackson将数据转换为Json
1.确保相关依赖导入 2.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app ...
- mtail 调试
mtail 调式 mtail 不会采集当前accesslog 内容以前的内容,只有当你启动mtail后,去访问你的监控tomcat,有新的access 日志刷入localhost_access_log ...
- Mini Linux的制作过程
- 最后一面挂在volatile关键字上,面试官:重新学学Java吧!
最后一面挂在volatile关键字上,面试官:重新学学Java吧! 为什么会有volatile关键字? volatile: 易变的; 无定性的; 无常性的; 可能急剧波动的; 不稳定的; 易恶化的; ...
- Flume-1.4.0和Hbase-0.96.0整合
在使用Flume的时候,请确保你电脑里面已经搭建好Hadoop.Hbase.Zookeeper以及Flume.本文将以最新版的Hadoop-2.2.0.Hbase-0.96.0.Zookeeper-3 ...
- rust 函数-生命周期
记录一下自己理解的生命周期. 每个变量都有自己的生命周期. 在c++里生命周期好比作用域, 小的作用域的可以使用大作用域的变量. 如果把这里的每个作用域取个名,那么就相当于rust里的生命周期注解. ...