簡體   English   中英

CSS定位,需要一些建議

[英]CSS positioning, need some advice

這是我的HTML代碼

<div id="container">
        <div id="topleft"></div>
        <div id="topright"></div>
        <div id="bottomleft"></div>
        <div id="bottomright"></div>
    </div>

這是我的css代碼

#container{
    width: 400px;
    height: 400px;
    background-color: red;
    margin: 0 auto;

}

#topright{
    width: 50px;
    height: 50px;
    background-color: black;
    position: relative;
    top:0px;
    right:0px;
}
#topleft{
    width: 50px;
    height: 50px;
    background-color: black;
    position:relative;
    top:0px;
    left:350px;
}

#bottomright{
    width: 50px;
    height: 50px;
    background-color: black;
    position: relative;
    top:250px;
    right:0px;
}
#bottomleft{
    width: 50px;
    height: 50px;
    background-color: black;
    position:relative;
    top:250px;
    left:350px;
}

這是輸出http://s23.postimg.org/dhgy9mpq3/image.png

我需要獲得的是,所有4個黑色方塊都位於紅色方塊的角落,就像在這張圖片中一樣,我應該更改或添加代碼? THX http://postimg.org/image/r5kv15l5v/

添加position:relative對於#containerposition:absolute到內部div。

您可以將常用屬性與逗號結合使用,例如

#bottomright,#bottomleft{css properties}

CSS:

#container {
    position: relative;
}
#container > div {
    width: 50px;
    height: 50px;
    background-color: black;
    position: absolute;
}
#bottomright, #bottomleft {
    bottom:0;
}
#topright, #topleft {
    top:0;
}
#bottomleft, #topleft {
    left:0;
}
#bottomright, #topright {
    right:0;
}

DEMO在這里。

為什么你的css失敗:對於底部對齊,使用bottom:0;left:0; 而不是topright ....這是正確的語義,否則失敗的概念position ...同時,讓孩子absolute和父母relative :)

 #container {
        width: 400px;
        height: 400px;
        background-color: red;
        margin: 0 auto;
        position: relative;
    }
    #topright {
        width: 50px;
        height: 50px;
        background-color: black;
        position: absolute;
        top:0px;
        right:0px;
    }
    #topleft {
        width: 50px;
        height: 50px;
        background-color: black;
        position:absolute;
        top:0px;
        left:0;
    }
    #bottomright {
        width: 50px;
        height: 50px;
        background-color: black;
        position: absolute;
        bottom:0;
        right:0px;
    }
    #bottomleft {
        width: 50px;
        height: 50px;
        background-color: black;
        position:absolute;
        bottom:0;
        left:0;
    }

使#container位置relative 而div里面那absolute

完整代碼

#container{
    width: 400px;
    height: 400px;
    background-color: red;
    margin: 0 auto;
    position: relative; //change

}

#topright{    
    width: 50px;
    height: 50px;
    background-color: black;
    position: absolute; //change
    top:0px;
    right:0px;
}
#topleft{
    width: 50px;
    height: 50px;
    background-color: black;
    position:absolute; //change
    top:0px;
    left:0px;  //change
}

#bottomright{
    width: 50px;
    height: 50px;
    background-color: black;
    position: absolute;
    bottom:0px; //change
    right:0px;
}
#bottomleft{
    width: 50px;
    height: 50px;
    background-color: black;
    position:absolute; //change
    bottom:0px; //change
    left:0px; //change
}

演示

#container{
    width: 400px;
    height: 400px;
    background-color: red;
    margin: 0 auto;
    position: relative;

}

#topright{    
    width: 50px;
    height: 50px;
    background-color: black;
    position: absolute;
    top:0px;
    right:0px;
}
#topleft{
    width: 50px;
    height: 50px;
    background-color: black;
    position:absolute;
    top:0px;
    left:0;
}

#bottomright{
    width: 50px;
    height: 50px;
    background-color: black;
    position: absolute;
    bottom:0px;
    right:0px;
}
#bottomleft{
    width: 50px;
    height: 50px;
    background-color: black;
    position:absolute;
    bottom:0;
    left:0;
}

以下css將起作用

#container {
    background-color: #FF0000;
    height: 400px;
    margin: 0 auto;
    position: relative;
    width: 400px;
}
#topleft {
    background-color: #000000;
    height: 50px;
    left: 0;
    position: absolute;
    top: 0;
    width: 50px;
} 

#topright {
    background-color: #000000;
    height: 50px;
    position: absolute;
    right: 0;
    top: 0;
    width: 50px;
}
#bottomleft {
    background-color: #000000;
    height: 50px;
    left: 350px;
    position: absolute;
    top: 350px;
    width: 50px;
}
#bottomright {
    background-color: #000000;
    height: 50px;
    left: 0;
    position: absolute;
    top: 350px;
    width: 50px;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM