侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

20个非常实用的CSS技巧

前端  /  管理员 发布于 7年前   182

本文为大家分享了20个非常实用的CSS技巧,供大家参考,具体内容如下

1. 黑白图像

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

CSS Code复制内容到剪贴板
  1. img.desaturate {   
  2.     filter: grayscale(100%);   
  3.     -webkit-filter: grayscale(100%);   
  4.     -moz-filter: grayscale(100%);   
  5.     -ms-filter: grayscale(100%);   
  6.     -o-filter: grayscale(100%);   
  7. }   
  8.   

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

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

CSS Code复制内容到剪贴板
  1. /* add border */  
  2. .nav li {   
  3.   border-right: 1px solid #666;   
  4. }   
  5. ……然后再除去最后一个元素……   
  6.   
  7. // remove border /   
  8.   
  9. .nav li:last-child {   
  10.   border-right: none;   
  11. }   
  12. ……可以直接使用 :not() 伪类来应用元素:   
  13.   
  14. .nav li:not(:last-child) {   
  15.   border-right: 1px solid #666;   
  16. }   

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

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

CSS Code复制内容到剪贴板
  1. ..nav li:first-child ~ li {   
  2.   
  3.   border-left: 1px solid #666;   
  4. }   
  5.   

3. 页面顶部阴影

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

CSS Code复制内容到剪贴板
  1. body:before {   
  2.           content: "";   
  3.           position: fixed;   
  4.           top: -10px;   
  5.           left: 0;   
  6.           width: 100%;   
  7.           height: 10px;   
  8.     
  9.           -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);   
  10.           -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);   
  11.           box-shadow: 0px 0px 10px rgba(0,0,0,.8);   
  12.     
  13.           z-index: 100;   
  14. }   
  15.   

4. 给 body 添加行高

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

CSS Code复制内容到剪贴板
  1. body {   
  2.   line-height: 1;   
  3. }  

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

5. 所有一切都垂直居中

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

CSS Code复制内容到剪贴板
  1. html, body {   
  2.   height: 100%;   
  3.   margin: 0;   
  4. }   
  5.   
  6. body {   
  7.   -webkit-align-items: center;     
  8.   -ms-flex-align: center;     
  9.   align-items: center;   
  10.   display: -webkit-flex;   
  11.   display: flex;   
  12. }   
  13.   

看,是不是很简单。

注意:在IE11中要小心flexbox。

6. 逗号分隔的列表

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

CSS Code复制内容到剪贴板
  1. ul > li:not(:last-child)::after {   
  2.   content: ",";   
  3. }   
  4.   

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

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

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

CSS Code复制内容到剪贴板
  1. li {   
  2.   display: none;   
  3. }   
  4.   
  5. /* select items 1 through 3 and display them */  
  6. li:nth-child(-n+3) {   
  7.   display: block;   
  8. }   
  9.   

8. 对图标使用 SVG

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

CSS Code复制内容到剪贴板
  1. .logo {   
  2.   background: url("logo.svg");   
  3. }  

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

9. 优化显示文本

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

XML/HTML Code复制内容到剪贴板
  1. html {   
  2.   -moz-osx-font-smoothing: grayscale;   
  3.   -webkit-font-smoothing: antialiased;   
  4.   text-rendering: optimizeLegibility;   
  5. }   
  6.   

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

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

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

CSS Code复制内容到剪贴板
  1. .slider ul {   
  2.   max-height: 0;   
  3.   overlow: hidden;   
  4. }   
  5.   
  6. .slider:hover ul {   
  7.   max-height: 1000px;   
  8.   transition: .3s ease;   
  9. }   
  10.   

11. 继承 box-sizing

让 box-sizing 继承 html:

XML/HTML Code复制内容到剪贴板
  1. html {   
  2.   box-sizing: border-box;   
  3. }   
  4.   
  5. *, *:before, *:after {   
  6.   box-sizing: inherit;   
  7. }   
  8.   

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

12. 表格单元格等宽

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

CSS Code复制内容到剪贴板
  1. .calendar {   
  2.   table-layout: fixed;   
  3. }   
  4.   

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

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

CSS Code复制内容到剪贴板
  1. .list {   
  2.   display: flex;   
  3.   justify-content: space-between;   
  4. }   
  5.   
  6. .list .person {   
  7.   flex-basis: 23%;   
  8. }   
  9.   

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

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

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

