分类 前端探索 下的文章

自古以来(是有多古?)~网页CSS的垂直置中需求始终没有停过,而其困难度也始终没有让人轻松过,经过了每位开发先烈们的研究后,据说CSS的垂直置中技巧已达到近十种之多,但始终鲜为人知,部分公司甚至将CSS的垂直置中技巧当成了面试题目,其重要性可见一斑,经过了Amos通灵了一下之后把垂直置中的写法扩增到了23个,今天就让Amos带着大家轻松的了解一下CSS的垂直置中的方式吧。

1、Line-height

适用情景:单行文字垂直居中技巧

这个方式应该是最多人知道的了,常见于单行文字的应用,像是按钮这一类对象,或者是下拉框、导航此类元素最常见到的方式了。此方式的原理是在于将单行文字的行高设定后,文字会位于行高的垂直中间位置,利用此原理就能轻松达成垂直居中的需求了。


    <div class="content">Lorem ipsam.</div>

    .content{
      width: 400px;
      background: #ccc;
      line-height:100px;
      margin: auto;
    }

2、Line-height + inline-block

适用情景:多对象的垂直居中技巧

既然可以使用第一种方式对行元素达成垂直居中的话,当然没有理由不能做到多行啊~但是你需要将多个元素或多行元素当成一个行元素来看待,所以我们必须要将这些数据多包一层,并将其设定为inline-block,并在该inline-block对象的外层对象使用inline-block来代替height的设置,如此便可以达到垂直居中的目的了,从使你的数据是包含了标题跟内容在内也可以正常的垂直居中了。



    <div class="box box2">
      <div class="content">
        立马来看Amos实际完成的    
        
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      border: 1px solid #f00;
      margin: auto;
      line-height: 200px;
      text-align: center;
    }
    .box2 .content{
      display: inline-block;
      height: auto;
      line-height:1;
      width: 400px;
      background: #ccc;
    }

3、:before + inline-block

适用情景:多对象的CSS垂直居中技巧

:before 伪类元素搭配 inline-block 属性的写法应该是很传统的垂直居中的技巧了,此方式的好处在于子元素居中可以不需要特别设定高度,我们将利用:before伪类元素设定为100%高的inline-block,再搭配上将需要居中的子元素同样设置成inline-block性质后,就能使用vertical-align:middle来达到垂直居中的目的了,此方式在以往其实是个非常棒的垂直居中解决方案,唯独需要特别处理掉inline-block元素之间的4-5px空间这个小缺陷,但也很实用了。



    <h2>3.:before + inline-block</h2>
    <div class="box box3">
      <div class="content">
        立马来看Amos实际完成的    
        
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      text-align: center;
    }
    .box::before{
      content:'';
      display: inline-block;
      height: 100%;
      width: 0;
      vertical-align: middle;
    }
    .box .content{
      width: 400px;
      background: #ccc;
      display: inline-block;
      vertical-align: middle;
    }

4、absolute + margin 负值

适用情景:多行文字的垂直居中技巧

谁说绝对定位要少用?Amos认为没有少用多用的问题,重点在于你是否有妥善运用才是重点,绝对定位在这个例子中会设置top:50%来抓取空间高度的50%,接着在将居中元素的margin-top设定为负一半的高度,这样就能让元素居中了,此方法可是自古以来流传多年的居中方式呢?



    <h2>4.absolute + margin 負值</h2>
    <div class="box box4">
      <div class="content">
        立马来看Amos实际完成的    
        
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      position: relative;
    }
    .box4 .content{
      width: 400px;
      background: #ccc;
      height: 70px;
      position: absolute;
      top:50%;
      left: 50%;
      margin-left: -200px;
      margin-top: -35px;
    }

5、absolute + margin auto

适用情景:多行文字的垂直居中技巧

又一个绝对定位的垂直居中的方案,这个方式比较特别一点,当元素设置为绝对定位后,假设它是抓不到整体可运用的空间范围,所以margin:auto会失效,但当你设置了top:0;bottom:0;时,绝对定位元素就抓到了可运用的空间了,

这时你的margin:auto就生效了(神奇吧),如果你的绝对定位元素需要水平居中于父层,那你同样可以设定left:0;right:0;来让绝对定位元素取得空间可运用范围,再让marign-left与margin-right设定为auto即可居中。

但此方式的缺点是你的定位元素必须有固定的宽高(百分比也算)才能正常居中。



    <h2>5.absolute + translate(-50%, -50%)</h2>
    <div class="box box5">
      <div class="content">
        立马来看Amos实际完成的  
          <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      position: relative;
    }
    .content{
      width: 400px;
      background: #ccc;
      height: 70px;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
    }

6、absolute + translate

适用情景:多行文字的垂直居中技巧

在一个绝对定位居中的方式,此方式应该算是最方便的了,因为此居中的定位元素不需要固定的宽高,我们利用绝对定位时的top 与right设置元素的上方跟左方各为50%,再利用translate(-50%,-50%)位移居中元素自身宽与高的50%就能达成居中的目的了。(css3好棒)



    <h2>6.absolute + margin: auto</h2>
    <div class="box box6">
      <div class="content">
        立马来看Amos实际完成的    
       
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      position: relative;
    }
    .box5 .content{
      width: 400px;
      background: #ccc;
      position: absolute;
      top:50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

7.Flex + align-items

适用情景:多行文字的垂直居中技巧

Flex!前端的毒品!后端的宝物!这东西自从面世之后就不断的考验网页开发者的良心,到底要不要抛弃float拥抱flex,我想这答案人人心中自由一把尺,但先碰Flex再碰Float可谓先甜后苦,这顺序到底要倒吃甘蔗还是正吃甘蔗是实在难说,自从有了Flex之后,小孩考试一百分,设计网页不跑版,客户网页都RWD,老板赚钱好开心,我也加薪(加班)好甘心,不由的说Flex真的是一个神物,我们只要设定父层display:flex以及设定次轴(cross axis)属性align-items:center 就好了(说那么多结果重点就一行字是哪招啦),这个方式的优点是此层不需要设定高度即可自动居中,且原始代码干净无比,真的是用一次就让你升天啦。



    <h2>7.Flex + align-items</h2>
    <div class="box box7">
      <div class="content">
        立马来看Amos实际完成的    
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: flex;
      justify-content: center;
      align-items: center; 
    }
    .content{
      width: 400px;
      background: #ccc;
    }

8、Flex + :before + flex-grow

适用情景:多行文字的垂直居中技巧

