ITPub博客

首页 > 应用开发 > Html/css > 几个超级实用的css代码片段

几个超级实用的css代码片段

原创 Html/css 作者:freebus 时间:2020-03-15 11:07:03 0 删除 编辑

1、伸展一个元素到窗口高度

  在具体场景中,你可能想要将一个元素伸展到窗口高度,基本元素的调整只能调整容器的大小,因此要使一个元素伸展到窗口高度,我们需要伸展顶层元素:html和body:


html, 

body {

    height: 100%;

}

  然后将100%应用到任何元素的高:


div {

    height: 100%;

}

2、只在一边或两边显示盒子阴影

  如果你要一个盒阴影,试试这个技巧,能为任一边添加阴影。为了实现这个,首先定义一个有具体宽高的盒子,然后正确定位:after伪类。实现底边阴影的代码如下:


.box-shadow {

    background-color: #FF8020;

    width: 160px;

    height: 90px;

    margin-top: -45px;

    margin-left: -80px;

    position: absolute;

    top: 50%;

    left: 50%;

}

.box-shadow:after {

    content: "";

    width: 150px;

    height: 1px;

    margin-top: 88px;

    margin-left: -75px;

    display: block;

    position: absolute;

    left: 50%;

    z-index: -1;

    -webkit-box-shadow: 0px 0px 8px 2px #000000;

       -moz-box-shadow: 0px 0px 8px 2px #000000;

            box-shadow: 0px 0px 8px 2px #000000;

}

3、制造模糊文本

  想要让文本模糊?可以使用color透明和text-shadow实现。


.blurry-text {

   color: transparent;

   text-shadow: 0 0 5px rgba(0,0,0,0.5);

}

3、新版清除浮动(2011)

.clearfix:before, .container:after { content: ""; display: table; }

.clearfix:after { clear: both; }


/* IE 6/7 */

.clearfix { zoom: 1; }

4、通用媒体查询

/* Smartphones (portrait and landscape) ----------- */

@media only screen 

and (min-device-width : 320px) and (max-device-width : 480px) {

  /* Styles */

}


/* Smartphones (landscape) ----------- */

@media only screen and (min-width : 321px) {

  /* Styles */

}


/* Smartphones (portrait) ----------- */

@media only screen and (max-width : 320px) {

  /* Styles */

}


/* iPads (portrait and landscape) ----------- */

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {

  /* Styles */

}


/* iPads (landscape) ----------- */

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {

  /* Styles */

}


/* iPads (portrait) ----------- */

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {

  /* Styles */

}


/* Desktops and laptops ----------- */

@media only screen and (min-width : 1224px) {

  /* Styles */

}


/* Large screens ----------- */

@media only screen and (min-width : 1824px) {

  /* Styles */

}


/* iPhone 4 ----------- */

@media only screen and (-webkit-min-device-pixel-ratio:1.5), only screen and (min-device-pixel-ratio:1.5) {

  /* Styles */

}

5、强制出现垂直滚动条

html { height: 101% }

6、CSS3 斑马线

tbody tr:nth-child(odd) {

    background-color: #ccc;

}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31365439/viewspace-2680387/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-06-22

  • 博文量
    34
  • 访问量
    19637