CSS定位,需要一些建议

 紫夜丶月 发布于 2023-02-07 09:10

这是我的HTML代码

这是我的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/

1 个回答
  • 添加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在这里.

    2023-02-07 09:13 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有