Flex有多种方式可以让你把数据居中,适用Flex-grow的延展特性来达成,这个例子中Amos适用了flex-direction:column直式排法,搭配:before伪元素适用flex-grow伸展值能够取得剩下所有空间的特性,把它设定成一半的剩余空间就能做到把内容数据准确的推到垂直中间位置,算是个传统技法的延伸方式。这样的话上面第七个方式不是比较快?



    <h2>8.Flex + before + flex-grow</h2>
    <div class="box box8">
      <div class="content">
        立马来看Amos实际完成的    
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .box:before{
      content: '';
      flex-grow: .5;
    }
    .content{
      width: 400px;
      background: #ccc;
    }

9、Flex + margin

适用情景:多行文字的垂直居中技巧

继续用Flex来居中,由于Flex元素对空间解读的特殊性,我们只要在父层元素设定display:flex,接着在需要垂直居中的元素上设定margin:auto,即可自动居中



    <h2>9.Flex + margin</h2>
    <div class="box box9">
      <div class="content">
        立马来看Amos实际完成的   
         <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: flex;
    }
    .content{
      width: 400px;
      background: #ccc;
      margin: auto;
    }

10、Flex + align-self

适用情景:多行文字的垂直居中技巧

align-self 应该大家都不陌生,基本上就是对flex次轴cross axis 的个别对齐方式只要对单一子层元素设定align-self:center就能达成垂直居中的目的了。



    <h2>10.Flex + align-self</h2>
    <div class="box box10">
      <div class="content">
        立马来看Amos实际完成的  
          <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: flex;
      justify-content: center;
    }
    .content{
      width: 400px;
      background: #ccc;
      align-self: center
    }

11、Flex + align-content

适用情景:多行文字的垂直居中技巧

在正常的状况下,align-content 仅能对次轴多行flex item做居中,但是当我今天子层元素不确定有多少个时,且有时可能会有单个的情况出现时,此技巧就能用到了(当然你也能有其他解法),既然是多行子元素才能用,那我们就为单个子组件多加两个兄弟吧,使用:before以及:after 来让子元素增加到多个,这样就能使用flex的align-content属性来居中



    <h2>11.Flex + align-content</h2>
    <div class="box box11">
      <div class="content">
        立马来看Amos实际完成的   
         <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-content: center;
    }
    .content{
      width: 400px;
      background: #ccc;
    }
    .box11:before,
    .box11:after{
      content: '';
      display: block;
      width:100%;
    }

12、Grid + template

适用情景:多行文字的垂直居中技巧

CSS Grid最令人惊讶的就是这个template的功能了,简直就是把块元素当画布在使用,我们仅需要把模板设置成三列,就能搞定垂直居中了



    <h2>12.Grid + template</h2>
    <div class="box box12">
      <div class="content">
        立马来看Amos实际完成的    
        
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: grid;
      grid-template-rows: 1fr auto 1fr;
      grid-template-columns: 1fr auto 1fr;
      grid-template-areas: 
        '. . .'
        '. amos .'
        '. . .';
    }
    .content{
      width: 400px;
      background: #ccc;
      grid-area: amos;
    }

13、Grid + align-items

适用情景:多行文字的垂直居中技巧

align-items不仅是Flex可用,连CSS Grid也拥有此属性可使用,但在Flex中align-items是针对次轴cross axis作对齐,而在CSS Grid中则是针对Y轴做对齐,你可以把它想象成是表格中储存单元格的vertical-align属性看待,就可以很好理解了



    <h2>13.Grid + align-items</h2>
    <div class="box box13">
      <div class="content">
        立马来看Amos实际完成的    
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: grid;
      justify-content: center;
      align-items: center; 
    }
    .content{
      width: 400px;
      background: #ccc;
    }

14、Grid + align-content

适用情景:杜航文字的垂直居中技巧

CSS Grid的align-content跟Flex的align-content有点差异,CSS Grid对于空间的解释会跟Flex有一些些的落差,所以导致align-content在Flex中仅能针对多行元素起作用,但在Grid中就没这个问题,所以我们可以很开心的使用align-content来对子元素做垂直居中,丝毫不费力气



    <h2>14.Grid + align-content</h2>
    <div class="box box14">
      <div class="content">
        立马来看Amos实际完成的    
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: grid;
      justify-content: center;
      align-content: center; 
    }
    .content{
      width: 400px;
      background: #ccc;
    }

15、Grid + align-self

适用情景:多行文字的垂直居中技巧

align-self 应该大家都不陌生,基本上就是对grid Y轴的个别对齐方式,只要对单一子层元素设置为align-self:center就能达成垂直居中的目的了



    <h2>15.Grid + align-self</h2>
    <div class="box box15">
      <div class="content">
        立马来看Amos实际完成的   
         <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: grid;
      justify-content: center;
    }
    .content{
      width: 400px;
      background: #ccc;
      align-self: center;
    }

16、Grid + place-items

适用情景:多行文字的垂直居中技巧

place-items这属性不知道有多少人用过,此属性是align-items与justify-items的缩写,简单的说就是水平与垂直的对齐方式,想当然的,设定center就能居中



    <h2>16.Grid + place-items</h2>
    <div class="box box16">
      <div class="content">
        立马来看Amos实际完成的   
         <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: grid;
      height: 150px;
      margin: 0 auto;
      place-items: center;
    }
    .content{
      width: 400px;
      background: #ccc;
    }

17、Grid + place-content

适用情景:多行文字的垂直居中技巧

place-content这属性有多少人用过,此属性是align-content与justify-content的缩写,简单的说就是水平与垂直的对齐方式,想当然的,设置center就能居中了



<h2>17.Grid + place-content</h2>
<div class="box box17">
  <div class="content">
    立马来看Amos实际完成的   
<a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: grid;
      height: 150px;
      margin: 0 auto;
      place-content: center;
    }
    .content{
      width: 400px;
      background: #ccc;
    }

18、Grid + margin

适用情景:多行文字的垂直居中技巧

继续用Grid来居中,由于Grid元素对空间解读的特殊性,我们只要在父层元素设定display:grid,接着在需要垂直居中的元素上设置margin:auto即可自动居中。怎么这描述似曾相识。



  
    <h2>18.Grid + margin</h2>
    <div class="box box18">
      <div class="content">
        立马来看Amos实际完成的   
         <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      display: grid;
    }
    .content{
      width: 400px;
      background: #ccc;
      margin:auto;
    }

19、Display:table-cell

适用情景:多行文字的垂直居中技巧

这一招我想有点年纪的开发者应该都有看过,当然像我这么嫩的开发者当然是第一次看到啦,这一招的原理在于使用 CSS display属性将div设置成表格的单元格,这样就能利用支持存储单元格对齐的vertical-align属性来将信息垂直居中



