html - 将绝对定位的子项设置为父项宽度的 100%,包括边框

标签 html css

我有以下代码:

.parent , .child{
    border: 10px solid black;
    position: relative;
    box-sizing: border-box;
	background:green;
}
.child{
	background:blue;
    position: absolute;
    left: -10px;
    top: 100%;
    border-radius: 0px 0px 10px 10px;
	width:100%
}
<div class="parent">
Parent Element
<div class="child">
Child Element
</div>
</div>

虽然这可以通过将宽度更改为 calc(100% + 20px); 来实现,但是否有更动态的方法来确保底部 div 与顶部对齐(不使用 flexbox ) ?

最佳答案

既然您已经在使用 left: -10px,为什么不简单地使用 right: -10px 而不是设置宽度。

.parent , .child{
    border: 10px solid black;
    position: relative;
    box-sizing: border-box;
	background:green;
}
.child{
	background:blue;
    position: absolute;
    left: -10px;
    right: -10px;
    top: 100%;
    border-radius: 0px 0px 10px 10px;
}
<div class="parent">
Parent Element
<div class="child">
Child Element
</div>
</div>

关于html - 将绝对定位的子项设置为父项宽度的 100%,包括边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55194829/

相关文章:

javascript - 模态脚本中的 "Uncaught TypeError: Cannot set property ' onclick ' of null"

html - CSS 下拉菜单在 IE6 中不起作用

html - CSS:更改选择-选项字体大小,如果外观:无

php - 样式使用 PHP 从 csv 文件创建 div?

javascript - 如何在加载我的网站主体之前预加载我所有的 javascript 文件?

javascript - html iframe vs jquery.load() 动态添加内容时(无 php 等)

javascript - 3 网格布局 : fixed sidebars and fluid content

html - 如何使用 Nokogiri 解析 TABLE 文本?

javascript - 复选框取消点击事件Javascript

html - 如何重叠两个 SVG 图像?