html - CSS 布局 : one item overlaps the other and reaches the border of the container

标签 html css

发现自己我无法弄清楚:

Demo link

CSS

.container {
  width: 50%;
  border: 1px solid #000;
  position: relative;
}
.container .small {
  width: 40%;
  background-color: tomato;
  position: absolute;
  left: 30px;
  height: 250px;
  top: 0;
  bottom: 0;
  margin: auto;
}
.container .big {
  width: 60%;
  background-color: aquamarine;
  margin-left: auto;
}
.container > div {
  height: 300px;
}

HTML

<div class="container">
  <div class="small"></div>
  <div class="big"></div>
</div>

问题: 如何以某种方式修改布局,使 red box 到达容器的黑色边框并仍然与 blue box 重叠。

请注意:

蓝色框的内容可以有不同的高度,蓝色框的高度需要根据红色框的高度而变化(我可以使用 JS 处理)。但是,如果有另一种使用 flexbox 的方法,我们会很高兴听到

浏览器支持是 IE11+、FF36+、Chrome 36+、Android 4.2+、iOS 8+,因此您几乎可以使用任何您喜欢的 CSS 功能。

最佳答案

您是否尝试过将 left 属性设置为零并增加小 div 的宽度。

.container .small {
    width: 45%;
    background-color: tomato;
    position: absolute;
    left: 0px;
    height: 250px;
    top: 0;
    bottom: 0;
    margin: auto;
}

http://codepen.io/anon/pen/gpYwyK

根据您的描述和提供的代码,这具有预期的效果。

关于html - CSS 布局 : one item overlaps the other and reaches the border of the container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29706645/

相关文章:

internet-explorer - 如何在 IE 上使用 CSS3 边框半径和框阴影属性?

html - CSS:在包装器中水平对齐 N 个 div

html - 轮廓属性 CSS 的圆 Angular

html - CSS3 : Transparent Rounded Corner Problem

html - ID 的 CSS 不影响 img 标签

html - 隐藏作者简介

html - 如何通过元素的 id 选择元素的曾孙

html - 我可以在窗口加载时一次制作一个 css 动画吗?

javascript - 在 html 和 javascript 中, '?' 在这个特定的上下文中有何用途?

javascript - 单击按钮时显示/阻止消息