<h2>19.display: table-cell</h2>
    <div class="box box19">
      <div class="content">
        立马来看Amos实际完成的    
        
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
        text-align: center;
        display: table-cell;
      vertical-align: middle;
    }
    .content{
      width: 400px;
      background: #ccc;
      margin: auto;
    }

20、calc

适用情景:多行文字的垂直居中技巧

Cale是计算机英文单词calculator的缩写,这个由微软提出的css 方法,真的是网页开发者的一个福音。我们竟然可以在网页中直接做计算,这真是太猛了,从此我们再也不用在那边绞尽脑汁的数学计算了,或是想办法用js来动态计算,我们可以很轻松的利用calc()这个方法,来将百分比及时且动态的计算出实际要的是什么高度,真可谓是划时代的一个方法啊,但这个方法需要注意的是大量使用的话,网页性能会是比较差的,所以请谨慎使用。



    <h2>20.calc</h2>
    <div class="box box20">
      <div class="content">
        立马来看Amos实际完成的   
         <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
    }
    .content{
      width: 400px;
      background: #ccc;
      position: relative;
      top:calc((100% - 70px) / 2);
      margin:auto;
      height: 70px;
    }

21、Relative + translateY

适用情景:多行文字的垂直居中技巧

这个技巧是利用了top:50%的招式,让你的元素上方能产生固定百分比的距离,接着让要居中的元素本身使用tanslateY的百分比来达成垂直居中的需求,translate是一个很棒的属性,由于translate的百分比单位是利用元素自身的尺寸作为100%,这样让我们要利用元素自身宽高做事变得方便很多。



    <h2>21.relative + translateY(-50%)</h2>
    <div class="box box21">
      <div class="content">
        立马来看Amos实际完成的    
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>




    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
    }
    .content{
      width: 400px;
      background: #ccc;
      position: relative;
      top: 50%;
      transform: translateY(-50%);
      margin: auto;
    }

22、padding

适用情景:多行文字的垂直居中技巧

什么!这也算垂直居中技巧,连我奶奶都知道这方式吧

对的,这的确也算是一种垂直居中的方式,不可讳言的这方式真的是简单过头了,以至于有些开发者认为这种方式都不能算是一种垂直居中的技巧,但同样的你无法反驳的是,我的数据的确垂直居中啦,好啦,就当我硬凹吧,你说的对,好吧



    <h2>22.padding</h2>
    <div class="box box22">
      <div class="content">
        立马来看Amos实际完成的    
        
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
          CSS3精美相册效果    </a>
        效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      border: 1px solid #f00;
      margin: auto;
      height: auto;
      padding: 50px 0;
    }
    .content{
      width: 400px;
      background: #ccc;
      margin: auto;
    }

23、Write-mode

适用情景:多行文字的垂直剧种技巧

这个方式应该是比较少见到的有人使用的了,这个想法是被老友Paul所激发的,write-mode这个css属性的功能基本上跟垂直居中是八竿子打不着,它的用途是改变文字书写的方向从横变竖,且支持度从很早期的IE5就有支持了,但当时Amos很少使用,一来是网页多是横书较多,另外当时除了IE浏览器意外,其他浏览器的支持度都不是很好,也就很少使用了。

使用write-mode将一整个文字容器变成直书,接着将此容器利用text-align:center来达到垂直居中的目的,白话一点的解说就是,你把原本横排的文字变成竖排,所以原本横排用到的水平对齐方式,就变成了控制直排的中间了,原理就是这么简单。

但要特别注意的是浏览器对此语法的支持度来说,需要拆开写法才行,不然某些浏览器的语法不同,可能会让你的网页在某些浏览器上看起来无效,这会是最需要注意到的。



    <h2>23.writing-mode</h2>立马来看Amos实际完成的
    <div class="box box23">
      <div class="content">
        <div class="txt">
          立马来看Amos实际完成的      
    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
            CSS3精美相册效果      </a>
          效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!
          這個置中的想法來自於 Paul 
        </div>
      </div>
    </div>



    h2{
      text-align: center;
    }
    .box{
      width: 500px;
      height: 250px;
      border: 1px solid #f00;
      margin: auto;
      writing-mode: tb-lr; /* for ie11 */
      writing-mode: vertical-lr;
      text-align: center;
      margin:0 auto;
    }
    .content{
      width: 400px;
      background: #ccc;
      display: inline-block; /* for ie & edge */
      width: 100%;
      writing-mode: lr-tb;
      margin: auto; 
      text-align: left;
    }
    .box .txt{
      width: 80%;
      margin: auto;
    }

看完了这23中css垂直居中写法,不知道你用过哪几种呢?又学到了那几种呢?反正我是涨知识了

作者 | Amos
来源 | http://csscoke.com/2018/08/21/css-vertical-align/

HTML,CSS,font-family:中文字体的英文名称




中文名 对照名 css写法
宋体 SimSun font-family:SimSun;
黑体 SimHei font-family:SimHei;
微软雅黑 Microsoft YaHei font-family:Microsoft YaHei;
微软正黑体 Microsoft JhengHei font-family:Microsoft JhengHei;
新宋体 NSimSun font-family:NSimSun;
新细明体 PMingLiU font-family:PMingLiU;
细明体 MingLiU font-family:MingLiU;
标楷体 DFKai-SB font-family:DFKai-SB;
仿宋 FangSong font-family:FangSong;
楷体 KaiTi font-family:KaiTi;
仿宋_GB2312 FangSong_GB2312 font-family:FangSong_GB2312;
楷体_GB2312 KaiTi_GB2312 font-family:KaiTi_GB2312;

宋体:SimSuncss中中文字体(font-family)的英文名称




Mac OS的一些:

中文名 英文名 css写法
华文细黑 STHeiti Light [STXihei] font-family:STHeiti Light;
华文黑体 STHeiti font-family:STHeiti;
华文楷体 STKaiti font-family:STKaiti;
华文宋体 STSong font-family:STSong;
华文仿宋 STFangsong font-family:STFangsong;
儷黑 Pro LiHei Pro Medium font-family:Pro LiHei Pro Medium;
儷宋 Pro LiSong Pro Light font-family: Pro LiSong Pro Light;
標楷體 BiauKai font-family:BiauKai;
蘋果儷中黑 Apple LiGothic Medium font-family:Apple LiGothic Medium;
蘋果儷細宋 Apple LiSung Light font-family:Apple LiSung Light;




Windows的一些:


