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

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

  2. 为body添加行高web

  3. 垂直居中任何元素浏览器

  4. 逗号分离的列表ide

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

  6. 使用SVG图标学习

  7. 文本显示优化字体

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

  9. 继承box-sizing优化

  10. 表格单元格等宽flexbox

  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
相关文章
相关标签/搜索