html - CSS 问题重叠图像

标签 html css

请看附图,我想在html中设计这个,非常成功。但是当我在不同的分辨率下测试它时,红框到处移动。我设计了100%宽度和100%高度

enter image description here

<style type="text/css">

    #green-box { width: 75%; background: green; float: left; position: relative; height: 100%; overflow: visible; position: relative; }
    #blue-box { width: 25%; background: blue; float: left; height: 100%; }
    #red-box {
        position: relative;
        top: 50px;
        left:450px;
        width: 357px;
        background: red;
        height: 207px;
        margin:0 auto;
    }
    #green-box-content
    {
        margin:0 auto;
        width:1600px;
        height:800px;


    }
</style>
<div id="container">

        <div id="green-box">
        <div id="green-box-content">
            <p>Here is some text!</p>
            <div id="red-box"></div>
        </div> 
        </div>

        <div id="blue-box">

        </div>

        <div style="clear: both"></div>
    </div>

最佳答案

部分问题在于您尝试定位元素的方式。看起来您希望它在蓝色和绿色之间居中,但您是从左侧定位的。一旦果岭的宽度发生变化,它就不会在你想要的位置。最好从右边(两者之间的边界)定位,并将right设置为宽度的-1/2。

此外,100% 高度仅在父容器具有设置高度时才有效

这是修改后的 CSS 和一个 fiddle to demonstrate

#blue-box,
#green-box {
    height: 300px;
}

#green-box { 
    position: relative; 
    width: 75%; 
    float: left; 

    background: green; 
}
#blue-box { 
    width: 25%; 
    float: left;

    background: blue; 
}
#red-box {
    position: absolute;
    top: 50px;
    right: -178px; /* width / 2 */

    width: 357px;
    height: 207px;

    background: red;
}

关于html - CSS 问题重叠图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17214624/

相关文章:

javascript - 通过 HTML 中的 onClick 将变量传递给外部 js 文件

jquery - 垂直滚动条在 IE 和 firefox 中不可见

javascript - 如何使用Javascript获取3个输入框值的总和?

javascript - 如何在 vanilla javascript 中通过 DOM 创建多个元素?

javascript - 如何使用引导标签输入进行选择

javascript - 如何在 sidenav 中自动对焦文本框

python - 如何使用 beautifulsoup 从嵌套表中获取值

javascript - 更改 SVG 路径的 X 和 Y 坐标

html - 在中心而不是边界处填充 svg 的背景

HTML : Div on Image