新細明體:PMingLiU
細明體:MingLiU
標楷體:DFKai-SB
黑体:SimHei
新宋体:NSimSun
仿宋:FangSong
楷体:KaiTi
仿宋_GB2312:FangSong_GB2312
楷体_GB2312:KaiTi_GB2312
微軟正黑體:Microsoft JhengHei
微软雅黑体:Microsoft YaHei
装Office会生出来的一些:
隶书:LiSu
幼圆:YouYuan
华文细黑:STXihei
华文楷体:STKaiti
华文宋体:STSong
华文中宋:STZhongsong
华文仿宋:STFangsong
方正舒体:FZShuTi
方正姚体:FZYaoti
华文彩云:STCaiyun
华文琥珀:STHupo
华文隶书:STLiti
华文行楷:STXingkai
华文新魏:STXinwei




几个网站使用的好看的字体实例:

例1(小米米官网):font-family: “Arial”,”Microsoft YaHei”,”黑体”,”宋体”,sans-serif;

例2(淘宝技术研发中心):font: 12px/1.5 Tahoma,Helvetica,Arial,’宋体’,sans-serif;

例3(加网 ):font: 14px/1.5 ‘Microsoft YaHei’,arial,tahoma,\5b8b\4f53,sans-serif;

例4(淘宝UED):font: 12px/1 Tahoma,Helvetica,Arial,”\5b8b\4f53″,sans-serif;

例5(一淘UX):font-family: Helvetica, ‘Hiragino Sans GB’, ‘Microsoft Yahei’, ‘微软雅黑’, Arial, sans-serif;


前言

前几天看了一篇文章 , 颠覆了我对 CSS 认识,心中无数次蹦出一个念头:'WC,还能特么这么用,这特么太叼了' ...

于是我迫不及待想跟你们一起分享分享,以后你也可以在别人面前炫(装)耀(B)了~

ps:本文原创不是我,我只是搬运工,看到好东西与大家分享而已

装B指南

本文中,所有的图形都是在单个标签内实现的,大量使用了 CSS3中的 ::before::after 伪元素, transparentborder,多重线性与径向渐变,多重内外阴影,如果你的效果不尽人意,请尝试在 Chrome 浏览器下预览。

装B技巧

本文所有图形都会有个容器 <div class="css-cell"></div>包裹,其样式结构如下:


    .css-cell{

    position: relative;

    width: 100%;

    height: 300px;

    }

所有图形都是在容器内实现的,其结构如下:


    <!--heart-->

    <div class="css-cell">

    <div class="heart"></div>

    </div>

天气那一块有部分会多一个容器,其结构如下:


    <div class="css-cell ">

    <div class="breeze-container">

    <div class="breeze"></div>

    </div>

    </div>

为了方便起见,下面图形的具体实现,我只会贴出对应的类相应的样式代码~

装逼实战

太阳


利用线性渐变、阴影、旋转实现,具体代码如下:


    /*sun*/

    .sun{

    position: absolute;

    top: 50%;

    left: 50%;

    width:200px;

    height:260px;

    transform: translate(-50%, -50%);

    background:#0BF;

    border-radius:5px;

    }

    .sun:before{

    content:"";

    position: absolute;

    width: 80px;

    height: 80px;

    left: 50%;

    top: 50%;

    transform: translate(-50%, -50%);

    border-radius:50%;

    background:rgba(255, 238, 68, 1);

    box-shadow: 0 0 0 15px rgba(255,255,0,0.2),0 0 15px #fff;

    z-index:-10;

    }

    .sun:after{

    content:"";

    position: absolute;

    top: 50%;

    left: 50%;

    height: 160px;

    width: 160px;

    transform: translate(-50%, -50%) rotate(30deg);

    z-index:-100;

    background-image:

    -webkit-linear-gradient(top,rgba(255,255,255,0) 0%, rgba(255,255,255,0.8) 50%, rgba(255,255,255,0) 100%),

    -webkit-linear-gradient(left,rgba(255,255,255,0) 0%, rgba(255,255,255,0.8) 50%, rgba(255,255,255,0) 100%);

    background-size: 20px 100%, 100% 20px;

    background-repeat: no-repeat;

    background-position: center center, center center;

    animation:sunRotate 10s linear infinite;

    }

    @keyframes sunRotate{

    0%{

    transform: translate(-50%, -50%) rotate(30deg);

    }

    100%{

    transform: translate(-50%, -50%) rotate(390deg);

    }

    }

多云


