天天看點

CSS中div覆寫另一個div

将一個div覆寫在另一個div上有兩種手段:一是設定margin為負值,二是設定絕對定位。

可以根個人情況設定z-index的值

1->position 為absolute的情況

<html>

<head>

<style>

#div1{position:absolute;width:300px;height:300px;background:#ccc;}

#div2{position:absolute;left:0;top:0;width:200px;height:200px;background:red;filter:alpha(opacity=50);}

</style>

</head>

<body>

<divid="div1">這裡是div1的内容

          <divid="div2"></div>

</div>

</body>

</html>

2->用margin為負的操作

    <style>

    #div1 { position:relative; width:300px; height:300px; background:#ccc;}

    #div2 { position:relative; left:0; top:0;margin-top:-15px; width:200px; height:200px; background:red;filter:alpha(opacity=50);}

    </style>

<divid="div1"> 這裡是div1的内容