好程序员web前端分享12个CSS高级技巧汇总
发布日期:2021-08-31 11:35:52 浏览次数:4 分类:技术文章

本文共 4659 字,大约阅读时间需要 15 分钟。

  hot3.png

好程序员web前端分享下面这些CSS高级技巧,一般人我可不告诉他哦。

  使用 :not() 在菜单上应用/取消应用边框
  给body添加行高
  所有一切都垂直居中
  逗号分隔的列表
  使用负的 nth-child 选择项目
  对图标使用SVG
  优化显示文本
  对纯CSS滑块使用 max-height
  继承 box-sizing
  表格单元格等宽
  用Flexbox摆脱外边距的各种hack
  使用属性选择器用于空链接

  使用 :not() 在菜单上应用/取消应用边框
  先给每一个菜单项添加边框

  1. <p><font size="3">
  2. </font></p>
  3. <p><font size="3">  /* add border */</font></p>
  4. <p><font size="3">  .nav li {</font></p>
  5. <p><font size="3">  border-right: 1px solid #666;</font></p>
  6. <p><font size="3">  }</font></p>

……然后再除去最后一个元素……

//* remove border */

  1. <p><font size="3">  .nav li:last-child {</font></p>
  2. <p><font size="3">  border-right: none;</font></p>
  3. <p><font size="3">  }</font></p>
  4. <p><font size="3">  ……可以直接使用 :not() 伪类来应用元素:</font></p>
  5. <p><font size="3">  .nav li:not(:last-child) {</font></p>
  6. <p><font size="3">  border-right: 1px solid #666;</font></p>
  7. <p><font size="3">  }</font></p>

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

  当然,如果你的新元素有兄弟元素的话,也可以使用通用的兄弟选择符(~):
..nav li:first-child ~ li {

  1. <p><font size="3">  border-left: 1px solid #666;</font></p>
  2. <p><font size="3">  }</font></p>

  给 body添加行高

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

  1. <p><font size="3">  line-height: 1;</font></p>
  2. <p><font size="3">  }</font></p>

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

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

  1. <p><font size="3">
  2. </font></p>
  3. <p><font size="3">  html, body {</font></p>
  4. <p><font size="3">  height: 100%;</font></p>
  5. <p><font size="3">  margin: 0;</font></p>
  6. <p><font size="3">  }</font></p>
  7. <p><font size="3">  body {</font></p>
  8. <p><font size="3">  -webkit-align-items: center;</font></p>
  9. <p><font size="3">  -ms-flex-align: center;</font></p>
  10. <p><font size="3">  align-items: center;</font></p>
  11. <p><font size="3">  display: -webkit-flex;</font></p>
  12. <p><font size="3">  display: flex;</font></p>
  13. <p><font size="3">  }</font></p>

  看,是不是很简单。

  注:在IE11中要小心flexbox。
  逗号分隔的列表
  让HTML列表项看上去像一个真正的,用逗号分隔的列表:

  1. <p><font size="3">
  2. </font></p>
  3. <p><font size="3">  ul > li:not(:last-child)::after {</font></p>
  4. <p><font size="3">  content: ",";</font></p>
  5. <p><font size="3">  }</font></p>

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

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

  1. <p><font size="3">  display: none;</font></p>
  2. <p><font size="3">  }</font></p>
  3. <p><font size="3">  /* select items 1 through 3 and display them */</font></p>
  4. <p><font size="3">  li:nth-child(-n+3) {</font></p>
  5. <p><font size="3">  display: block;</font></p>
  6. <p><font size="3">  }</font></p>

  就是这么容易。

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

  1. <p><font size="3">  .logo {</font></p>
  2. <p><font size="3">  background: url("logo.svg");</font></p>
  3. <p><font size="3">  }</font></p>

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

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

  1. <p><font size="3">
  2. </font></p>
  3. <p><font size="3">  html {</font></p>
  4. <p><font size="3">  -moz-osx-font-smoothing: grayscale;</font></p>
  5. <p><font size="3">  -webkit-font-smoothing: antialiased;</font></p>
  6. <p><font size="3">  text-rendering: optimizeLegibility;</font></p>
  7. <p><font size="3">  }</font></p>

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

  对纯CSS滑块使用 max-height
  使用 max-height 和溢出隐藏来实现只有CSS的滑块:
.slider ul {

  1. <p><font size="3">  max-height: 0;</font></p>
  2. <p><font size="3">  overlow: hidden;</font></p>
  3. <p><font size="3">  }</font></p>
  4. <p><font size="3">  .slider:hover ul {</font></p>
  5. <p><font size="3">  max-height: 1000px;</font></p>
  6. <p><font size="3">  transition: .3s ease;</font></p>
  7. <p><font size="3">  }</font></p>

 

  1. <p><font size="3">  继承 box-sizing</font></p>
  2. <p><font size="3">  让 box-sizing 继承 html:</font></p>
  3. <p><font size="3">  html {</font></p>
  4. <p><font size="3">  box-sizing: border-box;</font></p>
  5. <p><font size="3">  }</font></p>
  6. <p><font size="3">  *, *:before, *:after {</font></p>
  7. <p><font size="3">  box-sizing: inherit;</font></p>
  8. <p><font size="3">  }</font></p>

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

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

  1. <p><font size="3">  .calendar {</font></p>
  2. <p><font size="3">  table-layout: fixed;</font></p>
  3. <p><font size="3">  }</font></p>

  用Flexbox摆脱外边距的各种hack

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

  1. <p><font size="3">  .list {</font></p>
  2. <p><font size="3">  display: flex;</font></p>
  3. <p><font size="3">  justify-content: space-between;</font></p>
  4. <p><font size="3">  }</font></p>
  5. <p><font size="3">  .list .person {</font></p>
  6. <p><font size="3">  flex-basis: 23%;</font></p>
  7. <p><font size="3">  }</font></p>

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

  使用属性选择器用于空链接
  当 [url=]元素没有文本值,但 href 属性有链接的时候显示链接:[/url]
[url=]

a[href^="http"]:empty::before {

  1. <p>  content: attr(href);</p>
  2. <p>  }</p>

  相当方便。

转载于:https://my.oschina.net/530504/blog/3043589

转载地址:https://blog.csdn.net/weixin_34072857/article/details/91732843 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:好程序员大数据纪实:HBase知识点集中总结
下一篇:浅说信息安全

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月17日 21时17分54秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

spring boot 与 Ant Design of Vue 实现组织管理布局的实现(二十二) 2019-04-27
spring boot 与 Ant Design of Vue 实现左侧组织树(二十三) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增组织(二十四) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改组织(二十五) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除组织(二十六) 2019-04-27
spring boot 与 Ant Design of Vue 实现获取用户列表(二十七) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增用户(二十八) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改用户(二十九) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除用户(三十) 2019-04-27
spring boot 与 Ant Design of Vue 鉴权体系登录的实现(三十一) 2019-04-27
spring boot 与 Ant Design of Vue 鉴权体系获取用户信息的实现(三十二) 2019-04-27
Druid连接池实现自定义场景的多数据库的连接 2019-04-27
CentOs7命令行(静默)的方式安装oracle数据库 2019-04-27
基于VMware安装CentOs7的镜像 2019-04-27
PL/SQL数据库管理工具的使用 2019-04-27
史上最简单的spring-boot集成websocket的实现方式 2019-04-27
带你玩转属于自己的spring-boot-starter系列(一) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
带你玩转属于自己的spring-boot-starter系列(三) 2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现 2019-04-27