利用线性渐变、阴影、缩放实现,具体实现如下:


    /*cloudy*/

    .cloudy{

    position: absolute;

    top: 50%;

    left: 50%;

    width:200px;

    height:260px;

    transform: translate(-50%, -50%);

    background:#2EB5E5;

    border-radius:5px;

    }

    .cloudy:before {

    content: "";

    text-indent:23px;

    font-size:22px;

    line-height:40px;

    color:#333;

    position: absolute;

    height: 50px;width: 50px;

    background: #FFFFFF;

    left:30%;

    top:45%;

    transform: translate(-50%, -50%);

    border-radius: 50%;

    box-shadow:

    #FFFFFF 65px -15px 0 -5px,

    #FFFFFF 25px -25px,

    #FFFFFF 30px 10px,

    #FFFFFF 60px 15px 0 -10px,

    #FFFFFF 85px 5px 0 -5px,

    #C8C8C8 35px -35px,

    #C8C8C8 66px -27px 0 -5px,

    #C8C8C8 91px -10px 0 -8px;

    animation: cloudy 5s ease-in-out infinite;

    }

    .cloudy:after{

    content:"";

    position: absolute;

    top: 80%;

    left: 50%;

    height: 15px;

    width: 120px;

    background:rgba(0,0,0,.5);

    border-radius: 50%;

    transform: translate(-50%, -50%);

    animation: cloudy_shadow 5s ease-in-out infinite;

    }

    @keyframes cloudy {

    50%{

    transform: translate(-50%, -70%);

    }

    100%{

    transform: translate(-50%, -50%);

    }

    }

    @keyframes cloudy_shadow {

    50%{

    transform: translate(-50%, -50%) scale(0.8);

    background:rgba(0,0,0,.2);

    }

    100%{

    transform: translate(-50%, -50%) scale(1);

    background:rgba(0,0,0,.5);

    }

    }


    /*cloudy2*/

    .cloudy2{

    position: absolute;

    top: 50%;

    left: 50%;

    width:200px;

    height:260px;

    transform: translate(-50%, -50%);

    background:#2EB5E5;

    border-radius:5px;

    }

    .cloudy2:before {

    content: "";

    text-indent:23px;

    font-size:22px;

    line-height:40px;

    color:#333;

    position: absolute;

    height: 50px;width: 50px;

    background: #FFFFFF;

    left:30%;

    top:55%;

    transform: translate(-50%, -50%);

    border-radius: 50%;

    z-index:100;

    box-shadow:

    #FFFFFF 65px -15px 0 -5px,

    #FFFFFF 25px -25px,

    #FFFFFF 30px 10px,

    #FFFFFF 60px 15px 0 -10px,

    #FFFFFF 85px 5px 0 -5px;

    animation: cloudy2 5s ease-in-out infinite;

    }

    .cloudy2:after{

    content:"";

    position: absolute;

    top: 45%;left: 63%;

    height: 60px;

    width: 60px;

    z-index:10;

    background:linear-gradient(180deg,#FE9F38 0%, #F46635 100%);

    border-radius: 50%;

    transform: translate(-50%, -50%);

    box-shadow: 0 0 10px 4px #FFA563;

    animation: cloudy2 10s ease-in-out infinite;

    }

    @keyframes cloudy2 {

    50%{

    transform: translate(-50%, -70%);

    }

    100%{

    transform: translate(-50%, -50%);

    }

    }


利用线性渐变、阴影、缩放实现,具体代码如下:


    /*rainy*/

    .rainy {

    position: absolute;

    width: 3px;

    height: 6px;

    top: 30%;

    left: 50%;

    background: #CCCCCC;

    border-radius: 50%;

    animation: rainy_rain .7s infinite linear;

    }

    .rainy:before {

    content: "";

    color: #333;

    position: absolute;

    height: 50px;

    width: 50px;

    top: 30px;

    left: -40px;

    background: #CCC;

    transform: translate(-50%, -50%);

    border-radius: 50%;

    box-shadow: #CCC 65px -15px 0 -5px, #CCC 25px -25px, #CCC 30px 10px, #CCC 60px 15px 0 -10px, #CCC 85px 5px 0 -5px;

    animation: cloudy 5s ease-in-out infinite;

    }

    .rainy:after {

    content: "";

    position: absolute;

    top: 120px;

    left: 50%;

    height: 15px;

    width: 120px;

    background: rgba(0, 0, 0, .5);

    border-radius: 50%;

    transform: translate(-50%, -50%);

    animation: cloudy_shadow 5s ease-in-out infinite;

    }

    @keyframes cloudy {

    50% {

    transform: translate(-50%, -70%);

    }

    100% {

    transform: translate(-50%, -50%);

    }

    }

    @keyframes cloudy_shadow {

    50% {

    transform: translate(-50%, -50%) scale(0.8);

    background: rgba(0, 0, 0, .2);

    }

    100% {

    transform: translate(-50%, -50%) scale(1);

    background: rgba(0, 0, 0, .5);

    }

    }

    @keyframes rainy_rain {

    0% {

    box-shadow: rgba(0, 0, 0, 0) -10px 30px, rgba(0, 0, 0, 0) 40px 40px, rgba(0, 0, 0, .3) -50px 75px, rgba(0, 0, 0, .3) 55px 50px, rgba(0, 0, 0, .3) -18px 100px, rgba(0, 0, 0, .3) 12px 95px, rgba(0, 0, 0, .3) -31px 45px, rgba(0, 0, 0, .3) 30px 35px;

    }

    25% {

    box-shadow: rgba(0, 0, 0, .3) -10px 45px, rgba(0, 0, 0, .3) 40px 60px, rgba(0, 0, 0, .3) -50px 90px, rgba(0, 0, 0, .3) 55px 65px, rgba(0, 0, 0, 0) -18px 120px, rgba(0, 0, 0, 0) 12px 120px, rgba(0, 0, 0, .3) -31px 70px, rgba(0, 0, 0, .3) 30px 60px;

    }

    26% {

    box-shadow: rgba(0, 0, 0, .3) -10px 45px, rgba(0, 0, 0, .3) 40px 60px, rgba(0, 0, 0, .3) -50px 90px, rgba(0, 0, 0, .3) 55px 65px, rgba(0, 0, 0, 0) -18px 40px, rgba(0, 0, 0, 0) 12px 20px, rgba(0, 0, 0, .3) -31px 70px, rgba(0, 0, 0, .3) 30px 60px;

    }

    50% {

    box-shadow: rgba(0, 0, 0, .3) -10px 70px, rgba(0, 0, 0, .3) 40px 80px, rgba(0, 0, 0, 0) -50px 100px, rgba(0, 0, 0, .3) 55px 80px, rgba(0, 0, 0, .3) -18px 60px, rgba(0, 0, 0, .3) 12px 45px, rgba(0, 0, 0, .3) -31px 95px, rgba(0, 0, 0, .3) 30px 85px;

    }

    51% {

    box-shadow: rgba(0, 0, 0, .3) -10px 70px, rgba(0, 0, 0, .3) 40px 80px, rgba(0, 0, 0, 0) -50px 45px, rgba(0, 0, 0, .3) 55px 80px, rgba(0, 0, 0, .3) -18px 60px, rgba(0, 0, 0, .3) 12px 45px, rgba(0, 0, 0, .3) -31px 95px, rgba(0, 0, 0, .3) 30px 85px;

    }

    75% {

    box-shadow: rgba(0, 0, 0, .3) -10px 95px, rgba(0, 0, 0, .3) 40px 100px, rgba(0, 0, 0, .3) -50px 60px, rgba(0, 0, 0, 0) 55px 95px, rgba(0, 0, 0, .3) -18px 80px, rgba(0, 0, 0, .3) 12px 70px, rgba(0, 0, 0, 0) -31px 120px, rgba(0, 0, 0, 0) 30px 110px;

    }

    76% {

    box-shadow: rgba(0, 0, 0, .3) -10px 95px, rgba(0, 0, 0, .3) 40px 100px, rgba(0, 0, 0, .3) -50px 60px, rgba(0, 0, 0, 0) 55px 35px, rgba(0, 0, 0, .3) -18px 80px, rgba(0, 0, 0, .3) 12px 70px, rgba(0, 0, 0, 0) -31px 25px, rgba(0, 0, 0, 0) 30px 15px;

    }

    100% {

    box-shadow: rgba(0, 0, 0, 0) -10px 120px, rgba(0, 0, 0, 0) 40px 120px, rgba(0, 0, 0, .3) -50px 75px, rgba(0, 0, 0, .3) 55px 50px, rgba(0, 0, 0, .3) -18px 100px, rgba(0, 0, 0, .3) 12px 95px, rgba(0, 0, 0, .3) -31px 45px, rgba(0, 0, 0, .3) 30px 35px;

    }

    }

微风


利用border、transparent、实现,这个会多一层 div.breeze-container 包裹,样式代码如下:


    /*breeze*/

    .breeze-container{

    position: absolute;

    top: 50%;left: 50%;

    width:200px;height:260px;

    transform: translate(-50%, -50%);

    text-align:center;

    font-size:200%;

    color:#fff;

    background:#00BBFF;

    border-radius:5px;

    }

    .breeze-container:after{

    content:"";

    position:absolute;

    top:58%;

    left:50%;

    transform: translate(-50%, -50%);

    width:6px;

    height:70px;

    background:#fff;

    }

    .breeze{

    position:absolute;

    top: 30%;

    left: 50%;

    transform: translate(-50%, -50%) rotate(1deg);

    border-bottom:60px solid #fff;

    border-left:5px solid transparent;

    border-right:5px solid transparent;

    animation: windmill 12s infinite linear;

    transform-origin:50.5% 62px;

    }

    .breeze:before{

    position:absolute;

    top: 75px;left: -59px;

    content:"";

    border-right:60px solid #fff;

    border-top:5px solid transparent;

    border-bottom:5px solid transparent;

    transform:rotate(-30deg);

    }

    .breeze:after{

    position:absolute;

    top: 75px;left: -1px;

    content:"";

    border-left:60px solid #fff;

    border-top:5px solid transparent;

    border-bottom:5px solid transparent;

    transform:rotate(30deg);

    }

    @keyframes windmill{

    0%{

    transform: translate(-50%, -50%) rotate(0deg);

    }

    100%{

    transform: translate(-50%, -50%) rotate(360deg);

    }

    }

彩虹


主要是利用border、box-shadow 实现,具体实现如下:


    /*rainbow*/

    .rainbow-container{

    position: absolute;

    top: 50%;

    left: 50%;

    width:200px;

    height:260px;

    transform: translate(-50%, -50%);

    background:#F3D166;

    border-radius:5px;

    }

    .rainbow{

    position:absolute;

    top: 50%;

    left: 50%;

    transform: translate(-50%, -50%);

    height: 1px;width: 1px;

    }

    .rainbow:before{

    content:"";

    position:absolute;

    top: 50%;left: 50%;

    transform: translate(-50%, -50%) rotate(45deg);

    height: 70px;width: 70px;

    border-radius: 100px 0 0 0;

    box-shadow:

    #F44336 -2px -2px 0 1px,

    #FF9800 -4px -4px 0 3px,

    #FFEB3B -6px -6px 0 5px,

    #8BC34A -8px -8px 0 7px,

    #00BCD4 -10px -10px 0 9px,

    #2196F3 -12px -12px 0 11px,

    #9C27B0 -14px -14px 0 13px;

    animation: rainbow 5s ease-in-out infinite;

    }

    .rainbow:after{

    content: "";

    position: absolute;

    top: 70px;

    left: 50%;

    height: 15px;

    width: 120px;

    background: rgba(0, 0, 0, .5);

    border-radius: 50%;

    transform: translate(-50%, -50%);

    animation: cloudy_shadow 5s ease-in-out infinite;

    }

    @keyframes rainbow {

    50% {

    transform: translate(-50%, -55%) rotate(30deg);

    }

    100% {

    transform: translate(-50%, -50%) rotate(45deg);

    }

    }

    @keyframes cloudy_shadow {

    50% {

    transform: translate(-50%, -50%) scale(0.8);

    background: rgba(0, 0, 0, .2);

    }

    100% {

    transform: translate(-50%, -50%) scale(1);

    background: rgba(0, 0, 0, .5);

    }

    }

夜空


主要是利用 box-shadow 实现 , 实现方式如下:


    /*starry*/

    .starry-container{

    position: absolute;

    top: 50%;

    left: 50%;

    width:200px;

    height:260px;

    transform: translate(-50%, -50%);

    background:#222233;

    border-radius:5px;

    }

    .starry{

    position:absolute;

    top: 30%;left: 40%;

    transform: translate(-50%, -50%);

    height: 4px;width: 4px;

    border-radius:50%;

    box-shadow:

    #FFFFFF -26px 77px 0 -1px,

    rgba(255,255,255,0.1) -36px 59px 0 -1px,

    rgba(255,255,255,0.1) -28px 89px 0 -1px,

    #FFFFFF -35px 20px 0 -1px,

    #FFFFFF 14px 100px,

    rgba(255,255,255,0.1) 41px 60px,

    #FFFFFF 34px 39px,

    rgba(255,255,255,0.1) 14px 45px 0 -1px,

    #FFFFFF 64px 12px 0 -1px,

    rgba(255,255,255,0.1) 32px 96px 0 -1px,

    #FFFFFF 64px 71px,

    rgba(255,255,255,0.1) 60px 18px 0 -1px,

    #FFFFFF 34px 9px,

    rgba(255,255,255,0.1) -26px 55px 0 -1px;

    animation: starry_star 5s ease-in-out infinite;

    }

    .starry:before{

    content:"";

    position:absolute;

    top: 20%;left: 50%;

    width:100px;height:100px;

    box-shadow: #FFFFFF -25px 0;

    transform: rotate(-5deg);

    border-radius: 50%;

    animation: starry 5s ease-in-out infinite;

    }

    @keyframes starry {

    50% {

    transform: rotate(10deg);

    }

    }

    @keyframes starry_star{

    50%{

    box-shadow:

    rgba(255,255,255,0.1) -26px 77px 0 -1px,

    #FFF -36px 59px 0 -1px,

    #FFF -28px 89px 0 -1px,

    rgba(255,255,255,0.1) -35px 20px 0 -1px,

    rgba(255,255,255,0.1) 14px 100px,

    #FFF 41px 60px,

    rgba(255,255,255,0.1) 34px 39px,

    #FFF 14px 45px 0 -1px,

    rgba(255,255,255,0.1) 64px 12px 0 -1px,

    #FFF 32px 96px 0 -1px,

    rgba(255,255,255,0.1) 64px 71px,

    #FFF 60px 18px 0 -1px,

    rgba(255,255,255,0.1) 34px 9px,

    #FFF -26px 55px 0 -1px;

    }

    }

雷电


主要是利用阴影、border实现,代码如下:


    /*thunder*/

    .thunder-container {

    position: absolute;

    top: 50%;

    left: 50%;

    width: 200px;

    height: 260px;

    transform: translate(-50%, -50%);

    background: #444;

    border-radius: 5px;

    }

    .thunder {

    color: #333;

    position: absolute;

    height: 50px;

    width: 50px;

    top: 40%;

    left: 30%;

    background: #222;

    transform: translate(-50%, -50%);

    border-radius: 50%;

    box-shadow:

    #222 65px -15px 0 -5px,

    #222 25px -25px,

    #222 30px 10px,

    #222 60px 15px 0 -10px,

    #222 85px 5px 0 -5px;

    animation: cloudy 5s ease-in-out infinite;

    }

    .thunder:before {

    content: "";

    position: absolute;

    top: 60px;

    left: 60px;

    border-left: 0px solid transparent;

    border-right: 8px solid transparent;

    border-top: 38px solid yellow;

    box-shadow: yellow -7px -32px;

    transform:rotate(30deg);

    transform-origin:center -50px;

    animation:stormy_thunder 2s steps(1, end) infinite;;

    }

    .thunder:after {

    content: "";

    position: absolute;

    top: 120px;

    left: 64px;

    height: 15px;

    width: 120px;

    background: rgba(0, 0, 0, .5);

    border-radius: 50%;

    z-index:-1;

    transform: translate(-50%, -50%);

    animation: cloudy_shadow 5s ease-in-out infinite;

    }

    @keyframes cloudy {

    50% {

    transform: translate(-50%, -30px);

    }

    }

    @keyframes cloudy_shadow {

    50% {

    transform: translate(-50%, 0) scale(0.8);

    background: rgba(0, 0, 0, .2);

    }

    }

    @keyframes stormy_thunder{

    0% { transform: rotate(30deg); opacity:1; }

    5% { transform: rotate(-34deg); opacity:1; }

    10% { transform: rotate(0deg); opacity:1; }

    15% { transform: rotate(-34deg); opacity:0; }

    }

大雪


利用阴影实现 , 代码如下:


    /*snow*/

    .snowy-container {

    position: absolute;

    top: 50%;

    left: 50%;

    width: 200px;

    height: 260px;

    transform: translate(-50%, -50%);

    text-align: center;

    font-size: 200%;

    color: #fff;

    background: #607D8B;

    border-radius: 5px;

    }

    .snowy {

    position: absolute;

    width: 4px;

    height: 4px;

    border-radius:50%;

    top: 30%;

    left: 50%;

    background: #fff;

    border-radius: 50%;

    animation: snowy_rain 2s infinite linear;

    }

    .snowy:before {

    content: "";

    color: #333;

    position: absolute;

    height: 50px;

    width: 50px;

    top: 30px;

    left: -40px;

    background: #eee;

    transform: translate(-50%, -50%);

    border-radius: 50%;

    box-shadow:

    #eee 65px -15px 0 -5px,

    #eee 25px -25px,

    #eee 30px 10px,

    #eee 60px 15px 0 -10px,

    #eee 85px 5px 0 -5px;

    animation: cloudy 5s ease-in-out infinite;

    }

    .snowy:after {

    content: "";

    position: absolute;

    top: 120px;

    left: 50%;

    height: 15px;

    width: 120px;

    background: rgba(0, 0, 0, .5);

    border-radius: 50%;

    transform: translate(-50%, -50%);

    animation: cloudy_shadow 5s ease-in-out infinite;

    }

    @keyframes cloudy {

    50% {

    transform: translate(-50%, -70%);

    }

    100% {

    transform: translate(-50%, -50%);

    }

    }

    @keyframes cloudy_shadow {

    50% {

    transform: translate(-50%, -50%) scale(0.8);

    background: rgba(0, 0, 0, .2);

    }

    100% {

    transform: translate(-50%, -50%) scale(1);

    background: rgba(0, 0, 0, .5);

    }

    }

    @keyframes snowy_rain {

    0% {

    box-shadow:

    rgba(255, 255, 255, 0) -10px 30px,

    rgba(255, 255, 255, 0) 40px 40px,

    rgba(255, 255, 255, .6) -50px 75px,

    rgba(255, 255, 255, .6) 55px 50px,

    rgba(255, 255, 255, .6) -18px 100px,

    rgba(255, 255, 255, .6) 12px 95px,

    rgba(255, 255, 255, .6) -31px 45px,

    rgba(255, 255, 255, .6) 30px 35px;

    }

    25% {

    box-shadow:

    rgba(255, 255, 255, .6) -10px 45px,

    rgba(255, 255, 255, .6) 40px 60px,

    rgba(255, 255, 255, .6) -50px 90px,

    rgba(255, 255, 255, .6) 55px 65px,

    rgba(255, 255, 255, 0) -18px 120px,

    rgba(255, 255, 255, 0) 12px 120px,

    rgba(255, 255, 255, .6) -31px 70px,

    rgba(255, 255, 255, .6) 30px 60px;

    }

    26% {

    box-shadow:

    rgba(255, 255, 255, .6) -10px 45px,

    rgba(255, 255, 255, .6) 40px 60px,

    rgba(255, 255, 255, .6) -50px 90px,

    rgba(255, 255, 255, .6) 55px 65px,

    rgba(255, 255, 255, 0) -18px 40px,

    rgba(255, 255, 255, 0) 12px 20px,

    rgba(255, 255, 255, .6) -31px 70px,

    rgba(255, 255, 255, .6) 30px 60px;

    }

    50% {

    box-shadow:

    rgba(255, 255, 255, .6) -10px 70px,

    rgba(255, 255, 255, .6) 40px 80px,

    rgba(255, 255, 255, 0) -50px 100px,

    rgba(255, 255, 255, .6) 55px 80px,

    rgba(255, 255, 255, .6) -18px 60px,

    rgba(255, 255, 255, .6) 12px 45px,

    rgba(255, 255, 255, .6) -31px 95px,

    rgba(255, 255, 255, .6) 30px 85px;

    }

    51% {

    box-shadow:

    rgba(255, 255, 255, .6) -10px 70px,

    rgba(255, 255, 255, .6) 40px 80px,

    rgba(255, 255, 255, 0) -50px 45px,

    rgba(255, 255, 255, .6) 55px 80px,

    rgba(255, 255, 255, .6) -18px 60px,

    rgba(255, 255, 255, .6) 12px 45px,

    rgba(255, 255, 255, .6) -31px 95px,

    rgba(255, 255, 255, .6) 30px 85px;

    }

    75% {

    box-shadow:

    rgba(255, 255, 255, .6) -10px 95px,

    rgba(255, 255, 255, .6) 40px 100px,

    rgba(255, 255, 255, .6) -50px 60px,

    rgba(255, 255, 255, 0) 55px 95px,

    rgba(255, 255, 255, .6) -18px 80px,

    rgba(255, 255, 255, .6) 12px 70px,

    rgba(255, 255, 255, 0) -31px 120px,

    rgba(255, 255, 255, 0) 30px 110px;

    }

    76% {

    box-shadow:

    rgba(255, 255, 255, .6) -10px 95px,

    rgba(255, 255, 255, .6) 40px 100px,

    rgba(255, 255, 255, .6) -50px 60px,

    rgba(255, 255, 255, 0) 55px 35px,

    rgba(255, 255, 255, .6) -18px 80px,

    rgba(255, 255, 255, .6) 12px 70px,

    rgba(255, 255, 255, 0) -31px 25px,

    rgba(255, 255, 255, 0) 30px 15px;

    }

    100% {

    box-shadow:

    rgba(255, 255, 255, 0) -10px 120px,

    rgba(255, 255, 255, 0) 40px 120px,

    rgba(255, 255, 255, .6) -50px 75px,

    rgba(255, 255, 255, .6) 55px 50px,

    rgba(255, 255, 255, .6) -18px 100px,

    rgba(255, 255, 255, .6) 12px 95px,

    rgba(255, 255, 255, .6) -31px 45px,

    rgba(255, 255, 255, .6) 30px 35px;

    }

    }

装逼总结

怎么样,是不是颠覆了你对 CSS3 的认识?

实际上CSS3 带给我们的远不止这些东西,没有做不到,只有想不到,只有你脑洞够大,各种黑科技、酷炫的 CSS必然也是手到擒来~

虽然你给不了CSS3 全部,但它却把全部给了你,骚年,加油吧~

PS: 本文由Chris威在掘金上发布,进行了部分节选。

缩写CSS样式的好处:

对于浏览者而言,缩写可以减少CSS代码量,进而减少CSS文件体积,有利于访问者更快的打开网页;对于开发者而言,CSS代码量少,阅读简洁明了;对于网站服务器而言,在相同带宽下可以承受更多的访问请求。

常用CSS样式表缩写语法总结

盒尺寸(padding、margin)
常见margin与padding边距缩写margin外边距与padding内边距是制作布局时常用到的两个属性,在传统写法上,我们通常使用以下形式:

.box{
   width:300px;
   height: 300px;
   background: #eee;
   margin-left: 20px;
   margin-right: 30px;
   margin-top: 40px;
   margin-bottom: 50px;
}

padding也是如此,而在css缩写中,可以使用缩写的编写方式,margin及padding的缩写在默认状态下需要提供4个参数,按顺序分别为上、右、下、左。例:

.box{
   width:300px;
   height: 300px;
   background: #eee;
   /*外边距连写*/
   margin:20px 30px 40px 50px;
}

注意:margin及padding的缩写比较特殊一点,方便的记忆方法是顺时针,上右下左。
通常有下面四种书写方法(这里拿margin做例子):

/*顺序*/
/*从上开始,顺时针转;margin:上 右 下 左*/
/*上下左右都相等: margin: 5px(上下左右)*/
margin: 5px;
/*上下相等, 左右相等: margin: 5px(上下) 15px(左右)*/
margin: 5px 15px 5px 15px;
/*上下不等,左右相等: margin: 5px(上) 10px(左右) 20px(下)*/
margin: 5px 10px 20px 10px
/*上下左右都不相等: margin: 20px(上)   30px(右)   40px(下)  50px(左)*/
Margin: 20px  30px   40px  50px;


边框(border)
border边框缩写border对象本身是一个非常复杂的对象,包含了四条边的不同宽度、不同颜色及不同样式,所以border对象提供的缩写形式相对来说也要丰富很多,不仅可以对整个对象进行border缩写,也可以对单独某一个边进行缩写,
对于整个对象而言,可以使用如下的格式进行缩写:

border:border-width border-style border-color 

代码示例:

.box{border:5px solid red;}

通过上面的属性,我们就可以把样式针对整个对象或者每一个边都设置不同的样式


.box{
   width: 300px;
   height: 390px;
   background: #999;
   border-top-style: solid;   /*线型*/
   border-top-color: #ff0000;     /*边框颜色*/
   border-top-width: 5px;   /*宽度*/ 
   /*边框属性连写*/
   border-top: #ff0000 solid 5px;   
   /*四个边框值相同的写法*/
   border:12px solid red;    
   /*单独边框并且设置不同的样式*/ 
   border-top: red solid 5px; 
   border-right: 12px dotted #ffc0cb;
   border-buttom: 12px dotted #191970;
   border-left: 5px dashed #00ffff;
}

注意:border缩写样式没有顺序要求,但线型(border-style)为必写项。
除了对于边框整体及4个边单独的缩写之外,border还为我们提供了对于
border-style、border-width以及border-color的单独缩写方式,语法格式如下:

border-width:top right bottom left;
border-color:top right bottom left; 
border-style:top right bottom left; 

代码示例:

.box{
   border-width:1px 2px 3px 4px;
   border-color:blue white red green; 
   border-style:solid dashed; 
}


背景(Background)
背景的属性如下:
background-color:#f00;
background-image:url(background.gif);
background-repeat:no-repeat;
background-attachment:fixed;
background-position:0 0;
背景缩写用于针对背景控制的相关属性进行缩写,其语法格式如下:
background:background-color background-image background-repeat background-attachment background-position
根据以上语法,可以缩写为一句:

.box{background:#f00 url(background.gif) no-repeat fixed 0 0;} 

注意:background属性连写的时候没有顺序要求,但url为必写项。
color颜色缩写css对于颜色代码也提供了简单的缩写模式,主要针对16进制颜色代码。
16进制代码传统写法一般使用#ABCDEF,ABCDEF分别代表6个 16进制数,css的颜色缩写必须符合一定要求,
当A与B数字相同,C与D数字相同,E与F数字相同时,可以使用颜色缩写,
例:


p{color:#ffffff;}
/* 可以缩写为*/
p{color:#fff;}
p{color:#2255bb;}
/* 可以缩写为*/
p{color:#25b;} 


字体(fonts)
字体的属性如下:
font-style:italic;
font-variant:small-caps;
font-weight:bold;
font-size:1em;
line-height:140%;
font-family:"Lucida Grande",sans-serif;
font字体缩写字体缩写是针对字体样式的缩写形式,包含字体、字号等属性,使用方法如下:

font:font-style font-variant font-weight font-size line-height font-family 

根据以上语法,可以缩写为一句:

font:italic small-caps bold 1em/140% "Lucida Grande",sans-serif;

注意:font:后边写属性的值。一定按照书写顺序。文本属性连写文字大小(font-size)和字体(font-family)为必写项。
列表(lists)
取消默认的圆点和序号可以这样写list-style:none;,
list的属性如下:
list-style-type:circle;
list-style-position:inside;
list-style-image:url(image.gif);
list列表缩写list缩写是针对list-style-type、list-style-position等用于ul的list属性,
语法格式如下:

list-style:list-style-type list-style-position list-style-image 

代码示例:
根据以上语法,可以缩写为一句

ul {list-style:disc outside none;} 


这里顺便提一句,有很多样式都涉及到了四边的问题,这里统一说明。
四边的简写一般如下:selector {property: value}
value:top | right | bottom | left

具体可以参考上面的marginborder属性!(←这里的marginborder属性可以点击噢(〃'▽'〃))
最后,偷偷告诉大家,学好css最需求的就是多看多写多实践,
那样才能学的好,记得住。٩(๑❛︶❛๑)۶