下面这些CSS高级技巧,通常人我可不告诉他哦。html
先给每个菜单项添加边框web
/* add border */ .nav li { border-right: 1px solid #666; }
……而后再除去最后一个元素……浏览器
//* remove border */ide
.nav li:last-child { border-right: none; }
……能够直接使用 :not() 伪类来应用元素:svg
.nav li:not(:last-child) { border-right: 1px solid #666; }
这样代码就干净,易读,易于理解了。字体
固然,若是你的新元素有兄弟元素的话,也能够使用通用的兄弟选择符(~):flex
..nav li:first-child ~ li {优化
border-left: 1px solid #666; }
你不须要分别添加 line-height 到每一个 <p>
,
<h*>
等。只要添加到 body 便可:flexbox
body { line-height: 1; }
这样文本元素就能够很容易地从 body 继承。url
要将全部元素垂直居中,太简单了:
html, body { height: 100%; margin: 0; } body { -webkit-align-items: center; -ms-flex-align: center; align-items: center; display: -webkit-flex; display: flex; }
看,是否是很简单。
注:在IE11中要当心flexbox。
让HTML列表项看上去像一个真正的,用逗号分隔的列表:
ul > li:not(:last-child)::after { content: ","; }
对最后一个列表项使用 :not() 伪类。
在CSS中使用负的 nth-child 选择项目1到项目n。
li { display: none; } /* select items 1 through 3 and display them */ li:nth-child(-n+3) { display: block; }
就是这么容易。
咱们没有理由不对图标使用SVG:
.logo { background: url("logo.svg"); }
SVG对全部的分辨率类型都具备良好的扩展性,并支持全部浏览器都回归到IE9。这样能够避开.png、.jpg或.gif文件了。
有时,字体并不能在全部设备上都达到最佳的显示,因此可让设备浏览器来帮助你:
html { -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
注:请负责任地使用 optimizeLegibility。此外,IE /Edge没有 text-rendering 支持。
使用 max-height 和溢出隐藏来实现只有CSS的滑块:
.slider ul { max-height: 0; overlow: hidden; } .slider:hover ul { max-height: 1000px; transition: .3s ease; }
让 box-sizing 继承 html:
html { box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; }
这样在插件或杠杆其余行为的其余组件中就能更容易地改变 box-sizing 了。
表格工做起来很麻烦,因此务必尽可能使用 table-layout: fixed 来保持单元格的等宽:
.calendar { table-layout: fixed; }
当须要用到列分隔符时,经过flexbox的 space-between 属性,你就能够摆脱nth-
,first-
,和 last-child 的
hack了:
.list { display: flex; justify-content: space-between; } .list .person { flex-basis: 23%; }
如今,列表分隔符就会在均匀间隔的位置出现。
当 <a> 元素没有文本值,但 href 属性有连接的时候显示连接:
a[href^="http"]:empty::before { content: attr(href); }
至关方便。
这些高级技巧在Chrome、Firefox、Safari、Edge的当前版本,以及IE11中都能有效工做。