html - 响应式地将 50% 的圆圈对齐到矩形的底部中心?

标签 html css responsive-design responsiveness

我有一个 div 填充了所有的宽度和一定的高度,然后我有一个较小的圆圈,它需要一半在这个 div 的底部/中心,一半在它后面。

一切正常,直到我获得更高的高度或尝试缩放它,然后圆将垂直移动并且不再是 50/50。此外,缩放页面会使圆圈从顶部而非中间展开。

jsFiddle

    <div id="rectangle">
        <img id="circle" src="http://alloutput.com/wp-content/uploads/2013/11/black-circle-mask-to-fill-compass-outline.png">
    </div>

CSS:

body {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    margin-left: 0px;
}
#rectangle {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 92%;
    background: #E5E5E5;
}
#circle {
    position: absolute;
    top: 86%;
    left: 0;
    right: 0;
    width: 100px;
    margin-right: auto;
    margin-left: auto;
}

最佳答案

由于圆在矩形内部,所以它的位置是相对于矩形的。这意味着您需要将 circle bottom 属性设置为圆半径的一半:

body {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    margin-left: 0px;
}
#rectangle {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 92%;
    background: #E5E5E5;
}
#circle {
    position: absolute;
    bottom: -50px;
    left: 0;
    right: 0;
    width: 100px;
    height: 100px;
    margin-right: auto;
    margin-left: auto;
    background: none #000;
    border-radius: 50%;
}
<div id="rectangle">
    <div id="circle"></div>
</div>

我使用 html 元素创建了一个圆圈,而不是您提供的图像(您可以删除它)。

关于html - 响应式地将 50% 的圆圈对齐到矩形的底部中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854213/

相关文章:

css - 页面居中

html - div 宽度被忽略

CSS - 为响应式设计集中 UL

javascript - 是否可以仅使用 JS、HTML 和 CSS 在我的 Chrome 扩展程序的窗口中显示网页?

html - 删除必需属性后 float 标签不起作用

css - 如何在IE中使背景尺寸工作?

datetime - mobiscroll 的开源替代品

html - CSS - 段落正在拉伸(stretch) DIV 的宽度

html - 如何相对于文本定位图像?

jquery - 我想在向下滚动期间修复 <div>