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样式表控制,好像不是那么容易了,其实 ...
随机推荐
- [C#.NET 拾遗补漏]02:数组的几个小知识
阅读本文大概需要 1.5 分钟. 数组本身相对来说比较简单,能想到的可写的东西不多.但还是有一些知识点值得总结和知晓一 下.有的知识点,知不知道不重要,工作中用的时候搜索一下就可以了,毕竟实现一个功 ...
- 解读三组容易混淆的Dockerfile指令
长话短说,今天分享三组容易混淆的Dockerfile指令, 帮助大家编写更优雅的Dockfile文件.构建更纯净的Docker镜像. COPY vs ADD COPY.ADD主体功能类似:从指定位置拷 ...
- 关于adb的下载和基本使用
我们无论是开发还是测试,对Android SDK一定都不陌生,如果我们要使用adb(Android debug bridge)命令,那么这个就必不可少了. 1.给大家提供一个下载地址:https:// ...
- Flask 的请求与响应
flask的请求与响应 from flask import Flask,request,make_response,render_template,redirect app = Flask(__nam ...
- Mac Book 问题汇集
1.mac wifi 无法连接问题 1. 由于插入的USB 转接头导致,USB转接口带有网线插口,机器默认网页接口接口导致. 解决方案: 拔掉转接口,连上WiFi ,再插入转接口使用 2.可以是路由器 ...
- ucore系统 eclipse-cdt实验环境准备
官网下载eclipse eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz省略安装jdk 等配置解压安装即可导入实验的项目 点击完成即可完成导入 开始配置qemu ...
- Mini2440上的第一个程序——点亮Led
手头的Mini2440搁置了两年半之后,我再次决定拿出它,重新尝试嵌入式Linux的学习. 我使用的是友善之臂的Mini2440开发板.韦东山的<嵌入式Linux应用开发完成手册>及其视频 ...
- RFID-RC522 模块的读写操作【Arduino】
接线 Arduino Uno <-> RFID-RC52210 <-> SDA13 <-> SCK11 <-> MOSI12 <-> MIS ...
- redis性能优化——生产中实际遇到的问题排查总结
背景 redis-K,V数据库,因其高性能的操作性和支持丰富的数据结构,目前大量被用于衔接应用层和关系数据库中间的缓存层.随着使用的场景越来越多,和数据量快速的递增,在生产环境中经常会遇到相关的性能瓶 ...
- JavaSE基础之数组
数组 一.静态初始化 格式一 数据类型[] 变量名 = {元素1,元素2,元素3...}; 格式二 数据类型[] 变量名 = new 数据类型{元素1,元素2,元素3...}; 或者: 数据类型[] ...