前端经常使用样式总结

本文所有使用 scss + autoprefixer
Brower support: flex box(IE 10+), :before & :after IE 8+(IE8 only supports the single-colon)css

Sticky footer

内容高度不够时,footer 依然显示到最下面
大概有这样的 html 结构html

<div id="content">
</div>
<div id="footer">&copy; Brook.inc</div>
  • flex 布局web

    html {
      height: 100%;
    }
    $footer-height: 30px;
    body {
      min-height: 100%;
      display: flex;
      flex-direction: column;
    }
    #content {
      flex: 1;
    }
    
    #footer {
      line-height: $footer-height;
      text-align: center;
    }

查看 demo微信

  • -margin & paddingsvg

    html, body {
      height: 100%;
    }
    $footer-height: 30px;
    #content {
      min-height: 100%;
      margin-bottom: -$footer-height;
      padding-bottom: $footer-height;
      // requires box-sizing: border-box;
      // 下面的不须要 border-box
      /*
      &::after {
        content: '';
        display: block;
        height: $footer-height; // footer height
      }
      */
    }
    
    #footer {
      line-height: $footer-height;
      text-align: center;
    }

查看 demo布局

absolute center

不定宽高的垂直水平居中flex

  • 首先 flexui

    .center-flex {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  • transformspa

    .center-transform {
      img {
        position: relative; left: 50%; top: 50%;
        transform: translate(-50%, -50%);
      }
    }
  • table-cellcode

    .center-tb-cell {
      display: table-cell;
      text-align: center; vertical-align: middle;
    }
  • :after,兼容性也不错能够,不想用 table-cell 时能够用

    .center-ib {
      text-align: center;
      &::after {
        content: '';
        display: inline-block; vertical-align: middle;
        height: 100%;
      }
      img {
        vertical-align: middle;
      }
    }

垂直水平居中 demo

Cenerting float

居中浮动元素

.center-float {
  // 父容器会产生滚动条
  float: left; position: relative; left: 50%;
  > ul {
    position: relative; left: -50%;
  }
}

float 居中 demo

Autohiding scrollbars for IE

IE 自动隐藏滚动条 (works in Edge and IE10/11)

html {
  -ms-overflow-style: -ms-autohiding-scrollbar;
}

如下是针对移动端 (mobile)的

Tap highlight

点击时高亮背景

.item {
  -webkit-tap-highlight-color: rgba(0,0,0,0); // 隐藏系统自带的背景
  // add `ontouchstart` attribte on body
  // to allow :active work (if :active not work)
  &:active {
    background: #ECECEC
  }
}

只添加上面的样式,:active 在移动端不必定(已经引入 zepto 的已经包含下面的 js 了)生效,须要下面的js

document.body.addEventListener('touchstart', function() {}, false);
// 也能够直接在body上添加 `ontouchstart` 属性,

Half pixel border

移动端半像素的边框

  • :after + scale(0.5) (能够是某一到两个边,或者所有边(支持圆角))

  • svg background

  • svg border-image
    查看 demo

Cells

移动端经常使用的 cells 布局

clipboard.png

查看微信我页面 demo (cell + tap highlight + half pixel border)

smooth scroll in webkit

平滑滚动

-webkit-overflow-scrolling: touch;

原文地址:https://uedsky.com/2016-05/front-end-css-summary/获取最佳阅读体验并参与讨论,请访问原文

相关文章
相关标签/搜索