CSS Code复制内容到剪贴板
  1. a[href^="http"]:empty::before {   
  2.   content: attr(href);   
  3. }   
  4.   

相当方便。

15. 检测鼠标双击

HTML:

XML/HTML Code复制内容到剪贴板
  1. <div class="test3">  
  2.   <span><input type="text" value=" " readonly="true" />  
  3.   <a href="http://renpingjun.com">Double click me</a></span>  
  4. </div>  
  5.   

CSS:

CSS Code复制内容到剪贴板
  1. .test3 span {   
  2.   position: relative;   
  3. }   
  4. .test3 span a {   
  5.   position: relative;   
  6.   z-index: 2;   
  7. }   
  8. .test3 span a:hover, .test3 span a:active {   
  9.   z-index: 4;   
  10. }   
  11. .test3 span input {   
  12.   background: transparent;   
  13.   border: 0;   
  14.   cursor: pointer;   
  15.   position: absolute;   
  16.   top: -1px;   
  17.   left: 0;   
  18.   width: 101%;  /* Hacky */  
  19.   height: 301%; /* Hacky */  
  20.   z-index: 3;   
  21. }   
  22. .test3 span input:focus {   
  23.   background: transparent;   
  24.   border: 0;   
  25.   z-index: 1;   
  26. }   
  27.   

16. CSS 写出三角形

利用border来写三角形代码,并且兼容IE6.

CSS Code复制内容到剪贴板
  1. /* create an arrow that points up */  
  2. div.arrow-up {   
  3.   width:0px;   
  4.   height:0px;   
  5.   border-left:5px solid transparent;  /* left arrow slant */  
  6.   border-right:5px solid transparent; /* right arrow slant */  
  7.   border-bottom:5px solid #2f2f2f; /* bottom, add background color here */  
  8.   font-size:0px;   
  9.   line-height:0px;   
  10. }   
  11.     
  12. /* create an arrow that points down */  
  13. div.arrow-down {   
  14.   width:0px;   
  15.   height:0px;   
  16.   border-left:5px solid transparent;   
  17.   border-right:5px solid transparent;   
  18.   border-top:5px solid #2f2f2f;   
  19.   font-size:0px;   
  20.   line-height:0px;   
  21. }   
  22.     
  23. /* create an arrow that points left */  
  24. div.arrow-left {   
  25.   width:0px;   
  26.   height:0px;   
  27.   border-bottom:5px solid transparent;  /* left arrow slant */  
  28.   border-top:5px solid transparent; /* right arrow slant */  
  29.   border-right:5px solid #2f2f2f; /* bottom, add background color here */  
  30.   font-size:0px;   
  31.   line-height:0px;   
  32. }   
  33.     
  34. /* create an arrow that points right */  
  35. div.arrow-rightright {   
  36.   width:0px;   
  37.   height:0px;   
  38.   border-bottom:5px solid transparent;  /* left arrow slant */  
  39.   border-top:5px solid transparent; /* right arrow slant */  
  40.   border-left:5px solid #2f2f2f; /* bottom, add background color here */  
  41.   font-size:0px;   
  42.   line-height:0px;   
  43. }   
  44.   

17. CSS3 calc() 的使用

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

CSS Code复制内容到剪贴板
  1. /* basic calc */  
  2. .simpleBlock {   
  3.   width: calc(100% - 100px);   
  4. }   
  5.     
  6. /* calc in calc */  
  7. .complexBlock {   
  8.   width: calc(100% - 50% / 3);   
  9.   padding: 5px calc(3% - 2px);      margin-left: calc(10% + 10px);   
  10. }   
  11.   

18. 文本渐变

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

CSS Code复制内容到剪贴板
  1. h2[data-text] {   
  2.   position: relative;   
  3. }   
  4. h2[data-text]::after {   
  5.   content: attr(data-text);   
  6.   z-index: 10;   
  7.   color: #e3e3e3;   
  8.   position: absolute;   
  9.   top: 0;   
  10.   left: 0;   
  11.   -webkit-mask-image: -webkit-gradient(linear, left top, left bottombottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));}   
  12.   

19. 禁用鼠标事件

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

.disabled { pointer-events: none; }

20. 模糊文本

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

CSS Code复制内容到剪贴板

  • 上一条:
    CSS实现曲面阴影效果的简单实例(推荐)
    下一条:
    CSS3字体效果的设置方法小结
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客