javascript - CSS定位,需要一些建议

标签 javascript html css css-position

这是我的 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 个黑色方 block 都位于红色方 block 的 Angular 落,就像这张图片一样,我应该更改或添加什么代码?谢谢 http://postimg.org/image/r5kv15l5v/

最佳答案

position:relative 添加到 #container 并将 position: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 here.

关于javascript - CSS定位,需要一些建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882556/

相关文章:

javascript - 隐藏文本区域时无法复制到剪贴板

javascript - 延迟加载模块可以使用 root 注入(inject)的 canActivate 防护吗?

javascript - Facebook 分享 og :url overrides data taken from og:title

HTML5 Canvas 颜色区分

html - 如何在较小的屏幕上重新定位一些 block ?

javascript - 将地理数据从 Firebase 导入到谷歌地图

html - 显示中的位置 :absolute,:在 IE 中具有动态高度的表格单元格

html - 2012年布局的首选宽度

javascript - 在自定义html视频播放列表上播放YouTube视频

css悬停不执行