1. 程式人生 > >Web從入門到放棄<8>

Web從入門到放棄<8>

div round lap mage inf .com line gpo 布局

Ref:

Cameron D. - HTML5, JavaScript and jQuery (Programmer to Programmer) - 2015

<1> CSS Responsive box

技術分享圖片

關鍵字:display:inline-block;

html:

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        .box {
            height:200px;
            width
:200px; display:inline-block; } </style> </head> <body> <div class="box" style="background:red"></div> <div id="middleBox" class="box" style="background:green"></div> <div id="thirdBox" class="box" style="background:blue"></div> <
div id="lastBox" class="box" style="background:yellow"></div> </body> </html>
View Code

如果要把綠色的立方體移動50px,把藍色向右推動50px,在這種靜態布局是不可能的.先試試position:relative.

技術分享圖片

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        .box {
            height
:200px; width:200px; display:inline-block; } #middleBox{ position: relative; left:50px; } </style> </head> <body> <div class="box" style="background:red"></div> <div id="middleBox" class="box" style="background:green"></div> <div id="thirdBox" class="box" style="background:blue"></div> <div id="lastBox" class="box" style="background:yellow"></div> </body> </html>
View Code

position設置為relative,意思就在現在的位置作為基礎,然後再做移動。

position設置為absolute:

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        .box {
            height:200px;
            width:200px;
            display:inline-block;
        }
        #middleBox{
            position: absolute;
            left:150px;
            top:150px;
        }

    </style>
</head>
<body>
<div class="box" style="background:red"></div>
<div id="middleBox" class="box" style="background:green"></div>
<div id="thirdBox" class="box" style="background:blue"></div>
<div id="fourthBox" class="box" style="background:yellow"></div>
<div id="lastBox" class="box" style="background:black"></div>
</body>
</html>
View Code

技術分享圖片

要把綠色放入下面寫:z-index:-1;

Web從入門到放棄<8>