123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!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>
            
          
!
            
              /* 从此例中不难看出,当父元素为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);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console