前端开发者应该知道的 CSS 小技巧

  1. 使用:not()去除导航上不需要的边框

  2. 为body添加行高

  3. 垂直居中任何元素

  4. 逗号分离的列表

  5. 使用负nth-child选择元素

  6. 使用SVG图标

  7. 文本显示优化

  8. 在纯CSS幻灯片上使用max-height

  9. 继承box-sizing

  10. 表格单元格等宽

  11. 使用Flexbox摆脱边界Hack

  12. 使用属性选择器选择空链接

使用:not()添加/去除导航上不需要的边框

添加边框…

.nav li { 
     border-right: 1px solid #666; 
}

…然后去除最后一个元素的边框…

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

…使用伪类 :not() 将样式只应用到你需要的元素上:

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

当然,你可以使用.nav li + li 或者 .nav li:first-child ~ li, 但是使用 :not() 的意图特别清晰,CSS选择器按照人类描述它的方式定义边框。

为body添加行高

你不需要分别为每一个 <p>, <h*> 等元素添加行高,而是为body添加:

body {
     line-height: 1;
}

这种方式下,文本元素可以很容易从body继承。

垂直居中任何元素

 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 }

注意:IE11上flexbox的一些 缺陷行为。

逗号分离的列表

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

使用伪类:not() ,这样最后一个元素不会被添加逗号。

使用负 nth-child 选择元素

li {
   display: none;
}
 
/* 选择1到3的元素并显示 */
 
li:nth-child(-n+3) { 
   display: block;
}

或者,你已经学习了一些关于 使用 :not(),尝试:

/* 选择1到3的元素并显示 */
 
li:not(:nth-child(-n+3)){
  display: none;
}

使用SVG图标

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

SVG对所有分辨率类型具有良好的伸缩性,IE9以上的所有浏览器都支持。所以放弃.png,.jpg或gif-jif等任何文件。

注意:如果你使用SVG图标按钮,同时SVG加载失败,下面能帮助你保持可访问性:

.no-svg .icon-only:after {
  content: attr(aria-label);
}

文本显示优化

有些字体在所有的设备上并不是最优显示,因此让设备浏览器来帮忙:

1 html {
2   -moz-osx-font-smoothing: grayscale;
3   -webkit-font-smoothing: antialiased;
4   text-rendering: optimizeLegibility;
5 }

注意:请使用optimizeLegibility。同时,IE/Edge不支持text-rendering。

在纯CSS实现的内容滑块上使用max-height

在纯CSS实现的内容滑块上使用max-height,同时设置overflow hidden:

1 .slider ul {
2   max-height: 0;
3   overlow: hidden;
4 }
5  
6 .slider:hover ul {
7   max-height: 1000px;
8   transition: .3s ease; /* animate to max-height */
9 }

继承box-sizing

1 html {
2   box-sizing: border-box;
3 }
4  *:before, *:after {
5   box-sizing: inherit;
6 }

表格单元格等宽

使用表格会很痛苦,因此使用table-layout:fixed来保持单元格相同的宽度:

使用Flexbox摆脱边界Hack

当使用列约束时,可以抛弃nth-,first- 和 last-child的hacks,而使用flexbox的space-between属性:

1 .list {
2   display: flex;
3   justify-content: space-between;
4 }
5  
6 .list .person {
7   flex-basis: 23%;
8 }

现在列约束总是等间隔出现。

使用属性选择器选择空链接

显示没有文本值但是 href 属性具有链接的 a 元素的链接:

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

这些技巧在当前版本的Chrome,Firefox, Safari, 以及Edge, 和IE11可以工作。

对话框

1 <div class="test-div"></div>
2 <p>完整的一个对话框样式呈现在眼前了,至于对话框的小三角形的方向,相信大家看了上上段对于border介绍的代码也都知道该怎么做了吧,没错,就是改下position的位置,改下border显示颜色的方位~</p>
 1 .test-div{
 2                 position: relative;
 3                 width:150px;
 4                 height: 36px;
 5                 border:black 1px solid;
 6                 border-radius:5px;
 7                 background: rgba(245,245,245,1)
 8             }
 9             .test-div:before,.test-div:after{
10                 content: ""; /* :before和:after必带技能,重要性为满5颗星*/
11                 display: block;
12                 position: absolute;  
13                 top:8px;
14                 width: 0;
15                 height: 0;
16                 border:6px transparent solid;
17             }
18             .test-div:before{
19                 left:-11px;
20                 border-right-color: rgba(245,245,245,1);/*控制箭头方向*/
21                 z-index:1
22             }
23             .test-div:after{
24                 left:-12px;
25                 border-right-color: rgba(0,0,0,1);
26                 z-index: 0
27             }
View Code
原文地址:https://www.cnblogs.com/dehua-chen/p/5442824.html