Z-index and Background

A Pen By Dai

Pen Settings

↑ Insert the most common viewport meta tag

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource
via CSS Lint

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource
via JS Hint

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Want a Run Button?

If active, the preview will update automatically when you change code.


When negative Z-index encounter with html and body tag's background

Try uncomment z-index: 0 for div.cover,
the shadow which has z-index:-1 declaration will be override by body tag.

<html> tag's background has infinitesimal z-index value,
<body> tag's background's z-index value may be default 0
<div class="cover">
  <div class="poster" style="background-image: url(http://img5.douban.com/view/photo/photo/public/p1997952346.jpg)">


              @mixin shadow() {
  position: relative;
  z-index: 0; // uncomment this line to see what happens
  &:before, &:after {
    z-index: -2;
    content: "";
    position: absolute;
    width: 50%;
    height: 10px;
    bottom: 5px;
    background: #000;
    box-shadow: 0 10px 10px #000;
  &:before {
    left: 10px;
    -webkit-transform: rotate(-3deg);
    transform: rotate(-3deg);
  &:after {
    right: 10px;
    -webkit-transform: rotate(3deg);
    transform: rotate(3deg);

html {
  background: aliceblue;
body {
  background: url(http://img5.douban.com/view/photo/large/public/p2005237016.jpg) no-repeat center;
  background-size: cover;
  width: 600px;
  height: 600px;
  margin: 0 auto;
.cover {
  width: 200px;
  padding: 10px;
  border: 1px solid #ccc;
  background: #fff;
  @include shadow();
  .poster {
    width: 100%;
    height: 300px;
    background-size: 200px;
    &:before {
      content: "";
      background: #fff;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: -1;


Loading ..................