Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

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

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <a>说明:黑色块为父元素,灰白色块为子元素。</a><p>
    <!-- 父元素static,子元素absolute -->
    <div id="a1">
      <div id="a2"><a>static+absolute 参照body</a></div>
    </div>
    <p>
    <!-- 父子元素均为absolute -->
        <div id="b1">
      <div id="b2"><a>absolute x2 参照父级</a></div>
    </div>
    <p>
    <!-- 父子元素均为relative -->
        <div id="c1">
      <div id="c2"><a>relative x2 参照父级</a></div>
    </div>
    <p>
    <!-- 父子元素均为fixed -->
        <div id="d1">
      <div id="d2"><a>fixed x2 参照body</a></div>
    </div>
    <p>
  </body> 
 </html>
              
            
!

CSS

              
                /* 从此例中不难看出,当父元素为static定位,子元素为absolute定位时,子元素的相对宽度属性实质上是相对窗口的宽度 */
/* 为了能直观地展示,先把body背景设为蓝色 */
body {
  margin: 0;
  padding: 0;
  background: #ff96ad;
  overflow: hidden;
}
a{
  color: white;
  font-size: 0.8em;
}

/* 父元素div#a1的定位方式设置为static(该关键字指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时 top, right, bottom, left 和 z-index 属性无效。——Mozilla文档,实际上由于position属性的默认值是static,无需明确写出position: static;) */
#a1 {
  position: static;/* 可省略 */
  height: 30px;
  width: 200px;
  background: rgb(0,0,0);
}

/* 子元素div#a2的定位方式设置为absolute,宽度设为相对?的80% */
#a2 {
  position: absolute;
  height:20px;
  width: 80%;/* 这里到底是相对谁的宽度呢 */
  background: rgba(255,255,255,.3);
}

/* ____________________ */

#b1 {
  position: absolute;
  height: 30px;
  width: 200px;
  background: rgb(0,0,0);
}

#b2 {
  position: absolute;
  height:20px;
  width: 80%;/* 这里到底是相对谁的宽度呢 */
  background: rgba(255,255,255,.3);
}

/* ____________________ */

#c1 {
  top : 50px;/* 由于relative定位的特性,添加这一句把它定位到上一组div的下方*/
  position: relative;
  height: 30px;
  width: 200px;
  background: rgb(0,0,0);
}

#c2 {
  position: relative;
  height:20px;
  width: 80%;/* 这里到底是相对谁的宽度呢 */
  background: rgba(255,255,255,.3);
}

/* ____________________ */

#d1 {
  bottom: 0;/* 由于fixed定位的特性,添加这一句把它定位到窗口底部*/
  position: fixed;
  height: 30px;
  width: 200px;
  background: rgb(0,0,0);
}

#d2 {
  position: fixed;
  height:20px;
  width: 80%;/* 这里到底是相对谁的宽度呢 */
  background: rgba(255,255,255,.3);
}
              
            
!

JS

              
                
              
            
!
999px

Console